#module_parameters(ASSERTION_ENABLED := true); HSL :: struct { h: u16; s: u8; l: u8; } RGB :: struct { r, g, b: u8; } /* --------- * * HSL procs * * --------- */ hsl_to_integers :: (using hsl: HSL) -> (h: int, s: int, l: int) { return xx h, xx s, xx l; } hsl_to_rgb_hex :: (bg_color_hsl: string) -> string { hsl := string_to_hsl(bg_color_hsl, disable_checks=true); rgb := hsl_to_rgb(hsl); hex := rgb_to_hex(rgb); return hex; } /** https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB_alternative */ hsl_to_rgb :: (hsl: HSL) -> RGB { MAGIC_RED :: 0; MAGIC_GREEN :: 8; MAGIC_BLUE :: 4; convert :: (n: float, h: float, s: float, l: float) -> int { k := fmod_cycling(n + (h / 30.0), 12.0); a := s * min(l, 1.0 - l); value := l - a * max(-1.0, min(min(k - 3.0, 9.0 - k), 1.0)); value *= RGB_MAX; return round(value); } h := cast(float, hsl.h); s := hsl.s / 100.0; l := hsl.l / 100.0; r := convert(MAGIC_RED, h, s, l); g := convert(MAGIC_GREEN, h, s, l); b := convert(MAGIC_BLUE, h, s, l); #if ASSERTION_ENABLED then for int.[r, g, b] { assert(it >= 0 && it <= U8_MAX, "Does not fit inside U8"); } rgb: RGB; rgb.r = xx r; rgb.g = xx g; rgb.b = xx b; return rgb; } /* --------- * * RGB procs * * --------- */ rgb_to_integers :: (using rgb: RGB) -> (r: int, g: int, b: int) { return xx r, xx g, xx b; } /** Idk if people really need a runtime lower case option. We'll see. */ rgb_to_hex :: (using rgb: RGB, $lower_case := false) -> string { #if lower_case { CHARS :: #run to_lower_copy(HEX_ALPHA); } else { CHARS :: HEX_ALPHA; } result := alloc_string(RGB_HEX_STRING_LENGTH); result.data[0] = #char "#"; for u8.[r, g, b] { result.data[it_index * 2 + 1] = CHARS[it >> 4]; result.data[it_index * 2 + 2] = CHARS[it & 0xF]; } return result; } /** https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB */ rgb_to_hsl :: (rgb: RGB) -> HSL { MAGIC_RED :: 6.0; MAGIC_GREEN :: 2.0; MAGIC_BLUE :: 4.0; r := rgb.r / RGB_FMAX; g := rgb.g / RGB_FMAX; b := rgb.b / RGB_FMAX; #if ASSERTION_ENABLED then for float.[r, g, b] { assert(it >= 0.0 && it <= 1.0, "RGB value is not within bounds"); } max_c := max(r, max(g, b)); min_c := min(r, min(g, b)); delta := max_c - min_c; l := (max_c + min_c) / 2.0; s: float; if delta != 0.0 { s = delta / (1.0 - abs(2.0 * l - 1.0)); } h: float; if delta != 0.0 { if max_c == r { h = fmod_cycling((g - b) / delta, MAGIC_RED); } else if max_c == g { h = (b - r) / delta + MAGIC_GREEN; } else { h = (r - g) / delta + MAGIC_BLUE; } h *= 60.0; if h < 0.0 then h += 360.0; } h = roundf(h); s = roundf(s * 100.0); l = roundf(l * 100.0); #if ASSERTION_ENABLED assert(h >= 0.0 && h <= 360.0, "Hue value is not within expected bounds"); #if ASSERTION_ENABLED then for float.[s, l] { assert(it >= 0.0 && it <= 100, "SL value is not within expected bounds"); } hsl: HSL; hsl.h = xx h; hsl.s = xx s; hsl.l = xx l; return hsl; } /* -------------- * * int_to_* procs * * -------------- */ integers_to_hsl :: (h: int, s: int, l: int, loc := #caller_location) -> HSL { #if ASSERTION_ENABLED { assert(h >= 0 && h <= 360, "%: H value is not within expected bounds", loc); assert(s >= 0 && s <= 100, "%: S value is not within expected bounds", loc); assert(l >= 0 && l <= 100, "%: L value is not within expected bounds", loc); } hsl: HSL = ---; hsl.h = xx h; hsl.s = xx s; hsl.l = xx l; return hsl; } integers_to_rgb :: (r: int, g: int, b: int, loc := #caller_location) -> RGB { #if ASSERTION_ENABLED { assert(r >= 0 && r <= RGB_MAX, "%: R value is not within expected bounds", loc); assert(g >= 0 && g <= RGB_MAX, "%: G value is not within expected bounds", loc); assert(b >= 0 && b <= RGB_MAX, "%: B value is not within expected bounds", loc); } rgb: RGB = ---; rgb.r = xx r; rgb.g = xx g; rgb.b = xx b; return rgb; } /* ----------------- * * string_to_* procs * * ----------------- */ string_to_hsl :: (maybe_hsl: string, $disable_checks := false, $do_logging := true, loc := #caller_location) -> (h: HSL = {}, ok: bool = false) { hsl_arr := split(maybe_hsl, " ",, temp); #if !disable_checks { if hsl_arr.count != 3 { #if do_logging then log_error( "%: Could not convert HSL string to HSL values. Your string does not have three value pairs: '%'", loc, maybe_hsl, flags=.ERROR, ); return; } for digit: hsl_arr for digit { if !is_digit(it) { #if do_logging then log_error("%: '%' is not a digit", loc, it); return; } } } h := to_integer(hsl_arr[0]); s := to_integer(hsl_arr[1]); l := to_integer(hsl_arr[2]); hsl := integers_to_hsl(h, s, l, loc=loc); return hsl, true; } string_to_rgb :: (maybe_rgb: string, $disable_checks := false, $do_logging := true, loc := #caller_location) -> (r: RGB = {}, ok: bool = false) { parse_byte :: (s: string) -> (ok: bool, b: u8) #expand { hi_ok, hi := char_to_hex(s[0]); lo_ok, lo := char_to_hex(s[1]); if !hi_ok || !lo_ok { #if `do_logging log_error("%: RGB value '%' is not in hex range", `loc, s); return false, 0; } byte := hi << 4 | lo; return true, byte; } char_to_hex :: (c: u8) -> (ok: bool, u8) { if c >= #char "0" && c <= #char "9" return true, c - #char "0"; if c >= #char "A" && c <= #char "F" return true, c - #char "A" + 10; return false, 0; } possibly_rgb: string; #if !disable_checks { if maybe_rgb.count != RGB_HEX_STRING_LENGTH { #if do_logging then log_error( "%: % Does not equal % characters in '%'", loc, RGB_ERROR_MSG, RGB_HEX_STRING_LENGTH, maybe_rgb ); return; } if maybe_rgb.count > 0 && maybe_rgb[0] != "#" { #if do_logging then log_error( "%: % Does not start with '#' in '%'", loc, RGB_ERROR_MSG, maybe_rgb ); return; } for maybe_rgb if (it >= #char "a" && it <= #char "z") { #if do_logging then log_error( "%: % Is not upper case in '%'", loc, RGB_ERROR_MSG, maybe_rgb ); return; } possibly_rgb = advance(maybe_rgb); for possibly_rgb { if !contains(HEX_ALPHA, it) { #if do_logging then log_error( "%: '%' Invalid character: '%' in '%'", loc, RGB_ERROR_MSG, it, maybe_rgb ); return; } } } ok: bool; rgb: RGB; ok, rgb.r = parse_byte(slice(maybe_rgb, 1, 2)); if !ok return; ok, rgb.g = parse_byte(slice(maybe_rgb, 3, 2)); if !ok return; ok, rgb.b = parse_byte(slice(maybe_rgb, 5, 2)); if !ok return; return rgb, true; } #scope_file HEX_ALPHA :: "0123456789ABCDEF"; RGB_MAX :: 255; RGB_FMAX :: 255.0; RGB_HEX_STRING_LENGTH :: 7; RGB_ERROR_MSG :: "Invalid RGB string."; roundf :: inline (x: float) -> float { return floor(x + 0.5); } round :: inline (x: float) -> int { return xx floor(x + 0.5); } using,only( log_error, min, max, temp, assert, alloc_string, to_integer, advance, is_digit ) Basic :: #import "Basic"; using,only(slice, split, contains) String :: #import "String"; using,only(fmod_cycling, U8_MAX, abs, floor) Math :: #import "Math"; /* ------------------------------------------------------------------------------ This software is available under 2 licenses -- choose whichever you prefer. ------------------------------------------------------------------------------ ALTERNATIVE A - MIT License Copyright (c) 2026 Adam Blazeowsky Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------ ALTERNATIVE B - Public Domain (www.unlicense.org) This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------ */