Color.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. import { clamp, euclideanModulo, lerp } from './MathUtils.js';
  2. import { ColorManagement, SRGBToLinear, LinearToSRGB } from './ColorManagement.js';
  3. import { SRGBColorSpace } from '../constants.js';
  4. import { warn } from '../utils.js';
  5. const _colorKeywords = { 'aliceblue': 0xF0F8FF, 'antiquewhite': 0xFAEBD7, 'aqua': 0x00FFFF, 'aquamarine': 0x7FFFD4, 'azure': 0xF0FFFF,
  6. 'beige': 0xF5F5DC, 'bisque': 0xFFE4C4, 'black': 0x000000, 'blanchedalmond': 0xFFEBCD, 'blue': 0x0000FF, 'blueviolet': 0x8A2BE2,
  7. 'brown': 0xA52A2A, 'burlywood': 0xDEB887, 'cadetblue': 0x5F9EA0, 'chartreuse': 0x7FFF00, 'chocolate': 0xD2691E, 'coral': 0xFF7F50,
  8. 'cornflowerblue': 0x6495ED, 'cornsilk': 0xFFF8DC, 'crimson': 0xDC143C, 'cyan': 0x00FFFF, 'darkblue': 0x00008B, 'darkcyan': 0x008B8B,
  9. 'darkgoldenrod': 0xB8860B, 'darkgray': 0xA9A9A9, 'darkgreen': 0x006400, 'darkgrey': 0xA9A9A9, 'darkkhaki': 0xBDB76B, 'darkmagenta': 0x8B008B,
  10. 'darkolivegreen': 0x556B2F, 'darkorange': 0xFF8C00, 'darkorchid': 0x9932CC, 'darkred': 0x8B0000, 'darksalmon': 0xE9967A, 'darkseagreen': 0x8FBC8F,
  11. 'darkslateblue': 0x483D8B, 'darkslategray': 0x2F4F4F, 'darkslategrey': 0x2F4F4F, 'darkturquoise': 0x00CED1, 'darkviolet': 0x9400D3,
  12. 'deeppink': 0xFF1493, 'deepskyblue': 0x00BFFF, 'dimgray': 0x696969, 'dimgrey': 0x696969, 'dodgerblue': 0x1E90FF, 'firebrick': 0xB22222,
  13. 'floralwhite': 0xFFFAF0, 'forestgreen': 0x228B22, 'fuchsia': 0xFF00FF, 'gainsboro': 0xDCDCDC, 'ghostwhite': 0xF8F8FF, 'gold': 0xFFD700,
  14. 'goldenrod': 0xDAA520, 'gray': 0x808080, 'green': 0x008000, 'greenyellow': 0xADFF2F, 'grey': 0x808080, 'honeydew': 0xF0FFF0, 'hotpink': 0xFF69B4,
  15. 'indianred': 0xCD5C5C, 'indigo': 0x4B0082, 'ivory': 0xFFFFF0, 'khaki': 0xF0E68C, 'lavender': 0xE6E6FA, 'lavenderblush': 0xFFF0F5, 'lawngreen': 0x7CFC00,
  16. 'lemonchiffon': 0xFFFACD, 'lightblue': 0xADD8E6, 'lightcoral': 0xF08080, 'lightcyan': 0xE0FFFF, 'lightgoldenrodyellow': 0xFAFAD2, 'lightgray': 0xD3D3D3,
  17. 'lightgreen': 0x90EE90, 'lightgrey': 0xD3D3D3, 'lightpink': 0xFFB6C1, 'lightsalmon': 0xFFA07A, 'lightseagreen': 0x20B2AA, 'lightskyblue': 0x87CEFA,
  18. 'lightslategray': 0x778899, 'lightslategrey': 0x778899, 'lightsteelblue': 0xB0C4DE, 'lightyellow': 0xFFFFE0, 'lime': 0x00FF00, 'limegreen': 0x32CD32,
  19. 'linen': 0xFAF0E6, 'magenta': 0xFF00FF, 'maroon': 0x800000, 'mediumaquamarine': 0x66CDAA, 'mediumblue': 0x0000CD, 'mediumorchid': 0xBA55D3,
  20. 'mediumpurple': 0x9370DB, 'mediumseagreen': 0x3CB371, 'mediumslateblue': 0x7B68EE, 'mediumspringgreen': 0x00FA9A, 'mediumturquoise': 0x48D1CC,
  21. 'mediumvioletred': 0xC71585, 'midnightblue': 0x191970, 'mintcream': 0xF5FFFA, 'mistyrose': 0xFFE4E1, 'moccasin': 0xFFE4B5, 'navajowhite': 0xFFDEAD,
  22. 'navy': 0x000080, 'oldlace': 0xFDF5E6, 'olive': 0x808000, 'olivedrab': 0x6B8E23, 'orange': 0xFFA500, 'orangered': 0xFF4500, 'orchid': 0xDA70D6,
  23. 'palegoldenrod': 0xEEE8AA, 'palegreen': 0x98FB98, 'paleturquoise': 0xAFEEEE, 'palevioletred': 0xDB7093, 'papayawhip': 0xFFEFD5, 'peachpuff': 0xFFDAB9,
  24. 'peru': 0xCD853F, 'pink': 0xFFC0CB, 'plum': 0xDDA0DD, 'powderblue': 0xB0E0E6, 'purple': 0x800080, 'rebeccapurple': 0x663399, 'red': 0xFF0000, 'rosybrown': 0xBC8F8F,
  25. 'royalblue': 0x4169E1, 'saddlebrown': 0x8B4513, 'salmon': 0xFA8072, 'sandybrown': 0xF4A460, 'seagreen': 0x2E8B57, 'seashell': 0xFFF5EE,
  26. 'sienna': 0xA0522D, 'silver': 0xC0C0C0, 'skyblue': 0x87CEEB, 'slateblue': 0x6A5ACD, 'slategray': 0x708090, 'slategrey': 0x708090, 'snow': 0xFFFAFA,
  27. 'springgreen': 0x00FF7F, 'steelblue': 0x4682B4, 'tan': 0xD2B48C, 'teal': 0x008080, 'thistle': 0xD8BFD8, 'tomato': 0xFF6347, 'turquoise': 0x40E0D0,
  28. 'violet': 0xEE82EE, 'wheat': 0xF5DEB3, 'white': 0xFFFFFF, 'whitesmoke': 0xF5F5F5, 'yellow': 0xFFFF00, 'yellowgreen': 0x9ACD32 };
  29. const _hslA = { h: 0, s: 0, l: 0 };
  30. const _hslB = { h: 0, s: 0, l: 0 };
  31. function hue2rgb( p, q, t ) {
  32. if ( t < 0 ) t += 1;
  33. if ( t > 1 ) t -= 1;
  34. if ( t < 1 / 6 ) return p + ( q - p ) * 6 * t;
  35. if ( t < 1 / 2 ) return q;
  36. if ( t < 2 / 3 ) return p + ( q - p ) * 6 * ( 2 / 3 - t );
  37. return p;
  38. }
  39. /**
  40. * A Color instance is represented by RGB components in the linear <i>working
  41. * color space</i>, which defaults to `LinearSRGBColorSpace`. Inputs
  42. * conventionally using `SRGBColorSpace` (such as hexadecimals and CSS
  43. * strings) are converted to the working color space automatically.
  44. *
  45. * ```js
  46. * // converted automatically from SRGBColorSpace to LinearSRGBColorSpace
  47. * const color = new THREE.Color().setHex( 0x112233 );
  48. * ```
  49. * Source color spaces may be specified explicitly, to ensure correct conversions.
  50. * ```js
  51. * // assumed already LinearSRGBColorSpace; no conversion
  52. * const color = new THREE.Color().setRGB( 0.5, 0.5, 0.5 );
  53. *
  54. * // converted explicitly from SRGBColorSpace to LinearSRGBColorSpace
  55. * const color = new THREE.Color().setRGB( 0.5, 0.5, 0.5, SRGBColorSpace );
  56. * ```
  57. * If THREE.ColorManagement is disabled, no conversions occur. For details,
  58. * see <i>Color management</i>. Iterating through a Color instance will yield
  59. * its components (r, g, b) in the corresponding order. A Color can be initialised
  60. * in any of the following ways:
  61. * ```js
  62. * //empty constructor - will default white
  63. * const color1 = new THREE.Color();
  64. *
  65. * //Hexadecimal color (recommended)
  66. * const color2 = new THREE.Color( 0xff0000 );
  67. *
  68. * //RGB string
  69. * const color3 = new THREE.Color("rgb(255, 0, 0)");
  70. * const color4 = new THREE.Color("rgb(100%, 0%, 0%)");
  71. *
  72. * //X11 color name - all 140 color names are supported.
  73. * //Note the lack of CamelCase in the name
  74. * const color5 = new THREE.Color( 'skyblue' );
  75. * //HSL string
  76. * const color6 = new THREE.Color("hsl(0, 100%, 50%)");
  77. *
  78. * //Separate RGB values between 0 and 1
  79. * const color7 = new THREE.Color( 1, 0, 0 );
  80. * ```
  81. */
  82. class Color {
  83. /**
  84. * Constructs a new color.
  85. *
  86. * Note that standard method of specifying color in three.js is with a hexadecimal triplet,
  87. * and that method is used throughout the rest of the documentation.
  88. *
  89. * @param {(number|string|Color)} [r] - The red component of the color. If `g` and `b` are
  90. * not provided, it can be hexadecimal triplet, a CSS-style string or another `Color` instance.
  91. * @param {number} [g] - The green component.
  92. * @param {number} [b] - The blue component.
  93. */
  94. constructor( r, g, b ) {
  95. /**
  96. * This flag can be used for type testing.
  97. *
  98. * @type {boolean}
  99. * @readonly
  100. * @default true
  101. */
  102. this.isColor = true;
  103. /**
  104. * The red component.
  105. *
  106. * @type {number}
  107. * @default 1
  108. */
  109. this.r = 1;
  110. /**
  111. * The green component.
  112. *
  113. * @type {number}
  114. * @default 1
  115. */
  116. this.g = 1;
  117. /**
  118. * The blue component.
  119. *
  120. * @type {number}
  121. * @default 1
  122. */
  123. this.b = 1;
  124. return this.set( r, g, b );
  125. }
  126. /**
  127. * Sets the colors's components from the given values.
  128. *
  129. * @param {(number|string|Color)} [r] - The red component of the color. If `g` and `b` are
  130. * not provided, it can be hexadecimal triplet, a CSS-style string or another `Color` instance.
  131. * @param {number} [g] - The green component.
  132. * @param {number} [b] - The blue component.
  133. * @return {Color} A reference to this color.
  134. */
  135. set( r, g, b ) {
  136. if ( g === undefined && b === undefined ) {
  137. // r is THREE.Color, hex or string
  138. const value = r;
  139. if ( value && value.isColor ) {
  140. this.copy( value );
  141. } else if ( typeof value === 'number' ) {
  142. this.setHex( value );
  143. } else if ( typeof value === 'string' ) {
  144. this.setStyle( value );
  145. }
  146. } else {
  147. this.setRGB( r, g, b );
  148. }
  149. return this;
  150. }
  151. /**
  152. * Sets the colors's components to the given scalar value.
  153. *
  154. * @param {number} scalar - The scalar value.
  155. * @return {Color} A reference to this color.
  156. */
  157. setScalar( scalar ) {
  158. this.r = scalar;
  159. this.g = scalar;
  160. this.b = scalar;
  161. return this;
  162. }
  163. /**
  164. * Sets this color from a hexadecimal value.
  165. *
  166. * @param {number} hex - The hexadecimal value.
  167. * @param {string} [colorSpace=SRGBColorSpace] - The color space.
  168. * @return {Color} A reference to this color.
  169. */
  170. setHex( hex, colorSpace = SRGBColorSpace ) {
  171. hex = Math.floor( hex );
  172. this.r = ( hex >> 16 & 255 ) / 255;
  173. this.g = ( hex >> 8 & 255 ) / 255;
  174. this.b = ( hex & 255 ) / 255;
  175. ColorManagement.colorSpaceToWorking( this, colorSpace );
  176. return this;
  177. }
  178. /**
  179. * Sets this color from RGB values.
  180. *
  181. * @param {number} r - Red channel value between `0.0` and `1.0`.
  182. * @param {number} g - Green channel value between `0.0` and `1.0`.
  183. * @param {number} b - Blue channel value between `0.0` and `1.0`.
  184. * @param {string} [colorSpace=ColorManagement.workingColorSpace] - The color space.
  185. * @return {Color} A reference to this color.
  186. */
  187. setRGB( r, g, b, colorSpace = ColorManagement.workingColorSpace ) {
  188. this.r = r;
  189. this.g = g;
  190. this.b = b;
  191. ColorManagement.colorSpaceToWorking( this, colorSpace );
  192. return this;
  193. }
  194. /**
  195. * Sets this color from RGB values.
  196. *
  197. * @param {number} h - Hue value between `0.0` and `1.0`.
  198. * @param {number} s - Saturation value between `0.0` and `1.0`.
  199. * @param {number} l - Lightness value between `0.0` and `1.0`.
  200. * @param {string} [colorSpace=ColorManagement.workingColorSpace] - The color space.
  201. * @return {Color} A reference to this color.
  202. */
  203. setHSL( h, s, l, colorSpace = ColorManagement.workingColorSpace ) {
  204. // h,s,l ranges are in 0.0 - 1.0
  205. h = euclideanModulo( h, 1 );
  206. s = clamp( s, 0, 1 );
  207. l = clamp( l, 0, 1 );
  208. if ( s === 0 ) {
  209. this.r = this.g = this.b = l;
  210. } else {
  211. const p = l <= 0.5 ? l * ( 1 + s ) : l + s - ( l * s );
  212. const q = ( 2 * l ) - p;
  213. this.r = hue2rgb( q, p, h + 1 / 3 );
  214. this.g = hue2rgb( q, p, h );
  215. this.b = hue2rgb( q, p, h - 1 / 3 );
  216. }
  217. ColorManagement.colorSpaceToWorking( this, colorSpace );
  218. return this;
  219. }
  220. /**
  221. * Sets this color from a CSS-style string. For example, `rgb(250, 0,0)`,
  222. * `rgb(100%, 0%, 0%)`, `hsl(0, 100%, 50%)`, `#ff0000`, `#f00`, or `red` ( or
  223. * any [X11 color name]{@link https://en.wikipedia.org/wiki/X11_color_names#Color_name_chart} -
  224. * all 140 color names are supported).
  225. *
  226. * @param {string} style - Color as a CSS-style string.
  227. * @param {string} [colorSpace=SRGBColorSpace] - The color space.
  228. * @return {Color} A reference to this color.
  229. */
  230. setStyle( style, colorSpace = SRGBColorSpace ) {
  231. function handleAlpha( string ) {
  232. if ( string === undefined ) return;
  233. if ( parseFloat( string ) < 1 ) {
  234. warn( 'Color: Alpha component of ' + style + ' will be ignored.' );
  235. }
  236. }
  237. let m;
  238. if ( m = /^(\w+)\(([^\)]*)\)/.exec( style ) ) {
  239. // rgb / hsl
  240. let color;
  241. const name = m[ 1 ];
  242. const components = m[ 2 ];
  243. switch ( name ) {
  244. case 'rgb':
  245. case 'rgba':
  246. if ( color = /^\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*(\d*\.?\d+)\s*)?$/.exec( components ) ) {
  247. // rgb(255,0,0) rgba(255,0,0,0.5)
  248. handleAlpha( color[ 4 ] );
  249. return this.setRGB(
  250. Math.min( 255, parseInt( color[ 1 ], 10 ) ) / 255,
  251. Math.min( 255, parseInt( color[ 2 ], 10 ) ) / 255,
  252. Math.min( 255, parseInt( color[ 3 ], 10 ) ) / 255,
  253. colorSpace
  254. );
  255. }
  256. if ( color = /^\s*(\d+)\%\s*,\s*(\d+)\%\s*,\s*(\d+)\%\s*(?:,\s*(\d*\.?\d+)\s*)?$/.exec( components ) ) {
  257. // rgb(100%,0%,0%) rgba(100%,0%,0%,0.5)
  258. handleAlpha( color[ 4 ] );
  259. return this.setRGB(
  260. Math.min( 100, parseInt( color[ 1 ], 10 ) ) / 100,
  261. Math.min( 100, parseInt( color[ 2 ], 10 ) ) / 100,
  262. Math.min( 100, parseInt( color[ 3 ], 10 ) ) / 100,
  263. colorSpace
  264. );
  265. }
  266. break;
  267. case 'hsl':
  268. case 'hsla':
  269. if ( color = /^\s*(\d*\.?\d+)\s*,\s*(\d*\.?\d+)\%\s*,\s*(\d*\.?\d+)\%\s*(?:,\s*(\d*\.?\d+)\s*)?$/.exec( components ) ) {
  270. // hsl(120,50%,50%) hsla(120,50%,50%,0.5)
  271. handleAlpha( color[ 4 ] );
  272. return this.setHSL(
  273. parseFloat( color[ 1 ] ) / 360,
  274. parseFloat( color[ 2 ] ) / 100,
  275. parseFloat( color[ 3 ] ) / 100,
  276. colorSpace
  277. );
  278. }
  279. break;
  280. default:
  281. warn( 'Color: Unknown color model ' + style );
  282. }
  283. } else if ( m = /^\#([A-Fa-f\d]+)$/.exec( style ) ) {
  284. // hex color
  285. const hex = m[ 1 ];
  286. const size = hex.length;
  287. if ( size === 3 ) {
  288. // #ff0
  289. return this.setRGB(
  290. parseInt( hex.charAt( 0 ), 16 ) / 15,
  291. parseInt( hex.charAt( 1 ), 16 ) / 15,
  292. parseInt( hex.charAt( 2 ), 16 ) / 15,
  293. colorSpace
  294. );
  295. } else if ( size === 6 ) {
  296. // #ff0000
  297. return this.setHex( parseInt( hex, 16 ), colorSpace );
  298. } else {
  299. warn( 'Color: Invalid hex color ' + style );
  300. }
  301. } else if ( style && style.length > 0 ) {
  302. return this.setColorName( style, colorSpace );
  303. }
  304. return this;
  305. }
  306. /**
  307. * Sets this color from a color name. Faster than {@link Color#setStyle} if
  308. * you don't need the other CSS-style formats.
  309. *
  310. * For convenience, the list of names is exposed in `Color.NAMES` as a hash.
  311. * ```js
  312. * Color.NAMES.aliceblue // returns 0xF0F8FF
  313. * ```
  314. *
  315. * @param {string} style - The color name.
  316. * @param {string} [colorSpace=SRGBColorSpace] - The color space.
  317. * @return {Color} A reference to this color.
  318. */
  319. setColorName( style, colorSpace = SRGBColorSpace ) {
  320. // color keywords
  321. const hex = _colorKeywords[ style.toLowerCase() ];
  322. if ( hex !== undefined ) {
  323. // red
  324. this.setHex( hex, colorSpace );
  325. } else {
  326. // unknown color
  327. warn( 'Color: Unknown color ' + style );
  328. }
  329. return this;
  330. }
  331. /**
  332. * Returns a new color with copied values from this instance.
  333. *
  334. * @return {Color} A clone of this instance.
  335. */
  336. clone() {
  337. return new this.constructor( this.r, this.g, this.b );
  338. }
  339. /**
  340. * Copies the values of the given color to this instance.
  341. *
  342. * @param {Color} color - The color to copy.
  343. * @return {Color} A reference to this color.
  344. */
  345. copy( color ) {
  346. this.r = color.r;
  347. this.g = color.g;
  348. this.b = color.b;
  349. return this;
  350. }
  351. /**
  352. * Copies the given color into this color, and then converts this color from
  353. * `SRGBColorSpace` to `LinearSRGBColorSpace`.
  354. *
  355. * @param {Color} color - The color to copy/convert.
  356. * @return {Color} A reference to this color.
  357. */
  358. copySRGBToLinear( color ) {
  359. this.r = SRGBToLinear( color.r );
  360. this.g = SRGBToLinear( color.g );
  361. this.b = SRGBToLinear( color.b );
  362. return this;
  363. }
  364. /**
  365. * Copies the given color into this color, and then converts this color from
  366. * `LinearSRGBColorSpace` to `SRGBColorSpace`.
  367. *
  368. * @param {Color} color - The color to copy/convert.
  369. * @return {Color} A reference to this color.
  370. */
  371. copyLinearToSRGB( color ) {
  372. this.r = LinearToSRGB( color.r );
  373. this.g = LinearToSRGB( color.g );
  374. this.b = LinearToSRGB( color.b );
  375. return this;
  376. }
  377. /**
  378. * Converts this color from `SRGBColorSpace` to `LinearSRGBColorSpace`.
  379. *
  380. * @return {Color} A reference to this color.
  381. */
  382. convertSRGBToLinear() {
  383. this.copySRGBToLinear( this );
  384. return this;
  385. }
  386. /**
  387. * Converts this color from `LinearSRGBColorSpace` to `SRGBColorSpace`.
  388. *
  389. * @return {Color} A reference to this color.
  390. */
  391. convertLinearToSRGB() {
  392. this.copyLinearToSRGB( this );
  393. return this;
  394. }
  395. /**
  396. * Returns the hexadecimal value of this color.
  397. *
  398. * @param {string} [colorSpace=SRGBColorSpace] - The color space.
  399. * @return {number} The hexadecimal value.
  400. */
  401. getHex( colorSpace = SRGBColorSpace ) {
  402. ColorManagement.workingToColorSpace( _color.copy( this ), colorSpace );
  403. return Math.round( clamp( _color.r * 255, 0, 255 ) ) * 65536 + Math.round( clamp( _color.g * 255, 0, 255 ) ) * 256 + Math.round( clamp( _color.b * 255, 0, 255 ) );
  404. }
  405. /**
  406. * Returns the hexadecimal value of this color as a string (for example, 'FFFFFF').
  407. *
  408. * @param {string} [colorSpace=SRGBColorSpace] - The color space.
  409. * @return {string} The hexadecimal value as a string.
  410. */
  411. getHexString( colorSpace = SRGBColorSpace ) {
  412. return ( '000000' + this.getHex( colorSpace ).toString( 16 ) ).slice( - 6 );
  413. }
  414. /**
  415. * Converts the colors RGB values into the HSL format and stores them into the
  416. * given target object.
  417. *
  418. * @param {{h:number,s:number,l:number}} target - The target object that is used to store the method's result.
  419. * @param {string} [colorSpace=ColorManagement.workingColorSpace] - The color space.
  420. * @return {{h:number,s:number,l:number}} The HSL representation of this color.
  421. */
  422. getHSL( target, colorSpace = ColorManagement.workingColorSpace ) {
  423. // h,s,l ranges are in 0.0 - 1.0
  424. ColorManagement.workingToColorSpace( _color.copy( this ), colorSpace );
  425. const r = _color.r, g = _color.g, b = _color.b;
  426. const max = Math.max( r, g, b );
  427. const min = Math.min( r, g, b );
  428. let hue, saturation;
  429. const lightness = ( min + max ) / 2.0;
  430. if ( min === max ) {
  431. hue = 0;
  432. saturation = 0;
  433. } else {
  434. const delta = max - min;
  435. saturation = lightness <= 0.5 ? delta / ( max + min ) : delta / ( 2 - max - min );
  436. switch ( max ) {
  437. case r: hue = ( g - b ) / delta + ( g < b ? 6 : 0 ); break;
  438. case g: hue = ( b - r ) / delta + 2; break;
  439. case b: hue = ( r - g ) / delta + 4; break;
  440. }
  441. hue /= 6;
  442. }
  443. target.h = hue;
  444. target.s = saturation;
  445. target.l = lightness;
  446. return target;
  447. }
  448. /**
  449. * Returns the RGB values of this color and stores them into the given target object.
  450. *
  451. * @param {Color} target - The target color that is used to store the method's result.
  452. * @param {string} [colorSpace=ColorManagement.workingColorSpace] - The color space.
  453. * @return {Color} The RGB representation of this color.
  454. */
  455. getRGB( target, colorSpace = ColorManagement.workingColorSpace ) {
  456. ColorManagement.workingToColorSpace( _color.copy( this ), colorSpace );
  457. target.r = _color.r;
  458. target.g = _color.g;
  459. target.b = _color.b;
  460. return target;
  461. }
  462. /**
  463. * Returns the value of this color as a CSS style string. Example: `rgb(255,0,0)`.
  464. *
  465. * @param {string} [colorSpace=SRGBColorSpace] - The color space.
  466. * @return {string} The CSS representation of this color.
  467. */
  468. getStyle( colorSpace = SRGBColorSpace ) {
  469. ColorManagement.workingToColorSpace( _color.copy( this ), colorSpace );
  470. const r = _color.r, g = _color.g, b = _color.b;
  471. if ( colorSpace !== SRGBColorSpace ) {
  472. // Requires CSS Color Module Level 4 (https://www.w3.org/TR/css-color-4/).
  473. return `color(${ colorSpace } ${ r.toFixed( 3 ) } ${ g.toFixed( 3 ) } ${ b.toFixed( 3 ) })`;
  474. }
  475. return `rgb(${ Math.round( r * 255 ) },${ Math.round( g * 255 ) },${ Math.round( b * 255 ) })`;
  476. }
  477. /**
  478. * Adds the given HSL values to this color's values.
  479. * Internally, this converts the color's RGB values to HSL, adds HSL
  480. * and then converts the color back to RGB.
  481. *
  482. * @param {number} h - Hue value between `0.0` and `1.0`.
  483. * @param {number} s - Saturation value between `0.0` and `1.0`.
  484. * @param {number} l - Lightness value between `0.0` and `1.0`.
  485. * @return {Color} A reference to this color.
  486. */
  487. offsetHSL( h, s, l ) {
  488. this.getHSL( _hslA );
  489. return this.setHSL( _hslA.h + h, _hslA.s + s, _hslA.l + l );
  490. }
  491. /**
  492. * Adds the RGB values of the given color to the RGB values of this color.
  493. *
  494. * @param {Color} color - The color to add.
  495. * @return {Color} A reference to this color.
  496. */
  497. add( color ) {
  498. this.r += color.r;
  499. this.g += color.g;
  500. this.b += color.b;
  501. return this;
  502. }
  503. /**
  504. * Adds the RGB values of the given colors and stores the result in this instance.
  505. *
  506. * @param {Color} color1 - The first color.
  507. * @param {Color} color2 - The second color.
  508. * @return {Color} A reference to this color.
  509. */
  510. addColors( color1, color2 ) {
  511. this.r = color1.r + color2.r;
  512. this.g = color1.g + color2.g;
  513. this.b = color1.b + color2.b;
  514. return this;
  515. }
  516. /**
  517. * Adds the given scalar value to the RGB values of this color.
  518. *
  519. * @param {number} s - The scalar to add.
  520. * @return {Color} A reference to this color.
  521. */
  522. addScalar( s ) {
  523. this.r += s;
  524. this.g += s;
  525. this.b += s;
  526. return this;
  527. }
  528. /**
  529. * Subtracts the RGB values of the given color from the RGB values of this color.
  530. *
  531. * @param {Color} color - The color to subtract.
  532. * @return {Color} A reference to this color.
  533. */
  534. sub( color ) {
  535. this.r = Math.max( 0, this.r - color.r );
  536. this.g = Math.max( 0, this.g - color.g );
  537. this.b = Math.max( 0, this.b - color.b );
  538. return this;
  539. }
  540. /**
  541. * Multiplies the RGB values of the given color with the RGB values of this color.
  542. *
  543. * @param {Color} color - The color to multiply.
  544. * @return {Color} A reference to this color.
  545. */
  546. multiply( color ) {
  547. this.r *= color.r;
  548. this.g *= color.g;
  549. this.b *= color.b;
  550. return this;
  551. }
  552. /**
  553. * Multiplies the given scalar value with the RGB values of this color.
  554. *
  555. * @param {number} s - The scalar to multiply.
  556. * @return {Color} A reference to this color.
  557. */
  558. multiplyScalar( s ) {
  559. this.r *= s;
  560. this.g *= s;
  561. this.b *= s;
  562. return this;
  563. }
  564. /**
  565. * Linearly interpolates this color's RGB values toward the RGB values of the
  566. * given color. The alpha argument can be thought of as the ratio between
  567. * the two colors, where `0.0` is this color and `1.0` is the first argument.
  568. *
  569. * @param {Color} color - The color to converge on.
  570. * @param {number} alpha - The interpolation factor in the closed interval `[0,1]`.
  571. * @return {Color} A reference to this color.
  572. */
  573. lerp( color, alpha ) {
  574. this.r += ( color.r - this.r ) * alpha;
  575. this.g += ( color.g - this.g ) * alpha;
  576. this.b += ( color.b - this.b ) * alpha;
  577. return this;
  578. }
  579. /**
  580. * Linearly interpolates between the given colors and stores the result in this instance.
  581. * The alpha argument can be thought of as the ratio between the two colors, where `0.0`
  582. * is the first and `1.0` is the second color.
  583. *
  584. * @param {Color} color1 - The first color.
  585. * @param {Color} color2 - The second color.
  586. * @param {number} alpha - The interpolation factor in the closed interval `[0,1]`.
  587. * @return {Color} A reference to this color.
  588. */
  589. lerpColors( color1, color2, alpha ) {
  590. this.r = color1.r + ( color2.r - color1.r ) * alpha;
  591. this.g = color1.g + ( color2.g - color1.g ) * alpha;
  592. this.b = color1.b + ( color2.b - color1.b ) * alpha;
  593. return this;
  594. }
  595. /**
  596. * Linearly interpolates this color's HSL values toward the HSL values of the
  597. * given color. It differs from {@link Color#lerp} by not interpolating straight
  598. * from one color to the other, but instead going through all the hues in between
  599. * those two colors. The alpha argument can be thought of as the ratio between
  600. * the two colors, where 0.0 is this color and 1.0 is the first argument.
  601. *
  602. * @param {Color} color - The color to converge on.
  603. * @param {number} alpha - The interpolation factor in the closed interval `[0,1]`.
  604. * @return {Color} A reference to this color.
  605. */
  606. lerpHSL( color, alpha ) {
  607. this.getHSL( _hslA );
  608. color.getHSL( _hslB );
  609. const h = lerp( _hslA.h, _hslB.h, alpha );
  610. const s = lerp( _hslA.s, _hslB.s, alpha );
  611. const l = lerp( _hslA.l, _hslB.l, alpha );
  612. this.setHSL( h, s, l );
  613. return this;
  614. }
  615. /**
  616. * Sets the color's RGB components from the given 3D vector.
  617. *
  618. * @param {Vector3} v - The vector to set.
  619. * @return {Color} A reference to this color.
  620. */
  621. setFromVector3( v ) {
  622. this.r = v.x;
  623. this.g = v.y;
  624. this.b = v.z;
  625. return this;
  626. }
  627. /**
  628. * Transforms this color with the given 3x3 matrix.
  629. *
  630. * @param {Matrix3} m - The matrix.
  631. * @return {Color} A reference to this color.
  632. */
  633. applyMatrix3( m ) {
  634. const r = this.r, g = this.g, b = this.b;
  635. const e = m.elements;
  636. this.r = e[ 0 ] * r + e[ 3 ] * g + e[ 6 ] * b;
  637. this.g = e[ 1 ] * r + e[ 4 ] * g + e[ 7 ] * b;
  638. this.b = e[ 2 ] * r + e[ 5 ] * g + e[ 8 ] * b;
  639. return this;
  640. }
  641. /**
  642. * Returns `true` if this color is equal with the given one.
  643. *
  644. * @param {Color} c - The color to test for equality.
  645. * @return {boolean} Whether this bounding color is equal with the given one.
  646. */
  647. equals( c ) {
  648. return ( c.r === this.r ) && ( c.g === this.g ) && ( c.b === this.b );
  649. }
  650. /**
  651. * Sets this color's RGB components from the given array.
  652. *
  653. * @param {Array<number>} array - An array holding the RGB values.
  654. * @param {number} [offset=0] - The offset into the array.
  655. * @return {Color} A reference to this color.
  656. */
  657. fromArray( array, offset = 0 ) {
  658. this.r = array[ offset ];
  659. this.g = array[ offset + 1 ];
  660. this.b = array[ offset + 2 ];
  661. return this;
  662. }
  663. /**
  664. * Writes the RGB components of this color to the given array. If no array is provided,
  665. * the method returns a new instance.
  666. *
  667. * @param {Array<number>} [array=[]] - The target array holding the color components.
  668. * @param {number} [offset=0] - Index of the first element in the array.
  669. * @return {Array<number>} The color components.
  670. */
  671. toArray( array = [], offset = 0 ) {
  672. array[ offset ] = this.r;
  673. array[ offset + 1 ] = this.g;
  674. array[ offset + 2 ] = this.b;
  675. return array;
  676. }
  677. /**
  678. * Sets the components of this color from the given buffer attribute.
  679. *
  680. * @param {BufferAttribute} attribute - The buffer attribute holding color data.
  681. * @param {number} index - The index into the attribute.
  682. * @return {Color} A reference to this color.
  683. */
  684. fromBufferAttribute( attribute, index ) {
  685. this.r = attribute.getX( index );
  686. this.g = attribute.getY( index );
  687. this.b = attribute.getZ( index );
  688. return this;
  689. }
  690. /**
  691. * This methods defines the serialization result of this class. Returns the color
  692. * as a hexadecimal value.
  693. *
  694. * @return {number} The hexadecimal value.
  695. */
  696. toJSON() {
  697. return this.getHex();
  698. }
  699. *[ Symbol.iterator ]() {
  700. yield this.r;
  701. yield this.g;
  702. yield this.b;
  703. }
  704. }
  705. const _color = /*@__PURE__*/ new Color();
  706. /**
  707. * A dictionary with X11 color names.
  708. *
  709. * Note that multiple words such as Dark Orange become the string 'darkorange'.
  710. *
  711. * @static
  712. * @type {Object}
  713. */
  714. Color.NAMES = _colorKeywords;
  715. export { Color };
粤ICP备19079148号