Skip to content

Commit 6fb4ad0

Browse files
committed
Added in various new methods such as hslToRgb.
1 parent a1e5c26 commit 6fb4ad0

1 file changed

Lines changed: 209 additions & 0 deletions

File tree

src/gameobjects/BitmapData.js

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,215 @@ Phaser.BitmapData.prototype = {
751751

752752

753753

754+
},
755+
756+
/**
757+
* Converts an RGB color to HSL.
758+
*
759+
* @author http://mjijackson.com
760+
* @method Phaser.BitmapData#rgbToHsl
761+
* @param {number} r - The r color component (0 - 255)
762+
* @param {number} g - The g color component (0 - 255)
763+
* @param {number} b - The b color component (0 - 255)
764+
* @return {object} An object containing 3 properties: h, s and l.
765+
*/
766+
rgbToHsl: function (r, g, b) {
767+
768+
if (Array.isArray(r)) {
769+
b = r[2];
770+
g = r[1];
771+
r = r[0];
772+
}
773+
774+
r /= 255;
775+
g /= 255;
776+
b /= 255;
777+
778+
var max = Math.max(r, g, b);
779+
var min = Math.min(r, g, b);
780+
var h, s, l = (max + min) / 2;
781+
782+
if (max === min)
783+
{
784+
h = s = 0; // achromatic
785+
}
786+
else
787+
{
788+
var d = max - min;
789+
790+
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
791+
792+
switch (max) {
793+
case r:
794+
h = (g - b) / d + (g < b ? 6 : 0);
795+
break;
796+
case g:
797+
h = (b - r) / d + 2;
798+
break;
799+
case b:
800+
h = (r - g) / d + 4;
801+
break;
802+
}
803+
804+
h /= 6;
805+
806+
}
807+
808+
return { h: h, s: s, l: l };
809+
810+
},
811+
812+
/**
813+
* Converts an RGB color to HSL.
814+
*
815+
* @author http://mjijackson.com
816+
* @method Phaser.BitmapData#hslToRgb
817+
* @param {number} h - The h color component (0 - 255)
818+
* @param {number} s - The s color component (0 - 255)
819+
* @param {number} l - The l color component (0 - 255)
820+
* @return {object} A color object.
821+
*/
822+
hslToRgb: function (h, s, l) {
823+
824+
var r, g, b;
825+
826+
if (s === 0)
827+
{
828+
r = g = b = l; // achromatic
829+
}
830+
else
831+
{
832+
var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
833+
var p = 2 * l - q;
834+
r = this.hue2rgb(p, q, h + 1 / 3);
835+
g = this.hue2rgb(p, q, h);
836+
b = this.hue2rgb(p, q, h - 1 / 3);
837+
}
838+
839+
return this.createColor(r * 255 | 0, g * 255 | 0, b * 255 | 0);
840+
841+
},
842+
843+
/**
844+
* Converts a hue to an RGB color.
845+
*
846+
* @author http://mjijackson.com
847+
* @method Phaser.BitmapData#hue2rgb
848+
* @private
849+
* @param {number} p
850+
* @param {number} q
851+
* @param {number} t
852+
* @return {number} The color component value.
853+
*/
854+
hue2rgb: function (p, q, t) {
855+
856+
if (t < 0) t += 1;
857+
if (t > 1) t -= 1;
858+
if (t < 1 / 6) return p + (q - p) * 6 * t;
859+
if (t < 1 / 2) return q;
860+
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
861+
862+
return p;
863+
864+
},
865+
866+
/**
867+
* Converts an RGB color to HSV.
868+
*
869+
* @author http://mjijackson.com
870+
* @method Phaser.BitmapData#rgbToHsv
871+
* @param {number} r - The r color component (0 - 255)
872+
* @param {number} g - The g color component (0 - 255)
873+
* @param {number} b - The b color component (0 - 255)
874+
* @return {object} An object containing 3 properties: h, s and v.
875+
*/
876+
rgbToHsv: function (r, g, b) {
877+
878+
if (Array.isArray(r))
879+
{
880+
b = r[2];
881+
g = r[1];
882+
r = r[0];
883+
}
884+
885+
r = r / 255, g = g / 255, b = b / 255;
886+
887+
var max = Math.max(r, g, b);
888+
var min = Math.min(r, g, b);
889+
var h, s, v = max;
890+
var d = max - min;
891+
892+
s = max == 0 ? 0 : d / max;
893+
894+
if (max === min)
895+
{
896+
h = 0; // achromatic
897+
}
898+
else
899+
{
900+
switch (max) {
901+
case r:
902+
h = (g - b) / d + (g < b ? 6 : 0);
903+
break;
904+
case g:
905+
h = (b - r) / d + 2;
906+
break;
907+
case b:
908+
h = (r - g) / d + 4;
909+
break;
910+
}
911+
912+
h /= 6;
913+
}
914+
915+
return { h: h, s: s, v: v };
916+
917+
},
918+
919+
/**
920+
* Converts a HSV color to RGB.
921+
*
922+
* @author http://mjijackson.com
923+
* @method Phaser.BitmapData#hsvToRgb
924+
* @param {number} h - The h color component (0 - 255)
925+
* @param {number} s - The s color component (0 - 255)
926+
* @param {number} v - The v color component (0 - 255)
927+
* @return {object} A color object.
928+
*/
929+
hsvToRgb: function (h, s, v) {
930+
931+
var r, g, b;
932+
933+
var i = Math.floor(h * 6);
934+
var f = h * 6 - i;
935+
var p = v * (1 - s);
936+
var q = v * (1 - f * s);
937+
var t = v * (1 - (1 - f) * s);
938+
939+
switch (i % 6)
940+
{
941+
case 0:
942+
r = v, g = t, b = p;
943+
break;
944+
case 1:
945+
r = q, g = v, b = p;
946+
break;
947+
case 2:
948+
r = p, g = v, b = t;
949+
break;
950+
case 3:
951+
r = p, g = q, b = v;
952+
break;
953+
case 4:
954+
r = t, g = p, b = v;
955+
break;
956+
case 5:
957+
r = v, g = p, b = q;
958+
break;
959+
}
960+
961+
return this.createColor(r * 255 | 0, g * 255 | 0, b * 255 | 0);
962+
754963
},
755964

756965
/**

0 commit comments

Comments
 (0)