Color.js 13 KB

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