This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelta.go
More file actions
51 lines (43 loc) · 1.51 KB
/
delta.go
File metadata and controls
51 lines (43 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package color
import "math"
// TODO:
// 2000
// CMC
// HCT
// ITP
// EJz
// DeltaDistance computes the Euclidean distance in the provided color space.
func DeltaDistance(reference, sample Color, space *Space) float64 {
ref := reference.Convert(space)
s := sample.Convert(space)
Δ0 := ref.Values[0] - s.Values[0]
Δ1 := ref.Values[1] - s.Values[1]
Δ2 := ref.Values[2] - s.Values[2]
return math.Hypot(math.Hypot(Δ0, Δ1), Δ2)
}
// DeltaE76 computes the CIE 1976 color difference using the Euclidean distance
// in the [Lab] color space.
func DeltaE76(reference, sample Color) float64 {
return DeltaDistance(reference, sample, Lab)
}
// DeltaEOK computes the color difference using the Euclidean distance in the
// [Oklab] color space.
func DeltaEOK(reference, sample Color) float64 {
return DeltaDistance(reference, sample, Oklab)
}
// DeltaEOK2 computes the color difference using the Euclidean distance in the
// [Oklab] color space, with the a and b axes scaled by a factor of 2, for
// better uniformity.
func DeltaEOK2(reference, sample Color) float64 {
// See
// https://github.com/w3c/csswg-drafts/issues/6642#issuecomment-945714988
// and
// https://github.com/color-js/color.js/blob/40e7a059c639bafde14504627e62791588c63100/src/deltaE/deltaEOK2.js
// for background on the scaling.
ref := reference.Convert(Oklab)
s := sample.Convert(Oklab)
Δ0 := ref.Values[0] - s.Values[0]
Δ1 := 2 * (ref.Values[1] - s.Values[1])
Δ2 := 2 * (ref.Values[2] - s.Values[2])
return math.Hypot(math.Hypot(Δ0, Δ1), Δ2)
}