Created
June 17, 2026 13:54
-
-
Save hinjolicious/ef2beccb53705ed722c4b292a593494c to your computer and use it in GitHub Desktop.
Color conversion utility based on Processing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Red [] | |
| ; Color conversion utility based on Processing | |
| ; Collaborator: Gemini AI | |
| hsb-to-rgb: func [ | |
| "Converts p5.js HSB values (H: 0-360, S: 0-100, B: 0-100) to a Red tuple!" | |
| h [number!] ; hue 0-360 | |
| s [number!] ; saturation 0-255 | |
| b [number!] ; brightness 0-255 | |
| /local c x m r g b-val | |
| ][ | |
| ; 1. Normalize Saturation and Brightness to 0.0 - 1.0 fractions | |
| s: s / 255.0 | |
| b: b / 255.0 | |
| ; Ensure Hue wraps perfectly around the 360-degree circle | |
| h: mod h 360.0 | |
| if h < 0 [h: h + 360.0] | |
| ; 2. Calculate Chroma (Intensity) and intermediate step | |
| c: b * s | |
| x: c * (1.0 - absolute ((mod (h / 60.0) 2.0) - 1.0)) | |
| m: b - c | |
| ; 3. Match raw components based on which 60-degree sector the hue lands in | |
| set [r g b-val] case [ | |
| h < 60.0 [reduce [c x 0.0]] | |
| h < 120.0 [reduce [x c 0.0]] | |
| h < 180.0 [reduce [0.0 c x]] | |
| h < 240.0 [reduce [0.0 x c]] | |
| h < 300.0 [reduce [x 0.0 c]] | |
| true [reduce [c 0.0 x]] | |
| ] | |
| ; 4. Add the baseline brightness match, scale to 0-255, and return a native tuple! | |
| as-color | |
| to integer! (r + m * 255) | |
| to integer! (g + m * 255) | |
| to integer! (b-val + m * 255) | |
| ] | |
| hsl-to-rgb: func [ | |
| "Converts p5.js HSL values (H: 0-360, S: 0-255, L: 0-255) to a Red tuple!" | |
| h [number!] ; hue 0-360 | |
| s [number!] ; saturation 0-255 | |
| l [number!] ; brightness 0-255 | |
| /local c x m r g b-val | |
| ][ | |
| ; 1. Normalize Saturation and Lightness to 0.0 - 1.0 fractions | |
| s: s / 255.0 | |
| l: l / 255.0 | |
| h: mod h 360.0 | |
| if h < 0 [h: h + 360.0] | |
| ; 2. HSL Specific Chroma Calculation: | |
| ; Chroma = (1 - |2L - 1|) * S | |
| c: (1.0 - absolute ((2.0 * l) - 1.0)) * s | |
| x: c * (1.0 - absolute ((mod (h / 60.0) 2.0) - 1.0)) | |
| m: l - (c / 2.0) | |
| ; 3. Map to sectors (Same component logic as HSB) | |
| set [r g b-val] case [ | |
| h < 60.0 [reduce [c x 0.0]] | |
| h < 120.0 [reduce [x c 0.0]] | |
| h < 180.0 [reduce [0.0 c x]] | |
| h < 240.0 [reduce [0.0 x c]] | |
| h < 300.0 [reduce [x 0.0 c]] | |
| true [reduce [c 0.0 x]] | |
| ] | |
| ; 4. Scale to 8-bit using strict Red evaluation order rules | |
| as-color | |
| to integer! ((r + m) * 255) | |
| to integer! ((g + m) * 255) | |
| to integer! ((b-val + m) * 255) | |
| ] | |
| cycle-hue: func [ | |
| h [number!] ; hue 0-360 | |
| /local x | |
| ][ | |
| h: mod h 360.0 | |
| if h < 0 [h: h + 360.0] | |
| ; Calculate intermediate color projection value | |
| x: to integer! (255.0 * (1.0 - absolute ((mod (h / 60.0) 2.0) - 1.0))) | |
| ; Directly route to RGB tuple based on the 60-degree wheel sector | |
| case [ | |
| h < 60.0 [as-color 255 x 0] | |
| h < 120.0 [as-color x 255 0] | |
| h < 180.0 [as-color 0 255 x] | |
| h < 240.0 [as-color 0 x 255] | |
| h < 300.0 [as-color x 0 255] | |
| true [as-color 255 0 x] | |
| ] | |
| ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment