| 1 |
package colorful |
| 2 |
|
| 3 |
import "math" |
| 4 |
|
| 5 |
// Source: https://github.com/hsluv/hsluv-go |
| 6 |
// Under MIT License |
| 7 |
// Modified so that Saturation and Luminance are in [0..1] instead of [0..100]. |
| 8 |
|
| 9 |
// HSLuv uses a rounded version of the D65. This has no impact on the final RGB |
| 10 |
// values, but to keep high levels of accuracy for internal operations and when |
| 11 |
// comparing to the test values, this modified white reference is used internally. |
| 12 |
// |
| 13 |
// See this GitHub thread for details on these values: |
| 14 |
// |
| 15 |
// https://github.com/hsluv/hsluv/issues/79 |
| 16 |
var hSLuvD65 = [3]float64{0.95045592705167, 1.0, 1.089057750759878} |
| 17 |
|
| 18 |
func LuvLChToHSLuv(l, c, h float64) (float64, float64, float64) { |
| 19 |
// [-1..1] but the code expects it to be [-100..100] |
| 20 |
c *= 100.0 |
| 21 |
l *= 100.0 |
| 22 |
|
| 23 |
var s, max float64 |
| 24 |
if l > 99.9999999 || l < 0.00000001 { |
| 25 |
s = 0.0 |
| 26 |
} else { |
| 27 |
max = maxChromaForLH(l, h) |
| 28 |
s = c / max * 100.0 |
| 29 |
} |
| 30 |
return h, clamp01(s / 100.0), clamp01(l / 100.0) |
| 31 |
} |
| 32 |
|
| 33 |
func HSLuvToLuvLCh(h, s, l float64) (float64, float64, float64) { |
| 34 |
l *= 100.0 |
| 35 |
s *= 100.0 |
| 36 |
|
| 37 |
var c, max float64 |
| 38 |
if l > 99.9999999 || l < 0.00000001 { |
| 39 |
c = 0.0 |
| 40 |
} else { |
| 41 |
max = maxChromaForLH(l, h) |
| 42 |
c = max / 100.0 * s |
| 43 |
} |
| 44 |
|
| 45 |
// c is [-100..100], but for LCh it's supposed to be almost [-1..1] |
| 46 |
return clamp01(l / 100.0), c / 100.0, h |
| 47 |
} |
| 48 |
|
| 49 |
func LuvLChToHPLuv(l, c, h float64) (float64, float64, float64) { |
| 50 |
// [-1..1] but the code expects it to be [-100..100] |
| 51 |
c *= 100.0 |
| 52 |
l *= 100.0 |
| 53 |
|
| 54 |
var s, max float64 |
| 55 |
if l > 99.9999999 || l < 0.00000001 { |
| 56 |
s = 0.0 |
| 57 |
} else { |
| 58 |
max = maxSafeChromaForL(l) |
| 59 |
s = c / max * 100.0 |
| 60 |
} |
| 61 |
return h, s / 100.0, l / 100.0 |
| 62 |
} |
| 63 |
|
| 64 |
func HPLuvToLuvLCh(h, s, l float64) (float64, float64, float64) { |
| 65 |
// [-1..1] but the code expects it to be [-100..100] |
| 66 |
l *= 100.0 |
| 67 |
s *= 100.0 |
| 68 |
|
| 69 |
var c, max float64 |
| 70 |
if l > 99.9999999 || l < 0.00000001 { |
| 71 |
c = 0.0 |
| 72 |
} else { |
| 73 |
max = maxSafeChromaForL(l) |
| 74 |
c = max / 100.0 * s |
| 75 |
} |
| 76 |
return l / 100.0, c / 100.0, h |
| 77 |
} |
| 78 |
|
| 79 |
// HSLuv creates a new Color from values in the HSLuv color space. |
| 80 |
// Hue in [0..360], a Saturation [0..1], and a Luminance (lightness) in [0..1]. |
| 81 |
// |
| 82 |
// The returned color values are clamped (using .Clamped), so this will never output |
| 83 |
// an invalid color. |
| 84 |
func HSLuv(h, s, l float64) Color { |
| 85 |
// HSLuv -> LuvLCh -> CIELUV -> CIEXYZ -> Linear RGB -> sRGB |
| 86 |
l, u, v := LuvLChToLuv(HSLuvToLuvLCh(h, s, l)) |
| 87 |
return LinearRgb(XyzToLinearRgb(LuvToXyzWhiteRef(l, u, v, hSLuvD65))).Clamped() |
| 88 |
} |
| 89 |
|
| 90 |
// HPLuv creates a new Color from values in the HPLuv color space. |
| 91 |
// Hue in [0..360], a Saturation [0..1], and a Luminance (lightness) in [0..1]. |
| 92 |
// |
| 93 |
// The returned color values are clamped (using .Clamped), so this will never output |
| 94 |
// an invalid color. |
| 95 |
func HPLuv(h, s, l float64) Color { |
| 96 |
// HPLuv -> LuvLCh -> CIELUV -> CIEXYZ -> Linear RGB -> sRGB |
| 97 |
l, u, v := LuvLChToLuv(HPLuvToLuvLCh(h, s, l)) |
| 98 |
return LinearRgb(XyzToLinearRgb(LuvToXyzWhiteRef(l, u, v, hSLuvD65))).Clamped() |
| 99 |
} |
| 100 |
|
| 101 |
// HSLuv returns the Hue, Saturation and Luminance of the color in the HSLuv |
| 102 |
// color space. Hue in [0..360], a Saturation [0..1], and a Luminance |
| 103 |
// (lightness) in [0..1]. |
| 104 |
func (col Color) HSLuv() (h, s, l float64) { |
| 105 |
// sRGB -> Linear RGB -> CIEXYZ -> CIELUV -> LuvLCh -> HSLuv |
| 106 |
return LuvLChToHSLuv(col.LuvLChWhiteRef(hSLuvD65)) |
| 107 |
} |
| 108 |
|
| 109 |
// HPLuv returns the Hue, Saturation and Luminance of the color in the HSLuv |
| 110 |
// color space. Hue in [0..360], a Saturation [0..1], and a Luminance |
| 111 |
// (lightness) in [0..1]. |
| 112 |
// |
| 113 |
// Note that HPLuv can only represent pastel colors, and so the Saturation |
| 114 |
// value could be much larger than 1 for colors it can't represent. |
| 115 |
func (col Color) HPLuv() (h, s, l float64) { |
| 116 |
return LuvLChToHPLuv(col.LuvLChWhiteRef(hSLuvD65)) |
| 117 |
} |
| 118 |
|
| 119 |
// DistanceHSLuv calculates Euclidean distance in the HSLuv colorspace. No idea |
| 120 |
// how useful this is. |
| 121 |
// |
| 122 |
// The Hue value is divided by 100 before the calculation, so that H, S, and L |
| 123 |
// have the same relative ranges. |
| 124 |
func (c1 Color) DistanceHSLuv(c2 Color) float64 { |
| 125 |
h1, s1, l1 := c1.HSLuv() |
| 126 |
h2, s2, l2 := c2.HSLuv() |
| 127 |
return math.Sqrt(sq((h1-h2)/100.0) + sq(s1-s2) + sq(l1-l2)) |
| 128 |
} |
| 129 |
|
| 130 |
// DistanceHPLuv calculates Euclidean distance in the HPLuv colorspace. No idea |
| 131 |
// how useful this is. |
| 132 |
// |
| 133 |
// The Hue value is divided by 100 before the calculation, so that H, S, and L |
| 134 |
// have the same relative ranges. |
| 135 |
func (c1 Color) DistanceHPLuv(c2 Color) float64 { |
| 136 |
h1, s1, l1 := c1.HPLuv() |
| 137 |
h2, s2, l2 := c2.HPLuv() |
| 138 |
return math.Sqrt(sq((h1-h2)/100.0) + sq(s1-s2) + sq(l1-l2)) |
| 139 |
} |
| 140 |
|
| 141 |
var m = [3][3]float64{ |
| 142 |
{3.2409699419045214, -1.5373831775700935, -0.49861076029300328}, |
| 143 |
{-0.96924363628087983, 1.8759675015077207, 0.041555057407175613}, |
| 144 |
{0.055630079696993609, -0.20397695888897657, 1.0569715142428786}, |
| 145 |
} |
| 146 |
|
| 147 |
const kappa = 903.2962962962963 |
| 148 |
const epsilon = 0.0088564516790356308 |
| 149 |
|
| 150 |
func maxChromaForLH(l, h float64) float64 { |
| 151 |
hRad := h / 360.0 * math.Pi * 2.0 |
| 152 |
minLength := math.MaxFloat64 |
| 153 |
for _, line := range getBounds(l) { |
| 154 |
length := lengthOfRayUntilIntersect(hRad, line[0], line[1]) |
| 155 |
if length > 0.0 && length < minLength { |
| 156 |
minLength = length |
| 157 |
} |
| 158 |
} |
| 159 |
return minLength |
| 160 |
} |
| 161 |
|
| 162 |
func getBounds(l float64) [6][2]float64 { |
| 163 |
var sub2 float64 |
| 164 |
var ret [6][2]float64 |
| 165 |
sub1 := math.Pow(l+16.0, 3.0) / 1560896.0 |
| 166 |
if sub1 > epsilon { |
| 167 |
sub2 = sub1 |
| 168 |
} else { |
| 169 |
sub2 = l / kappa |
| 170 |
} |
| 171 |
for i := range m { |
| 172 |
for k := 0; k < 2; k++ { |
| 173 |
top1 := (284517.0*m[i][0] - 94839.0*m[i][2]) * sub2 |
| 174 |
top2 := (838422.0*m[i][2]+769860.0*m[i][1]+731718.0*m[i][0])*l*sub2 - 769860.0*float64(k)*l |
| 175 |
bottom := (632260.0*m[i][2]-126452.0*m[i][1])*sub2 + 126452.0*float64(k) |
| 176 |
ret[i*2+k][0] = top1 / bottom |
| 177 |
ret[i*2+k][1] = top2 / bottom |
| 178 |
} |
| 179 |
} |
| 180 |
return ret |
| 181 |
} |
| 182 |
|
| 183 |
func lengthOfRayUntilIntersect(theta, x, y float64) (length float64) { |
| 184 |
length = y / (math.Sin(theta) - x*math.Cos(theta)) |
| 185 |
return |
| 186 |
} |
| 187 |
|
| 188 |
func maxSafeChromaForL(l float64) float64 { |
| 189 |
minLength := math.MaxFloat64 |
| 190 |
for _, line := range getBounds(l) { |
| 191 |
m1 := line[0] |
| 192 |
b1 := line[1] |
| 193 |
x := intersectLineLine(m1, b1, -1.0/m1, 0.0) |
| 194 |
dist := distanceFromPole(x, b1+x*m1) |
| 195 |
if dist < minLength { |
| 196 |
minLength = dist |
| 197 |
} |
| 198 |
} |
| 199 |
return minLength |
| 200 |
} |
| 201 |
|
| 202 |
func intersectLineLine(x1, y1, x2, y2 float64) float64 { |
| 203 |
return (y1 - y2) / (x2 - x1) |
| 204 |
} |
| 205 |
|
| 206 |
func distanceFromPole(x, y float64) float64 { |
| 207 |
return math.Sqrt(math.Pow(x, 2.0) + math.Pow(y, 2.0)) |
| 208 |
} |