Skip to content

Commit 9174558

Browse files
author
Lauren McCarthy
committed
Merge pull request #857 from xyfeng/master
color and colorMode update
2 parents 2d7d105 + 1c0c107 commit 9174558

File tree

12 files changed

+28816
-13328
lines changed

12 files changed

+28816
-13328
lines changed

docs/js/p5.js

Lines changed: 27999 additions & 4894 deletions
Large diffs are not rendered by default.

docs/js/p5.min.js

Lines changed: 8 additions & 7817 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/3d/p5.Renderer3D.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ p5.Renderer3D.prototype.background = function() {
9595
var gl = this.GL;
9696
var _col = this._pInst.color.apply(this._pInst, arguments);
9797
// gl.clearColor(0.0,0.0,0.0,1.0);
98-
var _r = (_col.color_array[0]) / 255;
99-
var _g = (_col.color_array[1]) / 255;
100-
var _b = (_col.color_array[2]) / 255;
101-
var _a = (_col.color_array[3]) / 255;
98+
var _r = (_col.rgba[0]) / 255;
99+
var _g = (_col.rgba[1]) / 255;
100+
var _b = (_col.rgba[2]) / 255;
101+
var _a = (_col.rgba[3]) / 255;
102102
gl.clearColor(_r, _g, _b, _a);
103103
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
104104
this.resetMatrix();

src/color/color_utils.js

Lines changed: 82 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,18 @@ p5.ColorUtils = {};
1111
/**
1212
* For a color expressed as an HSBA array, return the corresponding RGBA value
1313
* @param {Array} hsba An 'array' object that represents a list of HSB colors
14-
* @param {Array} maxes An 'array' object that represents the HSB range maxes
15-
* @return {Array} an array of RGBA values, on a scale of 0-255
14+
* @return {Array} an array of RGBA values, on a scale of 0-1
1615
*/
17-
p5.ColorUtils.hsbaToRGBA = function(hsba, maxes) {
16+
p5.ColorUtils.hsbaToRGBA = function(hsba) {
1817
var h = hsba[0];
1918
var s = hsba[1];
2019
var v = hsba[2];
21-
var a = hsba[3] || maxes[3];
22-
h /= maxes[0];
23-
s /= maxes[1];
24-
v /= maxes[2];
25-
a /= maxes[3];
20+
var a = hsba[3] || 1;
2621
// Adapted from http://www.easyrgb.com/math.html
2722
// hsv values = 0 - 1, rgb values = 0 - 255
2823
var RGBA = [];
2924
if(s===0){
30-
RGBA = [Math.round(v*255), Math.round(v*255), Math.round(v*255), a*255];
25+
RGBA = [v, v, v, a];
3126
} else {
3227
// h must be < 1
3328
var var_h = h * 6;
@@ -67,12 +62,7 @@ p5.ColorUtils.hsbaToRGBA = function(hsba, maxes) {
6762
g = var_1;
6863
b = var_2;
6964
}
70-
RGBA = [
71-
Math.round(r * 255),
72-
Math.round(g * 255),
73-
Math.round(b * 255),
74-
a * 255
75-
];
65+
RGBA = [r, g, b, a];
7666
}
7767
return RGBA;
7868
};
@@ -81,14 +71,13 @@ p5.ColorUtils.hsbaToRGBA = function(hsba, maxes) {
8171
* For a color expressed as an RGBA array, return the corresponding HSBA value
8272
*
8373
* @param {Array} rgba An 'array' object that represents a list of RGB colors
84-
* @param {Array} maxes An 'array' object that represents the RGB range maxes
85-
* @return {Array} an array of HSB values, scaled by the HSB-space maxes
74+
* @return {Array} an array of HSB values
8675
*/
87-
p5.ColorUtils.rgbaToHSBA = function(rgba, maxes) {
88-
var r = rgba[0]/maxes[0];
89-
var g = rgba[1]/maxes[1];
90-
var b = rgba[2]/maxes[2];
91-
var a = rgba[3]/maxes[3];
76+
p5.ColorUtils.rgbaToHSBA = function(rgba) {
77+
var r = rgba[0];
78+
var g = rgba[1];
79+
var b = rgba[2];
80+
var a = rgba[3] || 1;
9281

9382
var min = Math.min(r, g, b); //Min. value of RGB
9483
var max = Math.max(r, g, b); //Max. value of RGB
@@ -124,36 +113,26 @@ p5.ColorUtils.rgbaToHSBA = function(rgba, maxes) {
124113
h -= 1;
125114
}
126115
}
127-
return [
128-
Math.round(h * 360),
129-
Math.round(s * 100),
130-
Math.round(v * 100),
131-
a * 1
132-
];
116+
return [h, s, v, a];
133117
};
134118

135119
/**
136120
* For a color expressed as an HSLA array, return the corresponding RGBA value
137121
*
138122
* @param {Array} hsla An 'array' object that represents a list of HSL colors
139-
* @param {Array} maxes An 'array' object that represents the HSL range maxes
140-
*
141-
* @return {Array} an array of RGBA values, on a scale of 0-255
123+
* @return {Array} an array of RGBA values, on a scale of 0-1
142124
*/
143-
p5.ColorUtils.hslaToRGBA = function(hsla, maxes){
125+
p5.ColorUtils.hslaToRGBA = function(hsla){
144126
var h = hsla[0];
145127
var s = hsla[1];
146128
var l = hsla[2];
147-
var a = hsla[3] || maxes[3];
148-
h /= maxes[0];
149-
s /= maxes[1];
150-
l /= maxes[2];
151-
a /= maxes[3];
129+
var a = hsla[3] || 1;
130+
152131
// Adapted from http://www.easyrgb.com/math.html
153-
// hsl values = 0 - 1, rgb values = 0 - 255
132+
// hsl values = 0 - 1, rgb values = 0 - 1
154133
var rgba = [];
155134
if(s === 0){
156-
rgba = [Math.round(l*255), Math.round(l*255), Math.round(l*255), a];
135+
rgba = [l, l, l, a];
157136
} else {
158137
var m, n, r, g, b;
159138

@@ -182,31 +161,23 @@ p5.ColorUtils.hslaToRGBA = function(hsla, maxes){
182161
g = convert( m, n, h );
183162
b = convert( m, n, h - ( 1 / 3 ) );
184163

185-
rgba = [
186-
Math.round(r * 255),
187-
Math.round(g * 255),
188-
Math.round(b * 255),
189-
Math.round(a * 255)
190-
];
191-
164+
rgba = [r, g, b, a];
192165
}
193166

194167
return rgba;
195-
196168
};
197169

198170
/**
199171
* For a color expressed as an RGBA array, return the corresponding HSBA value
200172
*
201173
* @param {Array} rgba An 'array' object that represents a list of RGB colors
202-
* @param {Array} maxes An 'array' object that represents the RGB range maxes
203-
* @return {Array} an array of HSL values, scaled by the HSL-space maxes
174+
* @return {Array} an array of HSL values
204175
*/
205-
p5.ColorUtils.rgbaToHSLA = function(rgba, maxes) {
206-
var r = rgba[0]/maxes[0];
207-
var g = rgba[1]/maxes[1];
208-
var b = rgba[2]/maxes[2];
209-
var a = rgba[3]/maxes[3];
176+
p5.ColorUtils.rgbaToHSLA = function(rgba) {
177+
var r = rgba[0];
178+
var g = rgba[1];
179+
var b = rgba[2];
180+
var a = rgba[3] || 1;
210181

211182
var min = Math.min(r, g, b); //Min. value of RGB
212183
var max = Math.max(r, g, b); //Max. value of RGB
@@ -252,12 +223,63 @@ p5.ColorUtils.rgbaToHSLA = function(rgba, maxes) {
252223
}
253224

254225
}
255-
return [
256-
Math.round(h * 360),
257-
Math.round(s * 100),
258-
Math.round(l * 100),
259-
a * 1
260-
];
226+
return [h, s, l, a];
227+
};
228+
229+
/**
230+
* For a color expressed as an hsla array, return the corresponding HSBA value
231+
*
232+
* @param {Array} hsla An 'array' object that represents a list of HSLA colors
233+
* @return {Array} an array of HSBA values
234+
*/
235+
p5.ColorUtils.hslaToHSBA = function(hsla) {
236+
var h = hsla[0];
237+
var s = hsla[1];
238+
var l = hsla[2];
239+
var a = hsla[3] || 1;
240+
241+
var v;
242+
243+
//Hue and Alpha stay the same
244+
s *= l < 0.5 ? l : 1 - l;
245+
v = l + s;
246+
s = 2 * s / (l + s);
247+
248+
return[ h, s, v, a];
249+
};
250+
251+
/**
252+
* For a color expressed as an hsba array, return the corresponding HSLA value
253+
*
254+
* @param {Array} hsba An 'array' object that represents a list of HSBA colors
255+
* @return {Array} an array of HSLA values
256+
*/
257+
p5.ColorUtils.hsbaToHSLA = function(hsba) {
258+
var h = hsba[0];
259+
var s = hsba[1];
260+
var v = hsba[2];
261+
var a = hsba[3] || 1;
262+
263+
//Hue and Alpha stay the same
264+
//Lightness is (2 - s) * v / 2
265+
var l = (2 - s) * v / 2;
266+
267+
//Saturation is very different between the two color spaces
268+
//If l < 0.5 set it to s / (2 - s)
269+
//Otherwise s * v / (2 - (2 - s) * v)
270+
if( l !== 0 ){
271+
if( l === 1 ){
272+
s = 0;
273+
}
274+
else if( l < 0.5 ){
275+
s = s / (2 - s);
276+
}
277+
else{
278+
s = s * v / (2 - l * 2);
279+
}
280+
}
281+
282+
return [ h, s, l, a];
261283
};
262284

263285
module.exports = p5.ColorUtils;

0 commit comments

Comments
 (0)