Color.js 11 KB

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