Skip to content

Commit

Permalink
BlendOkLab + BlendOkLch (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
dyuri authored Dec 18, 2024
1 parent a50e695 commit 5ce6c1d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
26 changes: 26 additions & 0 deletions colors.go
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,15 @@ func OkLabToXyz(l, a, b float64) (x, y, z float64) {
return
}

// BlendOkLab blends two colors in the OkLab color-space, which should result in a better blend (even compared to BlendLab).
func (c1 Color) BlendOkLab(c2 Color, t float64) Color {
l1, a1, b1 := c1.OkLab()
l2, a2, b2 := c2.OkLab()
return OkLab(l1+t*(l2-l1),
a1+t*(a2-a1),
b1+t*(b2-b1))
}

/// OkLch ///
///////////

Expand Down Expand Up @@ -1105,3 +1114,20 @@ func OkLchToOkLab(l, c, h float64) (float64, float64, float64) {
b := c * math.Sin(h)
return l, a, b
}

// BlendOkLch blends two colors in the OkLch color-space, which should result in a better blend (even compared to BlendHcl).
func (col1 Color) BlendOkLch(col2 Color, t float64) Color {
l1, c1, h1 := col1.OkLch()
l2, c2, h2 := col2.OkLch()

// https://github.com/lucasb-eyer/go-colorful/pull/60
if c1 <= 0.00015 && c2 >= 0.00015 {
h1 = h2
} else if c2 <= 0.00015 && c1 >= 0.00015 {
h2 = h1
}

// We know that h are both in [0..360]
return OkLch(l1+t*(l2-l1), c1+t*(c2-c1), interp_angle(h1, h2, t)).Clamped()
}

18 changes: 18 additions & 0 deletions colors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,24 @@ func TestIssue11(t *testing.T) {
if blend != c2hex {
t.Errorf("Issue11: %v --LuvLCh-> %v = %v, want %v", c1hex, c2hex, blend, c2hex)
}

blend = c1.BlendOkLab(c2, 0).Hex()
if blend != c1hex {
t.Errorf("Issue11: %v --OkLab-> %v = %v, want %v", c1hex, c2hex, blend, c1hex)
}
blend = c1.BlendOkLab(c2, 1).Hex()
if blend != c2hex {
t.Errorf("Issue11: %v --OkLab-> %v = %v, want %v", c1hex, c2hex, blend, c2hex)
}

blend = c1.BlendOkLch(c2, 0).Hex()
if blend != c1hex {
t.Errorf("Issue11: %v --OkLch-> %v = %v, want %v", c1hex, c2hex, blend, c1hex)
}
blend = c1.BlendOkLch(c2, 1).Hex()
if blend != c2hex {
t.Errorf("Issue11: %v --OkLch-> %v = %v, want %v", c1hex, c2hex, blend, c2hex)
}
}

// For testing angular interpolation internal function
Expand Down

0 comments on commit 5ce6c1d

Please sign in to comment.