-
Notifications
You must be signed in to change notification settings - Fork 70
/
transform.go
37 lines (28 loc) · 989 Bytes
/
transform.go
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
// Copyright (c) 2023, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package cam16
import (
"image/color"
"cogentcore.org/core/colors/cam/cie"
"cogentcore.org/core/math32"
)
// Blend returns a color that is the given percent blend between the first
// and second color; 10 = 10% of the first and 90% of the second, etc;
// blending is done directly on non-premultiplied CAM16-UCS values, and
// a correctly premultiplied color is returned.
func Blend(pct float32, x, y color.Color) color.RGBA {
pct = math32.Clamp(pct, 0, 100)
amt := pct / 100
xsr, xsg, xsb, _ := cie.SRGBUint32ToFloat(x.RGBA())
ysr, ysg, ysb, _ := cie.SRGBUint32ToFloat(y.RGBA())
cx := FromSRGB(xsr, xsg, xsb)
cy := FromSRGB(ysr, ysg, ysb)
xj, _, xa, xb := cx.UCS()
yj, _, ya, yb := cy.UCS()
j := yj + (xj-yj)*amt
a := ya + (xa-ya)*amt
b := yb + (xb-yb)*amt
cam := FromUCS(j, a, b)
return cam.AsRGBA()
}