-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRulePalette.js
More file actions
56 lines (45 loc) · 1.23 KB
/
RulePalette.js
File metadata and controls
56 lines (45 loc) · 1.23 KB
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
import Palette from './Palette.js'
/**
* A basic rule Palette which allows you to pass
* a set of rules that will test properties of the
* pixel and decide what color it should be
*/
export default class RulePalette extends Palette {
constructor (name, rules, backgroundColor) {
super(name, backgroundColor)
this.rules = rules || []
}
getColorFromPixel (pixel) {
let color = pixel.color
this.rules.some((rule) => {
let result = rule.test(pixel)
if (result) {
color = rule.color
}
return result
})
return color
}
}
/**
* helper function to generate rule palettes based on a property,
* by default brightness
*/
export function stepColorPalette(paletteName, colors, intervals, backgroundColor, property) {
let steps = []
property = property || "brightness"
if (colors.length !== intervals.length) {
throw new Error("Colors and brightness list should be same length")
}
for (var i=0; i < colors.length; i++) {
(function (i) {
steps.push({
color: colors[i],
test: function (info) {
return info[property] < intervals[i]
}
})
})(i)
}
return new RulePalette(paletteName, steps, backgroundColor)
}