Debian Codegraph @

vendor/github.com/xyproto/gfx/cie_lab.go

raw
1 //go:build !tinygo
2 // +build !tinygo
3
4 package gfx
5
6 import "math"
7
8 // CIELab represents a color in CIE-L*ab.
9 type CIELab struct {
10 L float64
11 A float64
12 B float64
13 }
14
15 // DeltaC calculates Delta C* for two CIE-L*ab colors.
16 //
17 // CIE-a*1, CIE-b*1 //Color #1 CIE-L*ab values
18 // CIE-a*2, CIE-b*2 //Color #2 CIE-L*ab values
19 //
20 // Delta C* = sqrt( ( CIE-a*2 ^ 2 ) + ( CIE-b*2 ^ 2 ) )
21 // - sqrt( ( CIE-a*1 ^ 2 ) + ( CIE-b*1 ^ 2 ) )
22 func (c1 CIELab) DeltaC(c2 CIELab) float64 {
23 return math.Sqrt(math.Pow(c2.A, 2)+math.Pow(c2.B, 2)) -
24 math.Sqrt(math.Pow(c1.A, 2)+math.Pow(c1.B, 2))
25 }
26
27 // DeltaH calculates Delta H* for two CIE-L*ab colors.
28 //
29 // CIE-a*1, CIE-b*1 //Color #1 CIE-L*ab values
30 // CIE-a*2, CIE-b*2 //Color #2 CIE-L*ab values
31 //
32 // xDE = sqrt( ( CIE-a*2 ^ 2 ) + ( CIE-b*2 ^ 2 ) )
33 // - sqrt( ( CIE-a*1 ^ 2 ) + ( CIE-b*1 ^ 2 ) )
34 //
35 // Delta H* = sqrt( ( CIE-a*2 - CIE-a*1 ) ^ 2
36 // - ( CIE-b*2 - CIE-b*1 ) ^ 2 - ( xDE ^ 2 ) )
37 func (c1 CIELab) DeltaH(c2 CIELab) float64 {
38 xDE := math.Sqrt(math.Pow(c2.A, 2)+math.Pow(c2.B, 2)) -
39 math.Sqrt(math.Pow(c1.A, 2)+math.Pow(c1.B, 2))
40
41 return math.Sqrt(math.Pow(c2.A-c1.A, 2) +
42 math.Pow(c2.B-c1.B, 2) - math.Pow(xDE, 2))
43 }
44
45 // DeltaE calculates Delta E* for two CIE-L*ab colors.
46 //
47 // CIE-L*1, CIE-a*1, CIE-b*1 //Color #1 CIE-L*ab values
48 // CIE-L*2, CIE-a*2, CIE-b*2 //Color #2 CIE-L*ab values
49 //
50 // Delta E* = sqrt( ( ( CIE-L*1 - CIE-L*2 ) ^ 2 )
51 // - ( ( CIE-a*1 - CIE-a*2 ) ^ 2 )
52 // - ( ( CIE-b*1 - CIE-b*2 ) ^ 2 ) )
53 func (c1 CIELab) DeltaE(c2 CIELab) float64 {
54 return math.Sqrt(math.Pow(c1.L*1-c2.L*2, 2) +
55 math.Pow(c1.A*1-c2.A*2, 2) + math.Pow(c1.B*1-c2.B*2, 2),
56 )
57 }
58
59 // CIELab converts from XYZ to CIE-L*ab.
60 //
61 // Reference-X, Y and Z refer to specific illuminants and observers.
62 // Common reference values are available below in this same page.
63 //
64 // var_X = X / Reference-X
65 // var_Y = Y / Reference-Y
66 // var_Z = Z / Reference-Z
67 //
68 // if ( var_X > 0.008856 ) var_X = var_X ^ ( 1/3 )
69 // else var_X = ( 7.787 * var_X ) + ( 16 / 116 )
70 // if ( var_Y > 0.008856 ) var_Y = var_Y ^ ( 1/3 )
71 // else var_Y = ( 7.787 * var_Y ) + ( 16 / 116 )
72 // if ( var_Z > 0.008856 ) var_Z = var_Z ^ ( 1/3 )
73 // else var_Z = ( 7.787 * var_Z ) + ( 16 / 116 )
74 //
75 // CIE-L* = ( 116 * var_Y ) - 16
76 // CIE-a* = 500 * ( var_X - var_Y )
77 // CIE-b* = 200 * ( var_Y - var_Z )
78 func (xyz XYZ) CIELab(ref XYZ) CIELab {
79 X := xyz.X / ref.X
80 Y := xyz.Y / ref.Y
81 Z := xyz.Z / ref.Z
82
83 if X > 0.008856 {
84 X = math.Pow(X, (1.0 / 3))
85 } else {
86 X = (7.787 * X) + (16.0 / 116)
87 }
88
89 if Y > 0.008856 {
90 Y = math.Pow(Y, (1.0 / 3))
91 } else {
92 Y = (7.787 * Y) + (16.0 / 116)
93 }
94
95 if Z > 0.008856 {
96 Z = math.Pow(Z, (1.0 / 3))
97 } else {
98 Z = (7.787 * Z) + (16.0 / 116)
99 }
100
101 return CIELab{
102 L: (116.0 * Y) - 16,
103 A: 500.0 * (X - Y),
104 B: 200.0 * (Y - Z),
105 }
106 }