-
Notifications
You must be signed in to change notification settings - Fork 3
/
filter.go
62 lines (52 loc) · 2.03 KB
/
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
package vibrant
import (
"image/color"
"math"
)
// A Filter provides a mechanism for exercising fine-grained control over which colors
// are valid within a resulting Palette.
type Filter interface {
// Hook to allow clients to be able filter colors from resulting palette. Return true if the color is allowed, false if not.
isAllowed(c color.Color) bool
}
// Default values for the default filter.
const (
DefaultFilterMinLightness = 0.05
DefaultFilterMaxLightness = 0.95
)
// DefaultFilter removes colors close to white and black.
var DefaultFilter = NewLightnessFilter(DefaultFilterMinLightness, DefaultFilterMaxLightness)
type defaultFilter struct {
blackMaxLightness float64
whiteMinimumLightness float64
quantizedBlackMask uint16
quantizedWhiteMask uint16
}
func (f *defaultFilter) isAllowed(c color.Color) bool {
// Short circuit for quantized colors to allow for faster black/white checking.
if q, ok := c.(QuantizedColor); ok && !f.isAllowedQuantizedColor(q) {
return false
}
hsl := HSLModel.Convert(c).(HSL)
return !f.isWhite(hsl) && !f.isBlack(hsl)
}
func (f *defaultFilter) isAllowedQuantizedColor(q QuantizedColor) bool {
return !(uint16(q)|f.quantizedBlackMask == f.quantizedBlackMask || uint16(q)&f.quantizedWhiteMask == f.quantizedWhiteMask)
}
func (f *defaultFilter) isWhite(hsl HSL) bool {
return hsl.L >= f.whiteMinimumLightness
}
func (f *defaultFilter) isBlack(hsl HSL) bool {
return hsl.L <= f.blackMaxLightness
}
// NewLightnessFilter creates a Filter that removes colors based on a lightness.
func NewLightnessFilter(blackMaxLightness float64, whiteMinimumLightness float64) Filter {
quantizedBlackMask := uint16(uint8(math.Floor(float64(blackMaxLightness)*float64(255))) >> 3)
quantizedWhiteMask := uint16(uint8(math.Ceil(float64(whiteMinimumLightness)*float64(255))) >> 3)
return &defaultFilter{
blackMaxLightness,
whiteMinimumLightness,
(quantizedBlackMask << 10) | (quantizedBlackMask << 5) | quantizedBlackMask,
(quantizedWhiteMask << 10) | (quantizedWhiteMask << 5) | quantizedWhiteMask,
}
}