| 1 |
//go:build !tinygo |
| 2 |
// +build !tinygo |
| 3 |
|
| 4 |
package gfx |
| 5 |
|
| 6 |
import ( |
| 7 |
"image/color" |
| 8 |
"math" |
| 9 |
) |
| 10 |
|
| 11 |
// ColorToHSL converts a color into HSL. |
| 12 |
func ColorToHSL(c color.Color) HSL { |
| 13 |
var ( |
| 14 |
r, g, b = floatRGB(c) |
| 15 |
min, max = minRGB(r, g, b), maxRGB(r, g, b) |
| 16 |
h, s, l = (max + min) / 2.0, (max + min) / 2.0, (max + min) / 2.0 |
| 17 |
) |
| 18 |
|
| 19 |
if max == min { |
| 20 |
h, s = 0, 0 // achromatic |
| 21 |
} else { |
| 22 |
d := max - min |
| 23 |
|
| 24 |
if l > 0.4 { |
| 25 |
s = d / (2 - max - min) |
| 26 |
} else { |
| 27 |
s = d / (max + min) |
| 28 |
} |
| 29 |
|
| 30 |
switch max { |
| 31 |
case r: |
| 32 |
if g < b { |
| 33 |
h = (g-b)/d + 6 |
| 34 |
} else { |
| 35 |
h = (g-b)/d + 0 |
| 36 |
} |
| 37 |
case g: |
| 38 |
h = (b-r)/d + 2 |
| 39 |
case b: |
| 40 |
h = (r-g)/d + 4 |
| 41 |
} |
| 42 |
|
| 43 |
h /= 6 |
| 44 |
} |
| 45 |
|
| 46 |
return HSL{h * 360, s, l} |
| 47 |
} |
| 48 |
|
| 49 |
// HSL is the hue, saturation and lightness color representation. |
| 50 |
// - Hue [0,360] |
| 51 |
// - Saturation [0,1] |
| 52 |
// - Lightness [0,1] |
| 53 |
type HSL struct { |
| 54 |
Hue float64 |
| 55 |
Saturation float64 |
| 56 |
Lightness float64 |
| 57 |
} |
| 58 |
|
| 59 |
// Components in HSL. |
| 60 |
func (hsl HSL) Components() (h, s, l float64) { |
| 61 |
return hsl.Hue, hsl.Saturation, hsl.Lightness |
| 62 |
} |
| 63 |
|
| 64 |
// RGBA converts a HSL color value to color.RGBA. |
| 65 |
func (hsl HSL) RGBA() color.RGBA { |
| 66 |
h, s, l := hsl.Components() |
| 67 |
|
| 68 |
var r, g, b float64 |
| 69 |
|
| 70 |
switch s { |
| 71 |
case 0: |
| 72 |
r, g, b = l, l, l // achromatic |
| 73 |
default: |
| 74 |
var q = l + s - l*s |
| 75 |
|
| 76 |
if l < 0.5 { |
| 77 |
q = l * (1 + s) |
| 78 |
} |
| 79 |
|
| 80 |
var p = 2*l - q |
| 81 |
|
| 82 |
r = hue2rgb(p, q, h+1.0/3) |
| 83 |
g = hue2rgb(p, q, h) |
| 84 |
b = hue2rgb(p, q, h-1.0/3) |
| 85 |
} |
| 86 |
|
| 87 |
cr := Clamp(math.Round(r*255), 0, 255) |
| 88 |
cg := Clamp(math.Round(g*255), 0, 255) |
| 89 |
cb := Clamp(math.Round(b*255), 0, 255) |
| 90 |
|
| 91 |
return ColorRGBA(uint8(cr), uint8(cg), uint8(cb), 255) |
| 92 |
} |
| 93 |
|
| 94 |
func hue2rgb(p, q, t float64) float64 { |
| 95 |
if t < 0 { |
| 96 |
t += 1.0 |
| 97 |
} |
| 98 |
|
| 99 |
if t > 1 { |
| 100 |
t -= 1.0 |
| 101 |
} |
| 102 |
|
| 103 |
if t < 1.0/6.0 { |
| 104 |
return p + (q-p)*6*t |
| 105 |
} |
| 106 |
|
| 107 |
if t < 1.0/2.0 { |
| 108 |
return q |
| 109 |
} |
| 110 |
|
| 111 |
if t < 2.0/3.0 { |
| 112 |
return p + (q-p)*(2.0/3.0-t)*6 |
| 113 |
} |
| 114 |
|
| 115 |
return p |
| 116 |
} |
| 117 |
|
| 118 |
func floatRGB(c color.Color) (r, g, b float64) { |
| 119 |
cR, cG, cB, _ := c.RGBA() |
| 120 |
|
| 121 |
return float64(cR) / 0xFFFF, float64(cG) / 0xFFFF, float64(cB) / 0xFFFF |
| 122 |
} |
| 123 |
|
| 124 |
func maxRGB(r, g, b float64) float64 { |
| 125 |
return math.Max(r, math.Max(g, b)) |
| 126 |
} |
| 127 |
|
| 128 |
func minRGB(r, g, b float64) float64 { |
| 129 |
return math.Min(r, math.Min(g, b)) |
| 130 |
} |