Skip to content

Commit cb7e718

Browse files
authored
Revert "Remove dependency on color lib and upgrade version to 2.1.0" (#24)
Revert "Remove dependency on color lib and upgrade version to 2.1.0 (#6)" This reverts commit d923914. It seems that the implementation of `hueShift` is slightly different in this commit than before and therefore produces slightly different colors as before. Reverting so that we can fix this bug.
1 parent a8c034f commit cb7e718

File tree

3 files changed

+47
-90
lines changed

3 files changed

+47
-90
lines changed

index.js

+6-90
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var MersenneTwister = require('mersenne-twister');
22
var paperGen = require('./paper')
3+
var Color = require('color')
34
var colors = require('./colors')
45
var shapeCount = 4
56
var svgns = 'http://www.w3.org/2000/svg'
@@ -69,94 +70,9 @@ function genColor(colors) {
6970
var wobble = 30
7071
function hueShift(colors, generator) {
7172
var amount = (generator.random() * 30) - (wobble / 2)
72-
var rotate = (hex) => colorRotate(hex, amount)
73-
return colors.map(rotate)
73+
return colors.map(function(hex) {
74+
var color = Color(hex)
75+
color.rotate(amount)
76+
return color.hexString()
77+
})
7478
}
75-
76-
function colorRotate(hex, degrees) {
77-
var hsl = hexToHSL(hex)
78-
var hue = hsl.h
79-
hue = (hue + degrees) % 360;
80-
hue = hue < 0 ? 360 + hue : hue;
81-
hsl.h = hue;
82-
return HSLToHex(hsl);
83-
}
84-
85-
function hexToHSL(hex) {
86-
// Convert hex to RGB first
87-
var r = "0x" + hex[1] + hex[2];
88-
var g = "0x" + hex[3] + hex[4];
89-
var b = "0x" + hex[5] + hex[6];
90-
// Then to HSL
91-
r /= 255;
92-
g /= 255;
93-
b /= 255;
94-
var cmin = Math.min(r,g,b),
95-
cmax = Math.max(r,g,b),
96-
delta = cmax - cmin,
97-
h = 0,
98-
s = 0,
99-
l = 0;
100-
101-
if (delta == 0)
102-
h = 0;
103-
else if (cmax == r)
104-
h = ((g - b) / delta) % 6;
105-
else if (cmax == g)
106-
h = (b - r) / delta + 2;
107-
else
108-
h = (r - g) / delta + 4;
109-
110-
h = Math.round(h * 60);
111-
112-
if (h < 0)
113-
h += 360;
114-
115-
l = (cmax + cmin) / 2;
116-
s = delta == 0 ? 0 : delta / (1 - Math.abs(2 * l - 1));
117-
s = +(s * 100).toFixed(1);
118-
l = +(l * 100).toFixed(1);
119-
120-
return {h, s, l}
121-
}
122-
123-
function HSLToHex(hsl) {
124-
var {h, s, l} = hsl
125-
s /= 100;
126-
l /= 100;
127-
128-
let c = (1 - Math.abs(2 * l - 1)) * s,
129-
x = c * (1 - Math.abs((h / 60) % 2 - 1)),
130-
m = l - c/2,
131-
r = 0,
132-
g = 0,
133-
b = 0;
134-
135-
if (0 <= h && h < 60) {
136-
r = c; g = x; b = 0;
137-
} else if (60 <= h && h < 120) {
138-
r = x; g = c; b = 0;
139-
} else if (120 <= h && h < 180) {
140-
r = 0; g = c; b = x;
141-
} else if (180 <= h && h < 240) {
142-
r = 0; g = x; b = c;
143-
} else if (240 <= h && h < 300) {
144-
r = x; g = 0; b = c;
145-
} else if (300 <= h && h < 360) {
146-
r = c; g = 0; b = x;
147-
}
148-
// Having obtained RGB, convert channels to hex
149-
r = Math.round((r + m) * 255).toString(16);
150-
g = Math.round((g + m) * 255).toString(16);
151-
b = Math.round((b + m) * 255).toString(16);
152-
153-
// Prepend 0s, if necessary
154-
if (r.length == 1)
155-
r = "0" + r;
156-
if (g.length == 1)
157-
g = "0" + g;
158-
if (b.length == 1)
159-
b = "0" + b;
160-
161-
return "#" + r + g + b;
162-
}

package-lock.json

+40
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
],
1818
"license": "ISC",
1919
"dependencies": {
20+
"color": "^0.11.3",
2021
"mersenne-twister": "^1.1.0"
2122
},
2223
"devDependencies": {

0 commit comments

Comments
 (0)