Color.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.Color = function ( value ) {
  5. if ( value !== undefined ) this.set( value );
  6. return this;
  7. };
  8. THREE.Color.prototype = {
  9. constructor: THREE.Color,
  10. r: 1, g: 1, b: 1,
  11. set: function ( value ) {
  12. switch ( typeof value ) {
  13. case "number":
  14. this.setHex( value );
  15. break;
  16. case "string":
  17. this.setStyle( value );
  18. break;
  19. }
  20. },
  21. setHex: function ( hex ) {
  22. hex = Math.floor( hex );
  23. this.r = ( hex >> 16 & 255 ) / 255;
  24. this.g = ( hex >> 8 & 255 ) / 255;
  25. this.b = ( hex & 255 ) / 255;
  26. return this;
  27. },
  28. setRGB: function ( r, g, b ) {
  29. this.r = r;
  30. this.g = g;
  31. this.b = b;
  32. return this;
  33. },
  34. setHSV: function ( h, s, v ) {
  35. // based on MochiKit implementation by Bob Ippolito
  36. // h,s,v ranges are < 0.0 - 1.0 >
  37. var i, f, p, q, t;
  38. if ( v === 0 ) {
  39. this.r = this.g = this.b = 0;
  40. } else {
  41. i = Math.floor( h * 6 );
  42. f = ( h * 6 ) - i;
  43. p = v * ( 1 - s );
  44. q = v * ( 1 - ( s * f ) );
  45. t = v * ( 1 - ( s * ( 1 - f ) ) );
  46. if ( i === 0 ) {
  47. this.r = v;
  48. this.g = t;
  49. this.b = p;
  50. } else if ( i === 1 ) {
  51. this.r = q;
  52. this.g = v;
  53. this.b = p;
  54. } else if ( i === 2 ) {
  55. this.r = p;
  56. this.g = v;
  57. this.b = t;
  58. } else if ( i === 3 ) {
  59. this.r = p;
  60. this.g = q;
  61. this.b = v;
  62. } else if ( i === 4 ) {
  63. this.r = t;
  64. this.g = p;
  65. this.b = v;
  66. } else if ( i === 5 ) {
  67. this.r = v;
  68. this.g = p;
  69. this.b = q;
  70. }
  71. }
  72. return this;
  73. },
  74. setStyle: function ( style ) {
  75. // rgb(255,0,0)
  76. if ( /^rgb\((\d+),(\d+),(\d+)\)$/i.test( style ) ) {
  77. var color = /^rgb\((\d+),(\d+),(\d+)\)$/i.exec( style );
  78. this.r = Math.min( 255, parseInt( color[ 1 ], 10 ) ) / 255;
  79. this.g = Math.min( 255, parseInt( color[ 2 ], 10 ) ) / 255;
  80. this.b = Math.min( 255, parseInt( color[ 3 ], 10 ) ) / 255;
  81. return this;
  82. }
  83. // rgb(100%,0%,0%)
  84. if ( /^rgb\((\d+)\%,(\d+)\%,(\d+)\%\)$/i.test( style ) ) {
  85. var color = /^rgb\((\d+)\%,(\d+)\%,(\d+)\%\)$/i.exec( style );
  86. this.r = Math.min( 100, parseInt( color[ 1 ], 10 ) ) / 100;
  87. this.g = Math.min( 100, parseInt( color[ 2 ], 10 ) ) / 100;
  88. this.b = Math.min( 100, parseInt( color[ 3 ], 10 ) ) / 100;
  89. return this;
  90. }
  91. // #ff0000
  92. if ( /^\#([0-9a-f]{6})$/i.test( style ) ) {
  93. var color = /^\#([0-9a-f]{6})$/i.exec( style );
  94. this.setHex( parseInt( color[ 1 ], 16 ) );
  95. return this;
  96. }
  97. // #f00
  98. if ( /^\#([0-9a-f])([0-9a-f])([0-9a-f])$/i.test( style ) ) {
  99. var color = /^\#([0-9a-f])([0-9a-f])([0-9a-f])$/i.exec( style );
  100. this.setHex( parseInt( color[ 1 ] + color[ 1 ] + color[ 2 ] + color[ 2 ] + color[ 3 ] + color[ 3 ], 16 ) );
  101. return this;
  102. }
  103. // red
  104. if ( /^(\w+)$/i.test( style ) ) {
  105. this.setHex( THREE.ColorKeywords[ style ] );
  106. return this;
  107. }
  108. },
  109. copy: function ( color ) {
  110. this.r = color.r;
  111. this.g = color.g;
  112. this.b = color.b;
  113. return this;
  114. },
  115. copyGammaToLinear: function ( color ) {
  116. this.r = color.r * color.r;
  117. this.g = color.g * color.g;
  118. this.b = color.b * color.b;
  119. return this;
  120. },
  121. copyLinearToGamma: function ( color ) {
  122. this.r = Math.sqrt( color.r );
  123. this.g = Math.sqrt( color.g );
  124. this.b = Math.sqrt( color.b );
  125. return this;
  126. },
  127. convertGammaToLinear: function () {
  128. var r = this.r, g = this.g, b = this.b;
  129. this.r = r * r;
  130. this.g = g * g;
  131. this.b = b * b;
  132. return this;
  133. },
  134. convertLinearToGamma: function () {
  135. this.r = Math.sqrt( this.r );
  136. this.g = Math.sqrt( this.g );
  137. this.b = Math.sqrt( this.b );
  138. return this;
  139. },
  140. getHex: function () {
  141. return ( this.r * 255 ) << 16 ^ ( this.g * 255 ) << 8 ^ ( this.b * 255 ) << 0;
  142. },
  143. getHexString: function () {
  144. return ( '000000' + this.getHex().toString( 16 ) ).slice( - 6 );
  145. },
  146. getStyle: function () {
  147. return 'rgb(' + ( ( this.r * 255 ) | 0 ) + ',' + ( ( this.g * 255 ) | 0 ) + ',' + ( ( this.b * 255 ) | 0 ) + ')';
  148. },
  149. getHSV: function ( hsv ) {
  150. // based on MochiKit implementation by Bob Ippolito
  151. // h,s,v ranges are < 0.0 - 1.0 >
  152. var r = this.r;
  153. var g = this.g;
  154. var b = this.b;
  155. var max = Math.max( Math.max( r, g ), b );
  156. var min = Math.min( Math.min( r, g ), b );
  157. var hue;
  158. var saturation;
  159. var value = max;
  160. if ( min === max ) {
  161. hue = 0;
  162. saturation = 0;
  163. } else {
  164. var delta = ( max - min );
  165. saturation = delta / max;
  166. if ( r === max ) {
  167. hue = ( g - b ) / delta;
  168. } else if ( g === max ) {
  169. hue = 2 + ( ( b - r ) / delta );
  170. } else {
  171. hue = 4 + ( ( r - g ) / delta );
  172. }
  173. hue /= 6;
  174. if ( hue < 0 ) {
  175. hue += 1;
  176. }
  177. if ( hue > 1 ) {
  178. hue -= 1;
  179. }
  180. }
  181. if ( hsv === undefined ) {
  182. hsv = { h: 0, s: 0, v: 0 };
  183. }
  184. hsv.h = hue;
  185. hsv.s = saturation;
  186. hsv.v = value;
  187. return hsv;
  188. },
  189. add: function ( color ) {
  190. this.r += color.r;
  191. this.g += color.g;
  192. this.b += color.b;
  193. return this;
  194. },
  195. addColors: function ( color1, color2 ) {
  196. this.r = color1.r + color2.r;
  197. this.g = color1.g + color2.g;
  198. this.b = color1.b + color2.b;
  199. return this;
  200. },
  201. addScalar: function ( s ) {
  202. this.r += s;
  203. this.g += s;
  204. this.b += s;
  205. return this;
  206. },
  207. multiply: function ( color ) {
  208. this.r *= color.r;
  209. this.g *= color.g;
  210. this.b *= color.b;
  211. return this;
  212. },
  213. multiplyScalar: function ( s ) {
  214. this.r *= s;
  215. this.g *= s;
  216. this.b *= s;
  217. return this;
  218. },
  219. lerpSelf: function ( color, alpha ) {
  220. this.r += ( color.r - this.r ) * alpha;
  221. this.g += ( color.g - this.g ) * alpha;
  222. this.b += ( color.b - this.b ) * alpha;
  223. return this;
  224. },
  225. clone: function () {
  226. return new THREE.Color().setRGB( this.r, this.g, this.b );
  227. }
  228. };
  229. THREE.ColorKeywords = { "aliceblue": 0xF0F8FF, "antiquewhite": 0xFAEBD7, "aqua": 0x00FFFF, "aquamarine": 0x7FFFD4, "azure": 0xF0FFFF,
  230. "beige": 0xF5F5DC, "bisque": 0xFFE4C4, "black": 0x000000, "blanchedalmond": 0xFFEBCD, "blue": 0x0000FF, "blueviolet": 0x8A2BE2,
  231. "brown": 0xA52A2A, "burlywood": 0xDEB887, "cadetblue": 0x5F9EA0, "chartreuse": 0x7FFF00, "chocolate": 0xD2691E, "coral": 0xFF7F50,
  232. "cornflowerblue": 0x6495ED, "cornsilk": 0xFFF8DC, "crimson": 0xDC143C, "cyan": 0x00FFFF, "darkblue": 0x00008B, "darkcyan": 0x008B8B,
  233. "darkgoldenrod": 0xB8860B, "darkgray": 0xA9A9A9, "darkgreen": 0x006400, "darkgrey": 0xA9A9A9, "darkkhaki": 0xBDB76B, "darkmagenta": 0x8B008B,
  234. "darkolivegreen": 0x556B2F, "darkorange": 0xFF8C00, "darkorchid": 0x9932CC, "darkred": 0x8B0000, "darksalmon": 0xE9967A, "darkseagreen": 0x8FBC8F,
  235. "darkslateblue": 0x483D8B, "darkslategray": 0x2F4F4F, "darkslategrey": 0x2F4F4F, "darkturquoise": 0x00CED1, "darkviolet": 0x9400D3,
  236. "deeppink": 0xFF1493, "deepskyblue": 0x00BFFF, "dimgray": 0x696969, "dimgrey": 0x696969, "dodgerblue": 0x1E90FF, "firebrick": 0xB22222,
  237. "floralwhite": 0xFFFAF0, "forestgreen": 0x228B22, "fuchsia": 0xFF00FF, "gainsboro": 0xDCDCDC, "ghostwhite": 0xF8F8FF, "gold": 0xFFD700,
  238. "goldenrod": 0xDAA520, "gray": 0x808080, "green": 0x008000, "greenyellow": 0xADFF2F, "grey": 0x808080, "honeydew": 0xF0FFF0, "hotpink": 0xFF69B4,
  239. "indianred": 0xCD5C5C, "indigo": 0x4B0082, "ivory": 0xFFFFF0, "khaki": 0xF0E68C, "lavender": 0xE6E6FA, "lavenderblush": 0xFFF0F5, "lawngreen": 0x7CFC00,
  240. "lemonchiffon": 0xFFFACD, "lightblue": 0xADD8E6, "lightcoral": 0xF08080, "lightcyan": 0xE0FFFF, "lightgoldenrodyellow": 0xFAFAD2, "lightgray": 0xD3D3D3,
  241. "lightgreen": 0x90EE90, "lightgrey": 0xD3D3D3, "lightpink": 0xFFB6C1, "lightsalmon": 0xFFA07A, "lightseagreen": 0x20B2AA, "lightskyblue": 0x87CEFA,
  242. "lightslategray": 0x778899, "lightslategrey": 0x778899, "lightsteelblue": 0xB0C4DE, "lightyellow": 0xFFFFE0, "lime": 0x00FF00, "limegreen": 0x32CD32,
  243. "linen": 0xFAF0E6, "magenta": 0xFF00FF, "maroon": 0x800000, "mediumaquamarine": 0x66CDAA, "mediumblue": 0x0000CD, "mediumorchid": 0xBA55D3,
  244. "mediumpurple": 0x9370DB, "mediumseagreen": 0x3CB371, "mediumslateblue": 0x7B68EE, "mediumspringgreen": 0x00FA9A, "mediumturquoise": 0x48D1CC,
  245. "mediumvioletred": 0xC71585, "midnightblue": 0x191970, "mintcream": 0xF5FFFA, "mistyrose": 0xFFE4E1, "moccasin": 0xFFE4B5, "navajowhite": 0xFFDEAD,
  246. "navy": 0x000080, "oldlace": 0xFDF5E6, "olive": 0x808000, "olivedrab": 0x6B8E23, "orange": 0xFFA500, "orangered": 0xFF4500, "orchid": 0xDA70D6,
  247. "palegoldenrod": 0xEEE8AA, "palegreen": 0x98FB98, "paleturquoise": 0xAFEEEE, "palevioletred": 0xDB7093, "papayawhip": 0xFFEFD5, "peachpuff": 0xFFDAB9,
  248. "peru": 0xCD853F, "pink": 0xFFC0CB, "plum": 0xDDA0DD, "powderblue": 0xB0E0E6, "purple": 0x800080, "red": 0xFF0000, "rosybrown": 0xBC8F8F,
  249. "royalblue": 0x4169E1, "saddlebrown": 0x8B4513, "salmon": 0xFA8072, "sandybrown": 0xF4A460, "seagreen": 0x2E8B57, "seashell": 0xFFF5EE,
  250. "sienna": 0xA0522D, "silver": 0xC0C0C0, "skyblue": 0x87CEEB, "slateblue": 0x6A5ACD, "slategray": 0x708090, "slategrey": 0x708090, "snow": 0xFFFAFA,
  251. "springgreen": 0x00FF7F, "steelblue": 0x4682B4, "tan": 0xD2B48C, "teal": 0x008080, "thistle": 0xD8BFD8, "tomato": 0xFF6347, "turquoise": 0x40E0D0,
  252. "violet": 0xEE82EE, "wheat": 0xF5DEB3, "white": 0xFFFFFF, "whitesmoke": 0xF5F5F5, "yellow": 0xFFFF00, "yellowgreen": 0x9ACD32 };