Color.js 13 KB

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