/
color_filter.go
118 lines (101 loc) · 3.52 KB
/
color_filter.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright ©2021-2022 by Richard A. Wilkes. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with
// this file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, version 2.0.
package unison
import (
"runtime"
"github.com/richardwilkes/unison/enums/blendmode"
"github.com/richardwilkes/unison/enums/invertstyle"
"github.com/richardwilkes/unison/internal/skia"
)
// ColorFilter is called with source colors and return new colors, which are then passed onto the next stage.
type ColorFilter struct {
filter skia.ColorFilter
}
func newColorFilter(filter skia.ColorFilter) *ColorFilter {
if filter == nil {
return nil
}
f := &ColorFilter{filter: filter}
runtime.SetFinalizer(f, func(obj *ColorFilter) {
ReleaseOnUIThread(func() {
skia.ColorFilterUnref(obj.filter)
})
})
return f
}
func (f *ColorFilter) filterOrNil() skia.ColorFilter {
if f == nil {
return nil
}
return f.filter
}
// NewBlendColorFilter returns a new blend color filter.
func NewBlendColorFilter(color Color, blendMode blendmode.Enum) *ColorFilter {
return newColorFilter(skia.ColorFilterNewMode(skia.Color(color), skia.BlendMode(blendMode)))
}
// NewLightingColorFilter returns a new lighting color filter.
func NewLightingColorFilter(mul, add Color) *ColorFilter {
return newColorFilter(skia.ColorFilterNewLighting(skia.Color(mul), skia.Color(add)))
}
// NewComposeColorFilter returns a new color filter that combines two other color filters.
func NewComposeColorFilter(outer, inner *ColorFilter) *ColorFilter {
return newColorFilter(skia.ColorFilterNewCompose(outer.filter, inner.filter))
}
// NewMatrixColorFilter returns a new matrix color filter. array should be 20 long. If smaller, it will be filled with
// zeroes to make it 20.
func NewMatrixColorFilter(array []float32) *ColorFilter {
if len(array) < 20 {
a := make([]float32, 20)
copy(a, array)
array = a
}
return newColorFilter(skia.ColorFilterNewColorMatrix(array))
}
// NewLumaColorFilter returns a new luma color filter.
func NewLumaColorFilter() *ColorFilter {
return newColorFilter(skia.ColorFilterNewLumaColor())
}
// NewHighContrastColorFilter returns a new high contrast color filter.
func NewHighContrastColorFilter(contrast float32, style invertstyle.Enum, grayscale bool) *ColorFilter {
return newColorFilter(skia.ColorFilterNewHighContrast(&skia.HighContrastConfig{
Grayscale: grayscale,
InvertStyle: skia.InvertStyle(style),
Contrast: contrast,
}))
}
// NewAlphaFilter returns a new ColorFilter that applies an alpha blend.
func NewAlphaFilter(alpha float32) *ColorFilter {
return NewMatrixColorFilter([]float32{
1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, alpha, 0,
})
}
var grayscale30Filter *ColorFilter
// Grayscale30Filter returns a ColorFilter that transforms colors to grayscale and applies a 30% alpha blend.
func Grayscale30Filter() *ColorFilter {
if grayscale30Filter == nil {
grayscale30Filter = NewMatrixColorFilter([]float32{
0.2126, 0.7152, 0.0722, 0, 0,
0.2126, 0.7152, 0.0722, 0, 0,
0.2126, 0.7152, 0.0722, 0, 0,
0, 0, 0, 0.3, 0,
})
}
return grayscale30Filter
}
var alpha30Filter *ColorFilter
// Alpha30Filter returns a ColorFilter that transforms colors by applying a 30% alpha blend.
func Alpha30Filter() *ColorFilter {
if alpha30Filter == nil {
alpha30Filter = NewAlphaFilter(0.3)
}
return alpha30Filter
}