Skip to content

Commit 5ce6c1d

Browse files
authored
BlendOkLab + BlendOkLch (#70)
1 parent a50e695 commit 5ce6c1d

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

colors.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,15 @@ func OkLabToXyz(l, a, b float64) (x, y, z float64) {
10681068
return
10691069
}
10701070

1071+
// BlendOkLab blends two colors in the OkLab color-space, which should result in a better blend (even compared to BlendLab).
1072+
func (c1 Color) BlendOkLab(c2 Color, t float64) Color {
1073+
l1, a1, b1 := c1.OkLab()
1074+
l2, a2, b2 := c2.OkLab()
1075+
return OkLab(l1+t*(l2-l1),
1076+
a1+t*(a2-a1),
1077+
b1+t*(b2-b1))
1078+
}
1079+
10711080
/// OkLch ///
10721081
///////////
10731082

@@ -1105,3 +1114,20 @@ func OkLchToOkLab(l, c, h float64) (float64, float64, float64) {
11051114
b := c * math.Sin(h)
11061115
return l, a, b
11071116
}
1117+
1118+
// BlendOkLch blends two colors in the OkLch color-space, which should result in a better blend (even compared to BlendHcl).
1119+
func (col1 Color) BlendOkLch(col2 Color, t float64) Color {
1120+
l1, c1, h1 := col1.OkLch()
1121+
l2, c2, h2 := col2.OkLch()
1122+
1123+
// https://github.com/lucasb-eyer/go-colorful/pull/60
1124+
if c1 <= 0.00015 && c2 >= 0.00015 {
1125+
h1 = h2
1126+
} else if c2 <= 0.00015 && c1 >= 0.00015 {
1127+
h2 = h1
1128+
}
1129+
1130+
// We know that h are both in [0..360]
1131+
return OkLch(l1+t*(l2-l1), c1+t*(c2-c1), interp_angle(h1, h2, t)).Clamped()
1132+
}
1133+

colors_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,24 @@ func TestIssue11(t *testing.T) {
716716
if blend != c2hex {
717717
t.Errorf("Issue11: %v --LuvLCh-> %v = %v, want %v", c1hex, c2hex, blend, c2hex)
718718
}
719+
720+
blend = c1.BlendOkLab(c2, 0).Hex()
721+
if blend != c1hex {
722+
t.Errorf("Issue11: %v --OkLab-> %v = %v, want %v", c1hex, c2hex, blend, c1hex)
723+
}
724+
blend = c1.BlendOkLab(c2, 1).Hex()
725+
if blend != c2hex {
726+
t.Errorf("Issue11: %v --OkLab-> %v = %v, want %v", c1hex, c2hex, blend, c2hex)
727+
}
728+
729+
blend = c1.BlendOkLch(c2, 0).Hex()
730+
if blend != c1hex {
731+
t.Errorf("Issue11: %v --OkLch-> %v = %v, want %v", c1hex, c2hex, blend, c1hex)
732+
}
733+
blend = c1.BlendOkLch(c2, 1).Hex()
734+
if blend != c2hex {
735+
t.Errorf("Issue11: %v --OkLch-> %v = %v, want %v", c1hex, c2hex, blend, c2hex)
736+
}
719737
}
720738

721739
// For testing angular interpolation internal function

0 commit comments

Comments
 (0)