/
blend.go
48 lines (36 loc) · 1.25 KB
/
blend.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
38
39
40
41
42
43
44
45
46
47
48
package main
/* Functions for different blend modes
*/
import (
"github.com/lucasb-eyer/go-colorful"
)
// top layer over bottom - most common
func blendNormal(top colorful.Color, topAlpha float64, bottom colorful.Color, bottomAlpha float64) (colorful.Color, float64) {
alphaDelta := (1 - topAlpha) * bottomAlpha
alpha := alphaDelta + topAlpha
red := (alphaDelta*bottom.R + topAlpha*top.R) / alpha
green := (alphaDelta*bottom.G + topAlpha*top.G) / alpha
blue := (alphaDelta*bottom.B + topAlpha*top.B) / alpha
result := colorful.Color{R: red / 255, G: green / 255, B: blue / 255}
return result.Clamped(), alpha
}
/* color blend
* preserves the luma of the bottom
* adopts the hue and chroma of the top
*/
func blendColor(top colorful.Color, bottom colorful.Color) colorful.Color {
topHue, topChroma, _ := top.Hcl()
_, _, bottomLuma := bottom.Hcl()
result := colorful.Hcl(topHue, topChroma, bottomLuma)
return result.Clamped()
}
/* color blend
* preserves the chroma and luma of the bottom
* adopts the hue of the top
*/
func blendHue(top colorful.Color, bottom colorful.Color) colorful.Color {
topHue, _, _ := top.Hcl()
_, bottomChroma, bottomLuma := bottom.Hcl()
result := colorful.Hcl(topHue, bottomChroma, bottomLuma)
return result.Clamped()
}