/src/math/Color.js

https://github.com/kewinwang/three.js · JavaScript · 385 lines · 220 code · 155 blank · 10 comment · 17 complexity · b398759bcf867247114370974d6048ec MD5 · raw file

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