Skip to content

Commit de9fc08

Browse files
committed
Color.updateColor - updates an existing color object to update the rgba property.
Color.HSVColorWheel will return an array with 360 color objects for each segment of an HSV color wheel, you can optionally set the saturation and value amounts. Color.HSLColorWheel will return an array with 360 color objects for each segment of an HSL color wheel, you can optionally set the saturation and lightness amounts.
1 parent 27d62aa commit de9fc08

3 files changed

Lines changed: 92 additions & 15 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,10 @@ Version 2.0.4 - "Mos Shirare" - in development
126126
* Color.RGBtoHSV converts an rgb color into hsv (hue, saturation, value)
127127
* Color.HSVtoRGB converts an hsv value into an rgb color object.
128128
* Color.createColor - creates the new light-weight color object used by most Color conversion methods.
129+
* Color.updateColor - updates an existing color object to update the rgba property.
129130
* Color.RGBtoString converts an rgba color into a # or 0x color string.
130-
* Color.HSVColorWheel will return an array with 360 color objects for each segment of an HSV color wheel.
131+
* Color.HSVColorWheel will return an array with 360 color objects for each segment of an HSV color wheel, you can optionally set the saturation and value amounts.
132+
* Color.HSLColorWheel will return an array with 360 color objects for each segment of an HSL color wheel, you can optionally set the saturation and lightness amounts.
131133

132134

133135
### Bug Fixes

src/gameobjects/BitmapData.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,30 @@ Phaser.BitmapData.prototype = {
809809

810810

811811

812+
},
813+
814+
/**
815+
* Draws a filled Circle to the BitmapData at the given x, y coordinates and radius in size.
816+
*
817+
* @method Phaser.BitmapData#circle
818+
* @param {number} x - The x coordinate to draw the Circle at.
819+
* @param {number} y - The y coordinate to draw the Circle at.
820+
* @param {number} radius - The radius of the Circle.
821+
* @param {string} [fillStyle] - If set the context fillStyle will be set to this value before the circle is drawn.
822+
*/
823+
circle: function (x, y, radius, fillStyle) {
824+
825+
if (typeof fillStyle !== 'undefined')
826+
{
827+
this.context.fillStyle = fillStyle;
828+
}
829+
830+
this.context.beginPath();
831+
this.context.arc(x, y, radius, 0, Math.PI * 2, false);
832+
this.context.closePath();
833+
834+
this.context.fill();
835+
812836
},
813837

814838
/**

src/utils/Color.js

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,15 @@ Phaser.Color = {
237237
out.b = Phaser.Color.hueToColor(p, q, h - 1 / 3);
238238
}
239239

240-
out.r = (out.r * 255 | 0);
241-
out.g = (out.g * 255 | 0);
242-
out.b = (out.b * 255 | 0);
240+
// out.r = (out.r * 255 | 0);
241+
// out.g = (out.g * 255 | 0);
242+
// out.b = (out.b * 255 | 0);
243+
244+
out.r = Math.floor((out.r * 255 | 0));
245+
out.g = Math.floor((out.g * 255 | 0));
246+
out.b = Math.floor((out.b * 255 | 0));
247+
248+
Phaser.Color.updateColor(out);
243249

244250
return out;
245251

@@ -317,10 +323,7 @@ Phaser.Color = {
317323
*/
318324
HSVtoRGB: function (h, s, v, out) {
319325

320-
if (!out)
321-
{
322-
out = Phaser.Color.createColor(0, 0, 0, 0, h, s, 0, v);
323-
}
326+
if (typeof out === 'undefined') { out = Phaser.Color.createColor(0, 0, 0, 1, h, s, 0, v); }
324327

325328
var r, g, b;
326329
var i = Math.floor(h * 6);
@@ -363,9 +366,11 @@ Phaser.Color = {
363366
break;
364367
}
365368

366-
out.r = r * 255;
367-
out.g = g * 255;
368-
out.b = b * 255;
369+
out.r = Math.floor(r * 255);
370+
out.g = Math.floor(g * 255);
371+
out.b = Math.floor(b * 255);
372+
373+
Phaser.Color.updateColor(out);
369374

370375
return out;
371376

@@ -425,7 +430,7 @@ Phaser.Color = {
425430
* @param {number} [r=0] - The red color component, in the range 0 - 255.
426431
* @param {number} [g=0] - The green color component, in the range 0 - 255.
427432
* @param {number} [b=0] - The blue color component, in the range 0 - 255.
428-
* @param {number} [a=0] - The alpha color component, in the range 0 - 1.
433+
* @param {number} [a=1] - The alpha color component, in the range 0 - 1.
429434
* @param {number} [h=0] - The hue, in the range 0 - 1.
430435
* @param {number} [s=0] - The saturation, in the range 0 - 1.
431436
* @param {number} [l=0] - The lightness, in the range 0 - 1.
@@ -434,7 +439,23 @@ Phaser.Color = {
434439
*/
435440
createColor: function (r, g, b, a, h, s, l, v) {
436441

437-
var out = { r: r || 0, g: g || 0, b: b || 0, a: a || 0, h: h || 0, s: s || 0, l: l || 0, v: v || 0, color: 0 };
442+
var out = { r: r || 0, g: g || 0, b: b || 0, a: a || 1, h: h || 0, s: s || 0, l: l || 0, v: v || 0, color: 0 };
443+
444+
out.rgba = 'rgba(' + out.r + ',' + out.g + ',' + out.b + ',' + out.a + ')';
445+
446+
return out;
447+
448+
},
449+
450+
/**
451+
* Takes a color object and updates the rgba property.
452+
*
453+
* @method Phaser.Color.updateColor
454+
* @static
455+
* @param {object} out - The color object to update.
456+
* @returns {number} A native color value integer (format: 0xAARRGGBB).
457+
*/
458+
updateColor: function (out) {
438459

439460
out.rgba = 'rgba(' + out.r + ',' + out.g + ',' + out.b + ',' + out.a + ')';
440461

@@ -579,15 +600,45 @@ Phaser.Color = {
579600
*
580601
* @method Phaser.Color.HSVColorWheel
581602
* @static
603+
* @param {number} [s=1] - The saturation, in the range 0 - 1.
604+
* @param {number} [v=1] - The value, in the range 0 - 1.
582605
* @return {array} An array containing 360 elements corresponding to the HSV color wheel.
583606
*/
584-
HSVColorWheel: function () {
607+
HSVColorWheel: function (s, v) {
608+
609+
if (typeof s === 'undefined') { s = 1.0; }
610+
if (typeof v === 'undefined') { v = 1.0; }
611+
612+
var colors = [];
613+
614+
for (var c = 0; c <= 359; c++)
615+
{
616+
colors.push(Phaser.Color.HSVtoRGB(c / 359, s, v));
617+
}
618+
619+
return colors;
620+
621+
},
622+
623+
/**
624+
* Get HSL color wheel values in an array which will be 360 elements in size.
625+
*
626+
* @method Phaser.Color.HSLColorWheel
627+
* @static
628+
* @param {number} [s=0.5] - The saturation, in the range 0 - 1.
629+
* @param {number} [l=0.5] - The lightness, in the range 0 - 1.
630+
* @return {array} An array containing 360 elements corresponding to the HSL color wheel.
631+
*/
632+
HSLColorWheel: function (s, l) {
633+
634+
if (typeof s === 'undefined') { s = 0.5; }
635+
if (typeof l === 'undefined') { l = 0.5; }
585636

586637
var colors = [];
587638

588639
for (var c = 0; c <= 359; c++)
589640
{
590-
colors[c] = Phaser.Color.HSVtoRGB(c, 1.0, 1.0);
641+
colors.push(Phaser.Color.HSLtoRGB(c / 359, s, l));
591642
}
592643

593644
return colors;

0 commit comments

Comments
 (0)