| 1 |
// The colorful package provides all kinds of functions for working with colors. |
| 2 |
package colorful |
| 3 |
|
| 4 |
import ( |
| 5 |
"fmt" |
| 6 |
"image/color" |
| 7 |
"math" |
| 8 |
"strconv" |
| 9 |
) |
| 10 |
|
| 11 |
// A color is stored internally using sRGB (standard RGB) values in the range 0-1 |
| 12 |
type Color struct { |
| 13 |
R, G, B float64 |
| 14 |
} |
| 15 |
|
| 16 |
// Implement the Go color.Color interface. |
| 17 |
func (col Color) RGBA() (r, g, b, a uint32) { |
| 18 |
r = uint32(col.R*65535.0 + 0.5) |
| 19 |
g = uint32(col.G*65535.0 + 0.5) |
| 20 |
b = uint32(col.B*65535.0 + 0.5) |
| 21 |
a = 0xFFFF |
| 22 |
return |
| 23 |
} |
| 24 |
|
| 25 |
// Constructs a colorful.Color from something implementing color.Color |
| 26 |
func MakeColor(col color.Color) (Color, bool) { |
| 27 |
r, g, b, a := col.RGBA() |
| 28 |
if a == 0 { |
| 29 |
return Color{0, 0, 0}, false |
| 30 |
} |
| 31 |
|
| 32 |
// Since color.Color is alpha pre-multiplied, we need to divide the |
| 33 |
// RGB values by alpha again in order to get back the original RGB. |
| 34 |
r *= 0xffff |
| 35 |
r /= a |
| 36 |
g *= 0xffff |
| 37 |
g /= a |
| 38 |
b *= 0xffff |
| 39 |
b /= a |
| 40 |
|
| 41 |
return Color{float64(r) / 65535.0, float64(g) / 65535.0, float64(b) / 65535.0}, true |
| 42 |
} |
| 43 |
|
| 44 |
// Might come in handy sometimes to reduce boilerplate code. |
| 45 |
func (col Color) RGB255() (r, g, b uint8) { |
| 46 |
r = uint8(col.R*255.0 + 0.5) |
| 47 |
g = uint8(col.G*255.0 + 0.5) |
| 48 |
b = uint8(col.B*255.0 + 0.5) |
| 49 |
return |
| 50 |
} |
| 51 |
|
| 52 |
// Used to simplify HSLuv testing. |
| 53 |
func (col Color) values() (float64, float64, float64) { |
| 54 |
return col.R, col.G, col.B |
| 55 |
} |
| 56 |
|
| 57 |
// This is the tolerance used when comparing colors using AlmostEqualRgb. |
| 58 |
const Delta = 1.0 / 255.0 |
| 59 |
|
| 60 |
// This is the default reference white point. |
| 61 |
var D65 = [3]float64{0.95047, 1.00000, 1.08883} |
| 62 |
|
| 63 |
// And another one. |
| 64 |
var D50 = [3]float64{0.96422, 1.00000, 0.82521} |
| 65 |
|
| 66 |
// Checks whether the color exists in RGB space, i.e. all values are in [0..1] |
| 67 |
func (c Color) IsValid() bool { |
| 68 |
return 0.0 <= c.R && c.R <= 1.0 && |
| 69 |
0.0 <= c.G && c.G <= 1.0 && |
| 70 |
0.0 <= c.B && c.B <= 1.0 |
| 71 |
} |
| 72 |
|
| 73 |
// clamp01 clamps from 0 to 1. |
| 74 |
func clamp01(v float64) float64 { |
| 75 |
return math.Max(0.0, math.Min(v, 1.0)) |
| 76 |
} |
| 77 |
|
| 78 |
// Returns Clamps the color into valid range, clamping each value to [0..1] |
| 79 |
// If the color is valid already, this is a no-op. |
| 80 |
func (c Color) Clamped() Color { |
| 81 |
return Color{clamp01(c.R), clamp01(c.G), clamp01(c.B)} |
| 82 |
} |
| 83 |
|
| 84 |
func sq(v float64) float64 { |
| 85 |
return v * v |
| 86 |
} |
| 87 |
|
| 88 |
func cub(v float64) float64 { |
| 89 |
return v * v * v |
| 90 |
} |
| 91 |
|
| 92 |
// DistanceRgb computes the distance between two colors in RGB space. |
| 93 |
// This is not a good measure! Rather do it in Lab space. |
| 94 |
func (c1 Color) DistanceRgb(c2 Color) float64 { |
| 95 |
return math.Sqrt(sq(c1.R-c2.R) + sq(c1.G-c2.G) + sq(c1.B-c2.B)) |
| 96 |
} |
| 97 |
|
| 98 |
// DistanceLinearRgb computes the distance between two colors in linear RGB |
| 99 |
// space. This is not useful for measuring how humans perceive color, but |
| 100 |
// might be useful for other things, like dithering. |
| 101 |
func (c1 Color) DistanceLinearRgb(c2 Color) float64 { |
| 102 |
r1, g1, b1 := c1.LinearRgb() |
| 103 |
r2, g2, b2 := c2.LinearRgb() |
| 104 |
return math.Sqrt(sq(r1-r2) + sq(g1-g2) + sq(b1-b2)) |
| 105 |
} |
| 106 |
|
| 107 |
// DistanceLinearRGB is deprecated in favour of DistanceLinearRgb. |
| 108 |
// They do the exact same thing. |
| 109 |
func (c1 Color) DistanceLinearRGB(c2 Color) float64 { |
| 110 |
return c1.DistanceLinearRgb(c2) |
| 111 |
} |
| 112 |
|
| 113 |
// DistanceRiemersma is a color distance algorithm developed by Thiadmer Riemersma. |
| 114 |
// It uses RGB coordinates, but he claims it has similar results to CIELUV. |
| 115 |
// This makes it both fast and accurate. |
| 116 |
// |
| 117 |
// Sources: |
| 118 |
// |
| 119 |
// https://www.compuphase.com/cmetric.htm |
| 120 |
// https://github.com/lucasb-eyer/go-colorful/issues/52 |
| 121 |
func (c1 Color) DistanceRiemersma(c2 Color) float64 { |
| 122 |
rAvg := (c1.R + c2.R) / 2.0 |
| 123 |
// Deltas |
| 124 |
dR := c1.R - c2.R |
| 125 |
dG := c1.G - c2.G |
| 126 |
dB := c1.B - c2.B |
| 127 |
|
| 128 |
return math.Sqrt((2+rAvg)*dR*dR + 4*dG*dG + (2+(1-rAvg))*dB*dB) |
| 129 |
} |
| 130 |
|
| 131 |
// Check for equality between colors within the tolerance Delta (1/255). |
| 132 |
func (c1 Color) AlmostEqualRgb(c2 Color) bool { |
| 133 |
return math.Abs(c1.R-c2.R)+ |
| 134 |
math.Abs(c1.G-c2.G)+ |
| 135 |
math.Abs(c1.B-c2.B) < 3.0*Delta |
| 136 |
} |
| 137 |
|
| 138 |
// You don't really want to use this, do you? Go for BlendLab, BlendLuv or BlendHcl. |
| 139 |
func (c1 Color) BlendRgb(c2 Color, t float64) Color { |
| 140 |
return Color{ |
| 141 |
c1.R + t*(c2.R-c1.R), |
| 142 |
c1.G + t*(c2.G-c1.G), |
| 143 |
c1.B + t*(c2.B-c1.B), |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
// Utility used by Hxx color-spaces for interpolating between two angles in [0,360]. |
| 148 |
func interp_angle(a0, a1, t float64) float64 { |
| 149 |
// Based on the answer here: http://stackoverflow.com/a/14498790/2366315 |
| 150 |
// With potential proof that it works here: http://math.stackexchange.com/a/2144499 |
| 151 |
delta := math.Mod(math.Mod(a1-a0, 360.0)+540, 360.0) - 180.0 |
| 152 |
return math.Mod(a0+t*delta+360.0, 360.0) |
| 153 |
} |
| 154 |
|
| 155 |
/// HSV /// |
| 156 |
/////////// |
| 157 |
// From http://en.wikipedia.org/wiki/HSL_and_HSV |
| 158 |
// Note that h is in [0..359] and s,v in [0..1] |
| 159 |
|
| 160 |
// Hsv returns the Hue [0..359], Saturation and Value [0..1] of the color. |
| 161 |
func (col Color) Hsv() (h, s, v float64) { |
| 162 |
min := math.Min(math.Min(col.R, col.G), col.B) |
| 163 |
v = math.Max(math.Max(col.R, col.G), col.B) |
| 164 |
C := v - min |
| 165 |
|
| 166 |
s = 0.0 |
| 167 |
if v != 0.0 { |
| 168 |
s = C / v |
| 169 |
} |
| 170 |
|
| 171 |
h = 0.0 // We use 0 instead of undefined as in wp. |
| 172 |
if min != v { |
| 173 |
if v == col.R { |
| 174 |
h = math.Mod((col.G-col.B)/C, 6.0) |
| 175 |
} |
| 176 |
if v == col.G { |
| 177 |
h = (col.B-col.R)/C + 2.0 |
| 178 |
} |
| 179 |
if v == col.B { |
| 180 |
h = (col.R-col.G)/C + 4.0 |
| 181 |
} |
| 182 |
h *= 60.0 |
| 183 |
if h < 0.0 { |
| 184 |
h += 360.0 |
| 185 |
} |
| 186 |
} |
| 187 |
return |
| 188 |
} |
| 189 |
|
| 190 |
// Hsv creates a new Color given a Hue in [0..359], a Saturation and a Value in [0..1] |
| 191 |
func Hsv(H, S, V float64) Color { |
| 192 |
Hp := H / 60.0 |
| 193 |
C := V * S |
| 194 |
X := C * (1.0 - math.Abs(math.Mod(Hp, 2.0)-1.0)) |
| 195 |
|
| 196 |
m := V - C |
| 197 |
r, g, b := 0.0, 0.0, 0.0 |
| 198 |
|
| 199 |
switch { |
| 200 |
case 0.0 <= Hp && Hp < 1.0: |
| 201 |
r = C |
| 202 |
g = X |
| 203 |
case 1.0 <= Hp && Hp < 2.0: |
| 204 |
r = X |
| 205 |
g = C |
| 206 |
case 2.0 <= Hp && Hp < 3.0: |
| 207 |
g = C |
| 208 |
b = X |
| 209 |
case 3.0 <= Hp && Hp < 4.0: |
| 210 |
g = X |
| 211 |
b = C |
| 212 |
case 4.0 <= Hp && Hp < 5.0: |
| 213 |
r = X |
| 214 |
b = C |
| 215 |
case 5.0 <= Hp && Hp < 6.0: |
| 216 |
r = C |
| 217 |
b = X |
| 218 |
} |
| 219 |
|
| 220 |
return Color{m + r, m + g, m + b} |
| 221 |
} |
| 222 |
|
| 223 |
// You don't really want to use this, do you? Go for BlendLab, BlendLuv or BlendHcl. |
| 224 |
func (c1 Color) BlendHsv(c2 Color, t float64) Color { |
| 225 |
h1, s1, v1 := c1.Hsv() |
| 226 |
h2, s2, v2 := c2.Hsv() |
| 227 |
|
| 228 |
// https://github.com/lucasb-eyer/go-colorful/pull/60 |
| 229 |
if s1 == 0 && s2 != 0 { |
| 230 |
h1 = h2 |
| 231 |
} else if s2 == 0 && s1 != 0 { |
| 232 |
h2 = h1 |
| 233 |
} |
| 234 |
|
| 235 |
// We know that h are both in [0..360] |
| 236 |
return Hsv(interp_angle(h1, h2, t), s1+t*(s2-s1), v1+t*(v2-v1)) |
| 237 |
} |
| 238 |
|
| 239 |
/// HSL /// |
| 240 |
/////////// |
| 241 |
|
| 242 |
// Hsl returns the Hue [0..359], Saturation [0..1], and Luminance (lightness) [0..1] of the color. |
| 243 |
func (col Color) Hsl() (h, s, l float64) { |
| 244 |
min := math.Min(math.Min(col.R, col.G), col.B) |
| 245 |
max := math.Max(math.Max(col.R, col.G), col.B) |
| 246 |
|
| 247 |
l = (max + min) / 2 |
| 248 |
|
| 249 |
if min == max { |
| 250 |
s = 0 |
| 251 |
h = 0 |
| 252 |
} else { |
| 253 |
if l < 0.5 { |
| 254 |
s = (max - min) / (max + min) |
| 255 |
} else { |
| 256 |
s = (max - min) / (2.0 - max - min) |
| 257 |
} |
| 258 |
|
| 259 |
if max == col.R { |
| 260 |
h = (col.G - col.B) / (max - min) |
| 261 |
} else if max == col.G { |
| 262 |
h = 2.0 + (col.B-col.R)/(max-min) |
| 263 |
} else { |
| 264 |
h = 4.0 + (col.R-col.G)/(max-min) |
| 265 |
} |
| 266 |
|
| 267 |
h *= 60 |
| 268 |
|
| 269 |
if h < 0 { |
| 270 |
h += 360 |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
return |
| 275 |
} |
| 276 |
|
| 277 |
// Hsl creates a new Color given a Hue in [0..359], a Saturation [0..1], and a Luminance (lightness) in [0..1] |
| 278 |
func Hsl(h, s, l float64) Color { |
| 279 |
if s == 0 { |
| 280 |
return Color{l, l, l} |
| 281 |
} |
| 282 |
|
| 283 |
var r, g, b float64 |
| 284 |
var t1 float64 |
| 285 |
var t2 float64 |
| 286 |
var tr float64 |
| 287 |
var tg float64 |
| 288 |
var tb float64 |
| 289 |
|
| 290 |
if l < 0.5 { |
| 291 |
t1 = l * (1.0 + s) |
| 292 |
} else { |
| 293 |
t1 = l + s - l*s |
| 294 |
} |
| 295 |
|
| 296 |
t2 = 2*l - t1 |
| 297 |
h /= 360 |
| 298 |
tr = h + 1.0/3.0 |
| 299 |
tg = h |
| 300 |
tb = h - 1.0/3.0 |
| 301 |
|
| 302 |
if tr < 0 { |
| 303 |
tr++ |
| 304 |
} |
| 305 |
if tr > 1 { |
| 306 |
tr-- |
| 307 |
} |
| 308 |
if tg < 0 { |
| 309 |
tg++ |
| 310 |
} |
| 311 |
if tg > 1 { |
| 312 |
tg-- |
| 313 |
} |
| 314 |
if tb < 0 { |
| 315 |
tb++ |
| 316 |
} |
| 317 |
if tb > 1 { |
| 318 |
tb-- |
| 319 |
} |
| 320 |
|
| 321 |
// Red |
| 322 |
if 6*tr < 1 { |
| 323 |
r = t2 + (t1-t2)*6*tr |
| 324 |
} else if 2*tr < 1 { |
| 325 |
r = t1 |
| 326 |
} else if 3*tr < 2 { |
| 327 |
r = t2 + (t1-t2)*(2.0/3.0-tr)*6 |
| 328 |
} else { |
| 329 |
r = t2 |
| 330 |
} |
| 331 |
|
| 332 |
// Green |
| 333 |
if 6*tg < 1 { |
| 334 |
g = t2 + (t1-t2)*6*tg |
| 335 |
} else if 2*tg < 1 { |
| 336 |
g = t1 |
| 337 |
} else if 3*tg < 2 { |
| 338 |
g = t2 + (t1-t2)*(2.0/3.0-tg)*6 |
| 339 |
} else { |
| 340 |
g = t2 |
| 341 |
} |
| 342 |
|
| 343 |
// Blue |
| 344 |
if 6*tb < 1 { |
| 345 |
b = t2 + (t1-t2)*6*tb |
| 346 |
} else if 2*tb < 1 { |
| 347 |
b = t1 |
| 348 |
} else if 3*tb < 2 { |
| 349 |
b = t2 + (t1-t2)*(2.0/3.0-tb)*6 |
| 350 |
} else { |
| 351 |
b = t2 |
| 352 |
} |
| 353 |
|
| 354 |
return Color{r, g, b} |
| 355 |
} |
| 356 |
|
| 357 |
/// Hex /// |
| 358 |
/////////// |
| 359 |
|
| 360 |
// Hex returns the hex "html" representation of the color, as in #ff0080. |
| 361 |
func (col Color) Hex() string { |
| 362 |
// Add 0.5 for rounding |
| 363 |
return fmt.Sprintf("#%02x%02x%02x", uint8(col.R*255.0+0.5), uint8(col.G*255.0+0.5), uint8(col.B*255.0+0.5)) |
| 364 |
} |
| 365 |
|
| 366 |
// Hex parses a "html" hex color-string, either in the 3 "#f0c" or 6 "#ff1034" digits form. |
| 367 |
func Hex(scol string) (Color, error) { |
| 368 |
if scol == "" || scol[0] != '#' { |
| 369 |
return Color{}, fmt.Errorf("color: %v is not a hex-color", scol) |
| 370 |
} |
| 371 |
var c Color |
| 372 |
var err error |
| 373 |
switch len(scol) { |
| 374 |
case 4: |
| 375 |
c, err = parseHexColor(scol[1:2], scol[2:3], scol[3:4], 4, 1.0/15.0) |
| 376 |
case 7: |
| 377 |
c, err = parseHexColor(scol[1:3], scol[3:5], scol[5:7], 8, 1.0/255.0) |
| 378 |
default: |
| 379 |
return Color{}, fmt.Errorf("color: %v is not a hex-color", scol) |
| 380 |
} |
| 381 |
if err != nil { |
| 382 |
return Color{}, fmt.Errorf("color: %v is not a hex-color: %w", scol, err) |
| 383 |
} |
| 384 |
return c, nil |
| 385 |
} |
| 386 |
|
| 387 |
func parseHexColor(r, g, b string, bits int, factor float64) (Color, error) { |
| 388 |
var c Color |
| 389 |
var v uint64 |
| 390 |
var err error |
| 391 |
|
| 392 |
if v, err = strconv.ParseUint(r, 16, bits); err != nil { |
| 393 |
return Color{}, err |
| 394 |
} |
| 395 |
c.R = float64(v) * factor |
| 396 |
|
| 397 |
if v, err = strconv.ParseUint(g, 16, bits); err != nil { |
| 398 |
return Color{}, err |
| 399 |
} |
| 400 |
c.G = float64(v) * factor |
| 401 |
|
| 402 |
if v, err = strconv.ParseUint(b, 16, bits); err != nil { |
| 403 |
return Color{}, err |
| 404 |
} |
| 405 |
c.B = float64(v) * factor |
| 406 |
|
| 407 |
return c, err |
| 408 |
} |
| 409 |
|
| 410 |
/// Linear /// |
| 411 |
////////////// |
| 412 |
// http://www.sjbrown.co.uk/2004/05/14/gamma-correct-rendering/ |
| 413 |
// http://www.brucelindbloom.com/Eqn_RGB_to_XYZ.html |
| 414 |
|
| 415 |
func linearize(v float64) float64 { |
| 416 |
if v <= 0.04045 { |
| 417 |
return v / 12.92 |
| 418 |
} |
| 419 |
return math.Pow((v+0.055)/1.055, 2.4) |
| 420 |
} |
| 421 |
|
| 422 |
// LinearRgb converts the color into the linear RGB space (see http://www.sjbrown.co.uk/2004/05/14/gamma-correct-rendering/). |
| 423 |
func (col Color) LinearRgb() (r, g, b float64) { |
| 424 |
r = linearize(col.R) |
| 425 |
g = linearize(col.G) |
| 426 |
b = linearize(col.B) |
| 427 |
return |
| 428 |
} |
| 429 |
|
| 430 |
// A much faster and still quite precise linearization using a 6th-order Taylor approximation. |
| 431 |
// See the accompanying Jupyter notebook for derivation of the constants. |
| 432 |
func linearize_fast(v float64) float64 { |
| 433 |
v1 := v - 0.5 |
| 434 |
v2 := v1 * v1 |
| 435 |
v3 := v2 * v1 |
| 436 |
v4 := v2 * v2 |
| 437 |
// v5 := v3*v2 |
| 438 |
return -0.248750514614486 + 0.925583310193438*v + 1.16740237321695*v2 + 0.280457026598666*v3 - 0.0757991963780179*v4 //+ 0.0437040411548932*v5 |
| 439 |
} |
| 440 |
|
| 441 |
// FastLinearRgb is much faster than and almost as accurate as LinearRgb. |
| 442 |
// BUT it is important to NOTE that they only produce good results for valid colors r,g,b in [0,1]. |
| 443 |
func (col Color) FastLinearRgb() (r, g, b float64) { |
| 444 |
r = linearize_fast(col.R) |
| 445 |
g = linearize_fast(col.G) |
| 446 |
b = linearize_fast(col.B) |
| 447 |
return |
| 448 |
} |
| 449 |
|
| 450 |
func delinearize(v float64) float64 { |
| 451 |
if v <= 0.0031308 { |
| 452 |
return 12.92 * v |
| 453 |
} |
| 454 |
return 1.055*math.Pow(v, 1.0/2.4) - 0.055 |
| 455 |
} |
| 456 |
|
| 457 |
// LinearRgb creates an sRGB color out of the given linear RGB color (see http://www.sjbrown.co.uk/2004/05/14/gamma-correct-rendering/). |
| 458 |
func LinearRgb(r, g, b float64) Color { |
| 459 |
return Color{delinearize(r), delinearize(g), delinearize(b)} |
| 460 |
} |
| 461 |
|
| 462 |
func delinearize_fast(v float64) float64 { |
| 463 |
// This function (fractional root) is much harder to linearize, so we need to split. |
| 464 |
if v > 0.2 { |
| 465 |
v1 := v - 0.6 |
| 466 |
v2 := v1 * v1 |
| 467 |
v3 := v2 * v1 |
| 468 |
v4 := v2 * v2 |
| 469 |
v5 := v3 * v2 |
| 470 |
return 0.442430344268235 + 0.592178981271708*v - 0.287864782562636*v2 + 0.253214392068985*v3 - 0.272557158129811*v4 + 0.325554383321718*v5 |
| 471 |
} else if v > 0.03 { |
| 472 |
v1 := v - 0.115 |
| 473 |
v2 := v1 * v1 |
| 474 |
v3 := v2 * v1 |
| 475 |
v4 := v2 * v2 |
| 476 |
v5 := v3 * v2 |
| 477 |
return 0.194915592891669 + 1.55227076330229*v - 3.93691860257828*v2 + 18.0679839248761*v3 - 101.468750302746*v4 + 632.341487393927*v5 |
| 478 |
} else { |
| 479 |
v1 := v - 0.015 |
| 480 |
v2 := v1 * v1 |
| 481 |
v3 := v2 * v1 |
| 482 |
v4 := v2 * v2 |
| 483 |
v5 := v3 * v2 |
| 484 |
// You can clearly see from the involved constants that the low-end is highly nonlinear. |
| 485 |
return 0.0519565234928877 + 5.09316778537561*v - 99.0338180489702*v2 + 3484.52322764895*v3 - 150028.083412663*v4 + 7168008.42971613*v5 |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
// FastLinearRgb is much faster than and almost as accurate as LinearRgb. |
| 490 |
// BUT it is important to NOTE that they only produce good results for valid inputs r,g,b in [0,1]. |
| 491 |
func FastLinearRgb(r, g, b float64) Color { |
| 492 |
return Color{delinearize_fast(r), delinearize_fast(g), delinearize_fast(b)} |
| 493 |
} |
| 494 |
|
| 495 |
// XyzToLinearRgb converts from CIE XYZ-space to Linear RGB space. |
| 496 |
func XyzToLinearRgb(x, y, z float64) (r, g, b float64) { |
| 497 |
r = 3.2409699419045214*x - 1.5373831775700935*y - 0.49861076029300328*z |
| 498 |
g = -0.96924363628087983*x + 1.8759675015077207*y + 0.041555057407175613*z |
| 499 |
b = 0.055630079696993609*x - 0.20397695888897657*y + 1.0569715142428786*z |
| 500 |
return |
| 501 |
} |
| 502 |
|
| 503 |
func LinearRgbToXyz(r, g, b float64) (x, y, z float64) { |
| 504 |
x = 0.41239079926595948*r + 0.35758433938387796*g + 0.18048078840183429*b |
| 505 |
y = 0.21263900587151036*r + 0.71516867876775593*g + 0.072192315360733715*b |
| 506 |
z = 0.019330818715591851*r + 0.11919477979462599*g + 0.95053215224966058*b |
| 507 |
return |
| 508 |
} |
| 509 |
|
| 510 |
// BlendLinearRgb blends two colors in the Linear RGB color-space. |
| 511 |
// Unlike BlendRgb, this will not produce dark color around the center. |
| 512 |
// t == 0 results in c1, t == 1 results in c2 |
| 513 |
func (c1 Color) BlendLinearRgb(c2 Color, t float64) Color { |
| 514 |
r1, g1, b1 := c1.LinearRgb() |
| 515 |
r2, g2, b2 := c2.LinearRgb() |
| 516 |
return LinearRgb( |
| 517 |
r1+t*(r2-r1), |
| 518 |
g1+t*(g2-g1), |
| 519 |
b1+t*(b2-b1), |
| 520 |
) |
| 521 |
} |
| 522 |
|
| 523 |
/// XYZ /// |
| 524 |
/////////// |
| 525 |
// http://www.sjbrown.co.uk/2004/05/14/gamma-correct-rendering/ |
| 526 |
|
| 527 |
func (col Color) Xyz() (x, y, z float64) { |
| 528 |
return LinearRgbToXyz(col.LinearRgb()) |
| 529 |
} |
| 530 |
|
| 531 |
func Xyz(x, y, z float64) Color { |
| 532 |
return LinearRgb(XyzToLinearRgb(x, y, z)) |
| 533 |
} |
| 534 |
|
| 535 |
/// xyY /// |
| 536 |
/////////// |
| 537 |
// http://www.brucelindbloom.com/Eqn_XYZ_to_xyY.html |
| 538 |
|
| 539 |
// Well, the name is bad, since it's xyY but Golang needs me to start with a |
| 540 |
// capital letter to make the method public. |
| 541 |
func XyzToXyy(X, Y, Z float64) (x, y, Yout float64) { |
| 542 |
return XyzToXyyWhiteRef(X, Y, Z, D65) |
| 543 |
} |
| 544 |
|
| 545 |
func XyzToXyyWhiteRef(X, Y, Z float64, wref [3]float64) (x, y, Yout float64) { |
| 546 |
Yout = Y |
| 547 |
N := X + Y + Z |
| 548 |
if math.Abs(N) < 1e-14 { |
| 549 |
// When we have black, Bruce Lindbloom recommends to use |
| 550 |
// the reference white's chromacity for x and y. |
| 551 |
x = wref[0] / (wref[0] + wref[1] + wref[2]) |
| 552 |
y = wref[1] / (wref[0] + wref[1] + wref[2]) |
| 553 |
} else { |
| 554 |
x = X / N |
| 555 |
y = Y / N |
| 556 |
} |
| 557 |
return |
| 558 |
} |
| 559 |
|
| 560 |
func XyyToXyz(x, y, Y float64) (X, Yout, Z float64) { |
| 561 |
Yout = Y |
| 562 |
|
| 563 |
if -1e-14 < y && y < 1e-14 { |
| 564 |
X = 0.0 |
| 565 |
Z = 0.0 |
| 566 |
} else { |
| 567 |
X = Y / y * x |
| 568 |
Z = Y / y * (1.0 - x - y) |
| 569 |
} |
| 570 |
|
| 571 |
return |
| 572 |
} |
| 573 |
|
| 574 |
// Converts the given color to CIE xyY space using D65 as reference white. |
| 575 |
// (Note that the reference white is only used for black input.) |
| 576 |
// x, y and Y are in [0..1] |
| 577 |
func (col Color) Xyy() (x, y, Y float64) { |
| 578 |
return XyzToXyy(col.Xyz()) |
| 579 |
} |
| 580 |
|
| 581 |
// Converts the given color to CIE xyY space, taking into account |
| 582 |
// a given reference white. (i.e. the monitor's white) |
| 583 |
// (Note that the reference white is only used for black input.) |
| 584 |
// x, y and Y are in [0..1] |
| 585 |
func (col Color) XyyWhiteRef(wref [3]float64) (x, y, Y float64) { |
| 586 |
X, Y2, Z := col.Xyz() |
| 587 |
return XyzToXyyWhiteRef(X, Y2, Z, wref) |
| 588 |
} |
| 589 |
|
| 590 |
// Generates a color by using data given in CIE xyY space. |
| 591 |
// x, y and Y are in [0..1] |
| 592 |
func Xyy(x, y, Y float64) Color { |
| 593 |
return Xyz(XyyToXyz(x, y, Y)) |
| 594 |
} |
| 595 |
|
| 596 |
/// L*a*b* /// |
| 597 |
////////////// |
| 598 |
// http://en.wikipedia.org/wiki/Lab_color_space#CIELAB-CIEXYZ_conversions |
| 599 |
// For L*a*b*, we need to L*a*b*<->XYZ->RGB and the first one is device dependent. |
| 600 |
|
| 601 |
func lab_f(t float64) float64 { |
| 602 |
if t > 6.0/29.0*6.0/29.0*6.0/29.0 { |
| 603 |
return math.Cbrt(t) |
| 604 |
} |
| 605 |
return t/3.0*29.0/6.0*29.0/6.0 + 4.0/29.0 |
| 606 |
} |
| 607 |
|
| 608 |
func XyzToLab(x, y, z float64) (l, a, b float64) { |
| 609 |
// Use D65 white as reference point by default. |
| 610 |
// http://www.fredmiranda.com/forum/topic/1035332 |
| 611 |
// http://en.wikipedia.org/wiki/Standard_illuminant |
| 612 |
return XyzToLabWhiteRef(x, y, z, D65) |
| 613 |
} |
| 614 |
|
| 615 |
func XyzToLabWhiteRef(x, y, z float64, wref [3]float64) (l, a, b float64) { |
| 616 |
fy := lab_f(y / wref[1]) |
| 617 |
l = 1.16*fy - 0.16 |
| 618 |
a = 5.0 * (lab_f(x/wref[0]) - fy) |
| 619 |
b = 2.0 * (fy - lab_f(z/wref[2])) |
| 620 |
return |
| 621 |
} |
| 622 |
|
| 623 |
func lab_finv(t float64) float64 { |
| 624 |
if t > 6.0/29.0 { |
| 625 |
return t * t * t |
| 626 |
} |
| 627 |
return 3.0 * 6.0 / 29.0 * 6.0 / 29.0 * (t - 4.0/29.0) |
| 628 |
} |
| 629 |
|
| 630 |
func LabToXyz(l, a, b float64) (x, y, z float64) { |
| 631 |
// D65 white (see above). |
| 632 |
return LabToXyzWhiteRef(l, a, b, D65) |
| 633 |
} |
| 634 |
|
| 635 |
func LabToXyzWhiteRef(l, a, b float64, wref [3]float64) (x, y, z float64) { |
| 636 |
l2 := (l + 0.16) / 1.16 |
| 637 |
x = wref[0] * lab_finv(l2+a/5.0) |
| 638 |
y = wref[1] * lab_finv(l2) |
| 639 |
z = wref[2] * lab_finv(l2-b/2.0) |
| 640 |
return |
| 641 |
} |
| 642 |
|
| 643 |
// Converts the given color to CIE L*a*b* space using D65 as reference white. |
| 644 |
func (col Color) Lab() (l, a, b float64) { |
| 645 |
return XyzToLab(col.Xyz()) |
| 646 |
} |
| 647 |
|
| 648 |
// Converts the given color to CIE L*a*b* space, taking into account |
| 649 |
// a given reference white. (i.e. the monitor's white) |
| 650 |
func (col Color) LabWhiteRef(wref [3]float64) (l, a, b float64) { |
| 651 |
x, y, z := col.Xyz() |
| 652 |
return XyzToLabWhiteRef(x, y, z, wref) |
| 653 |
} |
| 654 |
|
| 655 |
// Generates a color by using data given in CIE L*a*b* space using D65 as reference white. |
| 656 |
// WARNING: many combinations of `l`, `a`, and `b` values do not have corresponding |
| 657 |
// valid RGB values, check the FAQ in the README if you're unsure. |
| 658 |
func Lab(l, a, b float64) Color { |
| 659 |
return Xyz(LabToXyz(l, a, b)) |
| 660 |
} |
| 661 |
|
| 662 |
// Generates a color by using data given in CIE L*a*b* space, taking |
| 663 |
// into account a given reference white. (i.e. the monitor's white) |
| 664 |
func LabWhiteRef(l, a, b float64, wref [3]float64) Color { |
| 665 |
return Xyz(LabToXyzWhiteRef(l, a, b, wref)) |
| 666 |
} |
| 667 |
|
| 668 |
// DistanceLab is a good measure of visual similarity between two colors! |
| 669 |
// A result of 0 would mean identical colors, while a result of 1 or higher |
| 670 |
// means the colors differ a lot. |
| 671 |
func (c1 Color) DistanceLab(c2 Color) float64 { |
| 672 |
l1, a1, b1 := c1.Lab() |
| 673 |
l2, a2, b2 := c2.Lab() |
| 674 |
return math.Sqrt(sq(l1-l2) + sq(a1-a2) + sq(b1-b2)) |
| 675 |
} |
| 676 |
|
| 677 |
// DistanceCIE76 is the same as DistanceLab. |
| 678 |
func (c1 Color) DistanceCIE76(c2 Color) float64 { |
| 679 |
return c1.DistanceLab(c2) |
| 680 |
} |
| 681 |
|
| 682 |
// Uses the CIE94 formula to calculate color distance. More accurate than |
| 683 |
// DistanceLab, but also more work. |
| 684 |
func (cl Color) DistanceCIE94(cr Color) float64 { |
| 685 |
l1, a1, b1 := cl.Lab() |
| 686 |
l2, a2, b2 := cr.Lab() |
| 687 |
|
| 688 |
// NOTE: Since all those formulas expect L,a,b values 100x larger than we |
| 689 |
// have them in this library, we either need to adjust all constants |
| 690 |
// in the formula, or convert the ranges of L,a,b before, and then |
| 691 |
// scale the distances down again. The latter is less error-prone. |
| 692 |
l1, a1, b1 = l1*100.0, a1*100.0, b1*100.0 |
| 693 |
l2, a2, b2 = l2*100.0, a2*100.0, b2*100.0 |
| 694 |
|
| 695 |
kl := 1.0 // 2.0 for textiles |
| 696 |
kc := 1.0 |
| 697 |
kh := 1.0 |
| 698 |
k1 := 0.045 // 0.048 for textiles |
| 699 |
k2 := 0.015 // 0.014 for textiles. |
| 700 |
|
| 701 |
deltaL := l1 - l2 |
| 702 |
c1 := math.Sqrt(sq(a1) + sq(b1)) |
| 703 |
c2 := math.Sqrt(sq(a2) + sq(b2)) |
| 704 |
deltaCab := c1 - c2 |
| 705 |
|
| 706 |
// Not taking Sqrt here for stability, and it's unnecessary. |
| 707 |
deltaHab2 := sq(a1-a2) + sq(b1-b2) - sq(deltaCab) |
| 708 |
sl := 1.0 |
| 709 |
sc := 1.0 + k1*c1 |
| 710 |
sh := 1.0 + k2*c1 |
| 711 |
|
| 712 |
vL2 := sq(deltaL / (kl * sl)) |
| 713 |
vC2 := sq(deltaCab / (kc * sc)) |
| 714 |
vH2 := deltaHab2 / sq(kh*sh) |
| 715 |
|
| 716 |
return math.Sqrt(vL2+vC2+vH2) * 0.01 // See above. |
| 717 |
} |
| 718 |
|
| 719 |
// DistanceCIEDE2000 uses the Delta E 2000 formula to calculate color |
| 720 |
// distance. It is more expensive but more accurate than both DistanceLab |
| 721 |
// and DistanceCIE94. |
| 722 |
func (cl Color) DistanceCIEDE2000(cr Color) float64 { |
| 723 |
return cl.DistanceCIEDE2000klch(cr, 1.0, 1.0, 1.0) |
| 724 |
} |
| 725 |
|
| 726 |
// DistanceCIEDE2000klch uses the Delta E 2000 formula with custom values |
| 727 |
// for the weighting factors kL, kC, and kH. |
| 728 |
func (cl Color) DistanceCIEDE2000klch(cr Color, kl, kc, kh float64) float64 { |
| 729 |
l1, a1, b1 := cl.Lab() |
| 730 |
l2, a2, b2 := cr.Lab() |
| 731 |
|
| 732 |
// As with CIE94, we scale up the ranges of L,a,b beforehand and scale |
| 733 |
// them down again afterwards. |
| 734 |
l1, a1, b1 = l1*100.0, a1*100.0, b1*100.0 |
| 735 |
l2, a2, b2 = l2*100.0, a2*100.0, b2*100.0 |
| 736 |
|
| 737 |
cab1 := math.Sqrt(sq(a1) + sq(b1)) |
| 738 |
cab2 := math.Sqrt(sq(a2) + sq(b2)) |
| 739 |
cabmean := (cab1 + cab2) / 2 |
| 740 |
|
| 741 |
g := 0.5 * (1 - math.Sqrt(math.Pow(cabmean, 7)/(math.Pow(cabmean, 7)+math.Pow(25, 7)))) |
| 742 |
ap1 := (1 + g) * a1 |
| 743 |
ap2 := (1 + g) * a2 |
| 744 |
cp1 := math.Sqrt(sq(ap1) + sq(b1)) |
| 745 |
cp2 := math.Sqrt(sq(ap2) + sq(b2)) |
| 746 |
|
| 747 |
hp1 := 0.0 |
| 748 |
if b1 != ap1 || ap1 != 0 { |
| 749 |
hp1 = math.Atan2(b1, ap1) |
| 750 |
if hp1 < 0 { |
| 751 |
hp1 += math.Pi * 2 |
| 752 |
} |
| 753 |
hp1 *= 180 / math.Pi |
| 754 |
} |
| 755 |
hp2 := 0.0 |
| 756 |
if b2 != ap2 || ap2 != 0 { |
| 757 |
hp2 = math.Atan2(b2, ap2) |
| 758 |
if hp2 < 0 { |
| 759 |
hp2 += math.Pi * 2 |
| 760 |
} |
| 761 |
hp2 *= 180 / math.Pi |
| 762 |
} |
| 763 |
|
| 764 |
deltaLp := l2 - l1 |
| 765 |
deltaCp := cp2 - cp1 |
| 766 |
dhp := 0.0 |
| 767 |
cpProduct := cp1 * cp2 |
| 768 |
if cpProduct != 0 { |
| 769 |
dhp = hp2 - hp1 |
| 770 |
if dhp > 180 { |
| 771 |
dhp -= 360 |
| 772 |
} else if dhp < -180 { |
| 773 |
dhp += 360 |
| 774 |
} |
| 775 |
} |
| 776 |
deltaHp := 2 * math.Sqrt(cpProduct) * math.Sin(dhp/2*math.Pi/180) |
| 777 |
|
| 778 |
lpmean := (l1 + l2) / 2 |
| 779 |
cpmean := (cp1 + cp2) / 2 |
| 780 |
hpmean := hp1 + hp2 |
| 781 |
if cpProduct != 0 { |
| 782 |
hpmean /= 2 |
| 783 |
if math.Abs(hp1-hp2) > 180 { |
| 784 |
if hp1+hp2 < 360 { |
| 785 |
hpmean += 180 |
| 786 |
} else { |
| 787 |
hpmean -= 180 |
| 788 |
} |
| 789 |
} |
| 790 |
} |
| 791 |
|
| 792 |
t := 1 - 0.17*math.Cos((hpmean-30)*math.Pi/180) + 0.24*math.Cos(2*hpmean*math.Pi/180) + 0.32*math.Cos((3*hpmean+6)*math.Pi/180) - 0.2*math.Cos((4*hpmean-63)*math.Pi/180) |
| 793 |
deltaTheta := 30 * math.Exp(-sq((hpmean-275)/25)) |
| 794 |
rc := 2 * math.Sqrt(math.Pow(cpmean, 7)/(math.Pow(cpmean, 7)+math.Pow(25, 7))) |
| 795 |
sl := 1 + (0.015*sq(lpmean-50))/math.Sqrt(20+sq(lpmean-50)) |
| 796 |
sc := 1 + 0.045*cpmean |
| 797 |
sh := 1 + 0.015*cpmean*t |
| 798 |
rt := -math.Sin(2*deltaTheta*math.Pi/180) * rc |
| 799 |
|
| 800 |
return math.Sqrt(sq(deltaLp/(kl*sl))+sq(deltaCp/(kc*sc))+sq(deltaHp/(kh*sh))+rt*(deltaCp/(kc*sc))*(deltaHp/(kh*sh))) * 0.01 |
| 801 |
} |
| 802 |
|
| 803 |
// BlendLab blends two colors in the L*a*b* color-space, which should result in a smoother blend. |
| 804 |
// t == 0 results in c1, t == 1 results in c2 |
| 805 |
func (c1 Color) BlendLab(c2 Color, t float64) Color { |
| 806 |
l1, a1, b1 := c1.Lab() |
| 807 |
l2, a2, b2 := c2.Lab() |
| 808 |
return Lab(l1+t*(l2-l1), |
| 809 |
a1+t*(a2-a1), |
| 810 |
b1+t*(b2-b1)) |
| 811 |
} |
| 812 |
|
| 813 |
/// L*u*v* /// |
| 814 |
////////////// |
| 815 |
// http://en.wikipedia.org/wiki/CIELUV#XYZ_.E2.86.92_CIELUV_and_CIELUV_.E2.86.92_XYZ_conversions |
| 816 |
// For L*u*v*, we need to L*u*v*<->XYZ<->RGB and the first one is device dependent. |
| 817 |
|
| 818 |
func XyzToLuv(x, y, z float64) (l, a, b float64) { |
| 819 |
// Use D65 white as reference point by default. |
| 820 |
// http://www.fredmiranda.com/forum/topic/1035332 |
| 821 |
// http://en.wikipedia.org/wiki/Standard_illuminant |
| 822 |
return XyzToLuvWhiteRef(x, y, z, D65) |
| 823 |
} |
| 824 |
|
| 825 |
func XyzToLuvWhiteRef(x, y, z float64, wref [3]float64) (l, u, v float64) { |
| 826 |
if y/wref[1] <= 6.0/29.0*6.0/29.0*6.0/29.0 { |
| 827 |
l = y / wref[1] * (29.0 / 3.0 * 29.0 / 3.0 * 29.0 / 3.0) / 100.0 |
| 828 |
} else { |
| 829 |
l = 1.16*math.Cbrt(y/wref[1]) - 0.16 |
| 830 |
} |
| 831 |
ubis, vbis := xyz_to_uv(x, y, z) |
| 832 |
un, vn := xyz_to_uv(wref[0], wref[1], wref[2]) |
| 833 |
u = 13.0 * l * (ubis - un) |
| 834 |
v = 13.0 * l * (vbis - vn) |
| 835 |
return |
| 836 |
} |
| 837 |
|
| 838 |
// For this part, we do as R's graphics.hcl does, not as wikipedia does. |
| 839 |
// Or is it the same? |
| 840 |
func xyz_to_uv(x, y, z float64) (u, v float64) { |
| 841 |
denom := x + 15.0*y + 3.0*z |
| 842 |
if denom == 0.0 { |
| 843 |
u, v = 0.0, 0.0 |
| 844 |
} else { |
| 845 |
u = 4.0 * x / denom |
| 846 |
v = 9.0 * y / denom |
| 847 |
} |
| 848 |
return |
| 849 |
} |
| 850 |
|
| 851 |
func LuvToXyz(l, u, v float64) (x, y, z float64) { |
| 852 |
// D65 white (see above). |
| 853 |
return LuvToXyzWhiteRef(l, u, v, D65) |
| 854 |
} |
| 855 |
|
| 856 |
func LuvToXyzWhiteRef(l, u, v float64, wref [3]float64) (x, y, z float64) { |
| 857 |
// y = wref[1] * lab_finv((l + 0.16) / 1.16) |
| 858 |
if l <= 0.08 { |
| 859 |
y = wref[1] * l * 100.0 * 3.0 / 29.0 * 3.0 / 29.0 * 3.0 / 29.0 |
| 860 |
} else { |
| 861 |
y = wref[1] * cub((l+0.16)/1.16) |
| 862 |
} |
| 863 |
un, vn := xyz_to_uv(wref[0], wref[1], wref[2]) |
| 864 |
if l != 0.0 { |
| 865 |
ubis := u/(13.0*l) + un |
| 866 |
vbis := v/(13.0*l) + vn |
| 867 |
x = y * 9.0 * ubis / (4.0 * vbis) |
| 868 |
z = y * (12.0 - 3.0*ubis - 20.0*vbis) / (4.0 * vbis) |
| 869 |
} else { |
| 870 |
x, y = 0.0, 0.0 |
| 871 |
} |
| 872 |
return |
| 873 |
} |
| 874 |
|
| 875 |
// Converts the given color to CIE L*u*v* space using D65 as reference white. |
| 876 |
// L* is in [0..1] and both u* and v* are in about [-1..1] |
| 877 |
func (col Color) Luv() (l, u, v float64) { |
| 878 |
return XyzToLuv(col.Xyz()) |
| 879 |
} |
| 880 |
|
| 881 |
// Converts the given color to CIE L*u*v* space, taking into account |
| 882 |
// a given reference white. (i.e. the monitor's white) |
| 883 |
// L* is in [0..1] and both u* and v* are in about [-1..1] |
| 884 |
func (col Color) LuvWhiteRef(wref [3]float64) (l, u, v float64) { |
| 885 |
x, y, z := col.Xyz() |
| 886 |
return XyzToLuvWhiteRef(x, y, z, wref) |
| 887 |
} |
| 888 |
|
| 889 |
// Generates a color by using data given in CIE L*u*v* space using D65 as reference white. |
| 890 |
// L* is in [0..1] and both u* and v* are in about [-1..1] |
| 891 |
// WARNING: many combinations of `l`, `u`, and `v` values do not have corresponding |
| 892 |
// valid RGB values, check the FAQ in the README if you're unsure. |
| 893 |
func Luv(l, u, v float64) Color { |
| 894 |
return Xyz(LuvToXyz(l, u, v)) |
| 895 |
} |
| 896 |
|
| 897 |
// Generates a color by using data given in CIE L*u*v* space, taking |
| 898 |
// into account a given reference white. (i.e. the monitor's white) |
| 899 |
// L* is in [0..1] and both u* and v* are in about [-1..1] |
| 900 |
func LuvWhiteRef(l, u, v float64, wref [3]float64) Color { |
| 901 |
return Xyz(LuvToXyzWhiteRef(l, u, v, wref)) |
| 902 |
} |
| 903 |
|
| 904 |
// DistanceLuv is a good measure of visual similarity between two colors! |
| 905 |
// A result of 0 would mean identical colors, while a result of 1 or higher |
| 906 |
// means the colors differ a lot. |
| 907 |
func (c1 Color) DistanceLuv(c2 Color) float64 { |
| 908 |
l1, u1, v1 := c1.Luv() |
| 909 |
l2, u2, v2 := c2.Luv() |
| 910 |
return math.Sqrt(sq(l1-l2) + sq(u1-u2) + sq(v1-v2)) |
| 911 |
} |
| 912 |
|
| 913 |
// BlendLuv blends two colors in the CIE-L*u*v* color-space, which should result in a smoother blend. |
| 914 |
// t == 0 results in c1, t == 1 results in c2 |
| 915 |
func (c1 Color) BlendLuv(c2 Color, t float64) Color { |
| 916 |
l1, u1, v1 := c1.Luv() |
| 917 |
l2, u2, v2 := c2.Luv() |
| 918 |
return Luv(l1+t*(l2-l1), |
| 919 |
u1+t*(u2-u1), |
| 920 |
v1+t*(v2-v1)) |
| 921 |
} |
| 922 |
|
| 923 |
/// HCL /// |
| 924 |
/////////// |
| 925 |
// HCL is nothing else than L*a*b* in cylindrical coordinates! |
| 926 |
// (this was wrong on English wikipedia, I fixed it, let's hope the fix stays.) |
| 927 |
// But it is widely popular since it is a "correct HSV" |
| 928 |
// http://www.hunterlab.com/appnotes/an09_96a.pdf |
| 929 |
|
| 930 |
// Converts the given color to HCL space using D65 as reference white. |
| 931 |
// H values are in [0..360], C and L values are in [0..1] although C can overshoot 1.0 |
| 932 |
func (col Color) Hcl() (h, c, l float64) { |
| 933 |
return col.HclWhiteRef(D65) |
| 934 |
} |
| 935 |
|
| 936 |
func LabToHcl(L, a, b float64) (h, c, l float64) { |
| 937 |
// Oops, floating point workaround necessary if a ~= b and both are very small (i.e. almost zero). |
| 938 |
if math.Abs(b-a) > 1e-4 && math.Abs(a) > 1e-4 { |
| 939 |
h = math.Mod(57.29577951308232087721*math.Atan2(b, a)+360.0, 360.0) // Rad2Deg |
| 940 |
} else { |
| 941 |
h = 0.0 |
| 942 |
} |
| 943 |
c = math.Sqrt(sq(a) + sq(b)) |
| 944 |
l = L |
| 945 |
return |
| 946 |
} |
| 947 |
|
| 948 |
// Converts the given color to HCL space, taking into account |
| 949 |
// a given reference white. (i.e. the monitor's white) |
| 950 |
// H values are in [0..360], C and L values are in [0..1] |
| 951 |
func (col Color) HclWhiteRef(wref [3]float64) (h, c, l float64) { |
| 952 |
L, a, b := col.LabWhiteRef(wref) |
| 953 |
return LabToHcl(L, a, b) |
| 954 |
} |
| 955 |
|
| 956 |
// Generates a color by using data given in HCL space using D65 as reference white. |
| 957 |
// H values are in [0..360], C and L values are in [0..1] |
| 958 |
// WARNING: many combinations of `h`, `c`, and `l` values do not have corresponding |
| 959 |
// valid RGB values, check the FAQ in the README if you're unsure. |
| 960 |
func Hcl(h, c, l float64) Color { |
| 961 |
return HclWhiteRef(h, c, l, D65) |
| 962 |
} |
| 963 |
|
| 964 |
func HclToLab(h, c, l float64) (L, a, b float64) { |
| 965 |
H := 0.01745329251994329576 * h // Deg2Rad |
| 966 |
a = c * math.Cos(H) |
| 967 |
b = c * math.Sin(H) |
| 968 |
L = l |
| 969 |
return |
| 970 |
} |
| 971 |
|
| 972 |
// Generates a color by using data given in HCL space, taking |
| 973 |
// into account a given reference white. (i.e. the monitor's white) |
| 974 |
// H values are in [0..360], C and L values are in [0..1] |
| 975 |
func HclWhiteRef(h, c, l float64, wref [3]float64) Color { |
| 976 |
L, a, b := HclToLab(h, c, l) |
| 977 |
return LabWhiteRef(L, a, b, wref) |
| 978 |
} |
| 979 |
|
| 980 |
// BlendHcl blends two colors in the CIE-L*C*h° color-space, which should result in a smoother blend. |
| 981 |
// t == 0 results in c1, t == 1 results in c2 |
| 982 |
func (col1 Color) BlendHcl(col2 Color, t float64) Color { |
| 983 |
h1, c1, l1 := col1.Hcl() |
| 984 |
h2, c2, l2 := col2.Hcl() |
| 985 |
|
| 986 |
// https://github.com/lucasb-eyer/go-colorful/pull/60 |
| 987 |
if c1 <= 0.00015 && c2 >= 0.00015 { |
| 988 |
h1 = h2 |
| 989 |
} else if c2 <= 0.00015 && c1 >= 0.00015 { |
| 990 |
h2 = h1 |
| 991 |
} |
| 992 |
|
| 993 |
// We know that h are both in [0..360] |
| 994 |
return Hcl(interp_angle(h1, h2, t), c1+t*(c2-c1), l1+t*(l2-l1)).Clamped() |
| 995 |
} |
| 996 |
|
| 997 |
// LuvLch |
| 998 |
|
| 999 |
// Converts the given color to LuvLCh space using D65 as reference white. |
| 1000 |
// h values are in [0..360], C and L values are in [0..1] although C can overshoot 1.0 |
| 1001 |
func (col Color) LuvLCh() (l, c, h float64) { |
| 1002 |
return col.LuvLChWhiteRef(D65) |
| 1003 |
} |
| 1004 |
|
| 1005 |
func LuvToLuvLCh(L, u, v float64) (l, c, h float64) { |
| 1006 |
// Oops, floating point workaround necessary if u ~= v and both are very small (i.e. almost zero). |
| 1007 |
if math.Abs(v-u) > 1e-4 && math.Abs(u) > 1e-4 { |
| 1008 |
h = math.Mod(57.29577951308232087721*math.Atan2(v, u)+360.0, 360.0) // Rad2Deg |
| 1009 |
} else { |
| 1010 |
h = 0.0 |
| 1011 |
} |
| 1012 |
l = L |
| 1013 |
c = math.Sqrt(sq(u) + sq(v)) |
| 1014 |
return |
| 1015 |
} |
| 1016 |
|
| 1017 |
// Converts the given color to LuvLCh space, taking into account |
| 1018 |
// a given reference white. (i.e. the monitor's white) |
| 1019 |
// h values are in [0..360], c and l values are in [0..1] |
| 1020 |
func (col Color) LuvLChWhiteRef(wref [3]float64) (l, c, h float64) { |
| 1021 |
return LuvToLuvLCh(col.LuvWhiteRef(wref)) |
| 1022 |
} |
| 1023 |
|
| 1024 |
// Generates a color by using data given in LuvLCh space using D65 as reference white. |
| 1025 |
// h values are in [0..360], C and L values are in [0..1] |
| 1026 |
// WARNING: many combinations of `l`, `c`, and `h` values do not have corresponding |
| 1027 |
// valid RGB values, check the FAQ in the README if you're unsure. |
| 1028 |
func LuvLCh(l, c, h float64) Color { |
| 1029 |
return LuvLChWhiteRef(l, c, h, D65) |
| 1030 |
} |
| 1031 |
|
| 1032 |
func LuvLChToLuv(l, c, h float64) (L, u, v float64) { |
| 1033 |
H := 0.01745329251994329576 * h // Deg2Rad |
| 1034 |
u = c * math.Cos(H) |
| 1035 |
v = c * math.Sin(H) |
| 1036 |
L = l |
| 1037 |
return |
| 1038 |
} |
| 1039 |
|
| 1040 |
// Generates a color by using data given in LuvLCh space, taking |
| 1041 |
// into account a given reference white. (i.e. the monitor's white) |
| 1042 |
// h values are in [0..360], C and L values are in [0..1] |
| 1043 |
func LuvLChWhiteRef(l, c, h float64, wref [3]float64) Color { |
| 1044 |
L, u, v := LuvLChToLuv(l, c, h) |
| 1045 |
return LuvWhiteRef(L, u, v, wref) |
| 1046 |
} |
| 1047 |
|
| 1048 |
// BlendLuvLCh blends two colors in the cylindrical CIELUV color space. |
| 1049 |
// t == 0 results in c1, t == 1 results in c2 |
| 1050 |
func (col1 Color) BlendLuvLCh(col2 Color, t float64) Color { |
| 1051 |
l1, c1, h1 := col1.LuvLCh() |
| 1052 |
l2, c2, h2 := col2.LuvLCh() |
| 1053 |
|
| 1054 |
// We know that h are both in [0..360] |
| 1055 |
return LuvLCh(l1+t*(l2-l1), c1+t*(c2-c1), interp_angle(h1, h2, t)) |
| 1056 |
} |
| 1057 |
|
| 1058 |
/// OkLab /// |
| 1059 |
/////////// |
| 1060 |
|
| 1061 |
func (col Color) OkLab() (l, a, b float64) { |
| 1062 |
return XyzToOkLab(col.Xyz()) |
| 1063 |
} |
| 1064 |
|
| 1065 |
func OkLab(l, a, b float64) Color { |
| 1066 |
return Xyz(OkLabToXyz(l, a, b)) |
| 1067 |
} |
| 1068 |
|
| 1069 |
func XyzToOkLab(x, y, z float64) (l, a, b float64) { |
| 1070 |
l_ := math.Cbrt(0.8189330101*x + 0.3618667424*y - 0.1288597137*z) |
| 1071 |
m_ := math.Cbrt(0.0329845436*x + 0.9293118715*y + 0.0361456387*z) |
| 1072 |
s_ := math.Cbrt(0.0482003018*x + 0.2643662691*y + 0.6338517070*z) |
| 1073 |
l = 0.2104542553*l_ + 0.7936177850*m_ - 0.0040720468*s_ |
| 1074 |
a = 1.9779984951*l_ - 2.4285922050*m_ + 0.4505937099*s_ |
| 1075 |
b = 0.0259040371*l_ + 0.7827717662*m_ - 0.8086757660*s_ |
| 1076 |
return |
| 1077 |
} |
| 1078 |
|
| 1079 |
func OkLabToXyz(l, a, b float64) (x, y, z float64) { |
| 1080 |
l_ := 0.9999999984505196*l + 0.39633779217376774*a + 0.2158037580607588*b |
| 1081 |
m_ := 1.0000000088817607*l - 0.10556134232365633*a - 0.0638541747717059*b |
| 1082 |
s_ := 1.0000000546724108*l - 0.08948418209496574*a - 1.2914855378640917*b |
| 1083 |
|
| 1084 |
ll := math.Pow(l_, 3) |
| 1085 |
m := math.Pow(m_, 3) |
| 1086 |
s := math.Pow(s_, 3) |
| 1087 |
|
| 1088 |
x = 1.2268798733741557*ll - 0.5578149965554813*m + 0.28139105017721594*s |
| 1089 |
y = -0.04057576262431372*ll + 1.1122868293970594*m - 0.07171106666151696*s |
| 1090 |
z = -0.07637294974672142*ll - 0.4214933239627916*m + 1.5869240244272422*s |
| 1091 |
|
| 1092 |
return |
| 1093 |
} |
| 1094 |
|
| 1095 |
// BlendOkLab blends two colors in the OkLab color-space, which should result in a better blend (even compared to BlendLab). |
| 1096 |
func (c1 Color) BlendOkLab(c2 Color, t float64) Color { |
| 1097 |
l1, a1, b1 := c1.OkLab() |
| 1098 |
l2, a2, b2 := c2.OkLab() |
| 1099 |
return OkLab(l1+t*(l2-l1), |
| 1100 |
a1+t*(a2-a1), |
| 1101 |
b1+t*(b2-b1)) |
| 1102 |
} |
| 1103 |
|
| 1104 |
/// OkLch /// |
| 1105 |
/////////// |
| 1106 |
|
| 1107 |
func (col Color) OkLch() (l, c, h float64) { |
| 1108 |
return OkLabToOkLch(col.OkLab()) |
| 1109 |
} |
| 1110 |
|
| 1111 |
func OkLch(l, c, h float64) Color { |
| 1112 |
return Xyz(OkLchToXyz(l, c, h)) |
| 1113 |
} |
| 1114 |
|
| 1115 |
func XyzToOkLch(x, y, z float64) (float64, float64, float64) { |
| 1116 |
l, c, h := OkLabToOkLch(XyzToOkLab(x, y, z)) |
| 1117 |
return l, c, h |
| 1118 |
} |
| 1119 |
|
| 1120 |
func OkLchToXyz(l, c, h float64) (float64, float64, float64) { |
| 1121 |
x, y, z := OkLabToXyz(OkLchToOkLab(l, c, h)) |
| 1122 |
return x, y, z |
| 1123 |
} |
| 1124 |
|
| 1125 |
func OkLabToOkLch(l, a, b float64) (float64, float64, float64) { |
| 1126 |
c := math.Sqrt((a * a) + (b * b)) |
| 1127 |
h := math.Atan2(b, a) |
| 1128 |
if h < 0 { |
| 1129 |
h += 2 * math.Pi |
| 1130 |
} |
| 1131 |
|
| 1132 |
return l, c, h * 180 / math.Pi |
| 1133 |
} |
| 1134 |
|
| 1135 |
func OkLchToOkLab(l, c, h float64) (float64, float64, float64) { |
| 1136 |
h *= math.Pi / 180 |
| 1137 |
a := c * math.Cos(h) |
| 1138 |
b := c * math.Sin(h) |
| 1139 |
return l, a, b |
| 1140 |
} |
| 1141 |
|
| 1142 |
// BlendOkLch blends two colors in the OkLch color-space, which should result in a better blend (even compared to BlendHcl). |
| 1143 |
func (col1 Color) BlendOkLch(col2 Color, t float64) Color { |
| 1144 |
l1, c1, h1 := col1.OkLch() |
| 1145 |
l2, c2, h2 := col2.OkLch() |
| 1146 |
|
| 1147 |
// https://github.com/lucasb-eyer/go-colorful/pull/60 |
| 1148 |
if c1 <= 0.00015 && c2 >= 0.00015 { |
| 1149 |
h1 = h2 |
| 1150 |
} else if c2 <= 0.00015 && c1 >= 0.00015 { |
| 1151 |
h2 = h1 |
| 1152 |
} |
| 1153 |
|
| 1154 |
// We know that h are both in [0..360] |
| 1155 |
return OkLch(l1+t*(l2-l1), c1+t*(c2-c1), interp_angle(h1, h2, t)).Clamped() |
| 1156 |
} |