-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
117 lines (104 loc) · 2.52 KB
/
helpers.js
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
const helpers = {
mirrorAndCombine (source) {
let output = createImage(source.width * 2, source.height)
output.copy(source, 0, 0, source.width, source.height, source.width, 0, source.width, source.height)
output.loadPixels()
for (let y = 0; y < output.height; y++) {
for (let x = 0; x < source.width; x++) {
let mirror = helpers.getColor(output, output.width-x-1, y)
helpers.writeColor(output, x, y, mirror.r, mirror.g, mirror.b, 255);
}
}
output.updatePixels()
return output
},
writeColor (image, x, y, red, green, blue, alpha) {
let index = (x + y * image.width) * 4;
image.pixels[index] = red;
image.pixels[index + 1] = green;
image.pixels[index + 2] = blue;
image.pixels[index + 3] = alpha;
},
getColor (image, x, y) {
let index = (x + y * image.width) * 4;
return {
r: image.pixels[index],
g: image.pixels[index + 1],
b: image.pixels[index + 2],
a: image.pixels[index + 3]
}
},
blendColors (s, b) {
let source = helpers.normalizeColor(s)
let backdrop = helpers.normalizeColor(b)
let ro = source.r * source.a + backdrop.r * backdrop.a * (1 - source.a)
let go = source.g * source.a + backdrop.g * backdrop.a * (1 - source.a)
let bo = source.b * source.a + backdrop.b * backdrop.a * (1 - source.a)
let ao = source.a + backdrop.a * (1 - source.a)
return {
r: ro / ao * 255,
g: go / ao * 255,
b: bo / ao * 255,
a: ao * 255
}
},
normalizeColor (c) {
return {
r: c.r / 255,
g: c.g / 255,
b: c.b / 255,
a: c.a / 255
}
},
getRandomGreen () {
colorMode(HSB, 360, 100, 100)
let c = color(
random(90, 150),
random(60,100),
random(35, 70)
)
colorMode(RGB, 255)
return c
},
getRandomBrown () {
colorMode(HSB, 360, 100, 100)
let c = color(
random(25, 42),
random(55, 100),
random(40, 75)
)
colorMode(RGB, 255)
return c
},
colorToRGB (color) {
return {
r: red(color),
g: green(color),
b: blue(color)
}
},
colorToRGBA (color) {
return {
r: red(color),
g: green(color),
b: blue(color),
a: 255
}
},
colorFromRGB (obj) {
return color(obj.r, obj.g, obj.b)
},
colorFromRGBA (obj) {
return color(obj.r, obj.g, obj.b, obj.a)
},
vectorToValues (vec) {
return {
x: vec.x,
y: vec.y,
z: vec.z
}
},
vectorFromValues (obj) {
return createVector(obj.x, obj.y, obj.z)
}
}