Color.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <base href="../../../" />
  6. <script src="page.js"></script>
  7. <link type="text/css" rel="stylesheet" href="page.css" />
  8. </head>
  9. <body>
  10. <h1>[name]</h1>
  11. <p class="desc">Class representing a color.</p>
  12. <p>
  13. A Color instance is represented by RGB components in the linear <i>working
  14. color space</i>, which defaults to `LinearSRGBColorSpace`. Inputs
  15. conventionally using `SRGBColorSpace` (such as hexadecimals and CSS
  16. strings) are converted to the working color space automatically.
  17. </p>
  18. <p>
  19. <code>
  20. // converted automatically from SRGBColorSpace to LinearSRGBColorSpace
  21. const color = new THREE.Color().setHex( 0x112233 );
  22. </code>
  23. </p>
  24. <p>
  25. Source color spaces may be specified explicitly, to ensure correct
  26. conversions.
  27. </p>
  28. <p>
  29. <code>
  30. // assumed already LinearSRGBColorSpace; no conversion
  31. const color = new THREE.Color().setRGB( 0.5, 0.5, 0.5 );
  32. // converted explicitly from SRGBColorSpace to LinearSRGBColorSpace
  33. const color = new THREE.Color().setRGB( 0.5, 0.5, 0.5, SRGBColorSpace );
  34. </code>
  35. </p>
  36. <p>
  37. If THREE.ColorManagement is disabled, no conversions occur. For details,
  38. see <i>Color management</i>.
  39. </p>
  40. <p>
  41. Iterating through a Color instance will yield its components (r, g, b) in
  42. the corresponding order.
  43. </p>
  44. <h2>Code Examples</h2>
  45. <p>A Color can be initialised in any of the following ways:</p>
  46. <code>
  47. //empty constructor - will default white
  48. const color1 = new THREE.Color();
  49. //Hexadecimal color (recommended)
  50. const color2 = new THREE.Color( 0xff0000 );
  51. //RGB string
  52. const color3 = new THREE.Color("rgb(255, 0, 0)");
  53. const color4 = new THREE.Color("rgb(100%, 0%, 0%)");
  54. //X11 color name - all 140 color names are supported.
  55. //Note the lack of CamelCase in the name
  56. const color5 = new THREE.Color( 'skyblue' );
  57. //HSL string
  58. const color6 = new THREE.Color("hsl(0, 100%, 50%)");
  59. //Separate RGB values between 0 and 1
  60. const color7 = new THREE.Color( 1, 0, 0 );
  61. </code>
  62. <h2>Constructor</h2>
  63. <h3>
  64. [name]( [param:Color_Hex_or_String r], [param:Float g], [param:Float b] )
  65. </h3>
  66. <p>
  67. [page:Color_Hex_or_String r] - (optional) If arguments [page:Float g] and
  68. [page:Float b] are defined, the red component of the color. If they are
  69. not defined, it can be a
  70. [link:https://en.wikipedia.org/wiki/Web_colors#Hex_triplet hexadecimal triplet] (recommended),
  71. a CSS-style string, or another `Color` instance.<br />
  72. [page:Float g] - (optional) If it is defined, the green component of the
  73. color.<br />
  74. [page:Float b] - (optional) If it is defined, the blue component of the
  75. color.<br /><br />
  76. Note that standard method of specifying color in three.js is with a
  77. [link:https://en.wikipedia.org/wiki/Web_colors#Hex_triplet hexadecimal triplet],
  78. and that method is used throughout the rest of the
  79. documentation.<br /><br />
  80. When all arguments are defined then [page:Color_Hex_or_String r] is the
  81. red component, [page:Float g] is the green component and [page:Float b] is
  82. the blue component of the color.<br />
  83. When only [page:Color_Hex_or_String r] is defined:<br />
  84. </p>
  85. <ul>
  86. <li>
  87. It can be a [link:https://en.wikipedia.org/wiki/Web_colors#Hex_triplet hexadecimal triplet] representing the color (recommended).
  88. </li>
  89. <li>It can be an another Color instance.</li>
  90. <li>
  91. It can be a CSS-style string. For example:
  92. <ul>
  93. <li>'rgb(250, 0,0)'</li>
  94. <li>'rgb(100%,0%,0%)'</li>
  95. <li>'hsl(0, 100%, 50%)'</li>
  96. <li>'#ff0000'</li>
  97. <li>'#f00'</li>
  98. <li>'red'</li>
  99. </ul>
  100. </li>
  101. </ul>
  102. <h2>Properties</h2>
  103. <h3>[property:Boolean isColor]</h3>
  104. <p>Read-only flag to check if a given object is of type [name].</p>
  105. <h3>[property:Float r]</h3>
  106. <p>Red channel value between `0.0` and `1.0`. Default is `1`.</p>
  107. <h3>[property:Float g]</h3>
  108. <p>Green channel value between `0.0` and `1.0`. Default is `1`.</p>
  109. <h3>[property:Float b]</h3>
  110. <p>Blue channel value between `0.0` and `1.0`. Default is `1`.</p>
  111. <h2>Methods</h2>
  112. <h3>[method:this add]( [param:Color color] )</h3>
  113. <p>
  114. Adds the RGB values of [page:Color color] to the RGB values of this color.
  115. </p>
  116. <h3>
  117. [method:this addColors]( [param:Color color1], [param:Color color2] )
  118. </h3>
  119. <p>
  120. Sets this color's RGB values to the sum of the RGB values of [page:Color color1] and [page:Color color2].
  121. </p>
  122. <h3>[method:this addScalar]( [param:Number s] )</h3>
  123. <p>Adds [page:Number s] to the RGB values of this color.</p>
  124. <h3>[method:this applyMatrix3]( [param:Matrix3 m] )</h3>
  125. <p>
  126. Applies the transform [page:Matrix3 m] to this color's RGB components.
  127. </p>
  128. <h3>[method:Color clone]()</h3>
  129. <p>
  130. Returns a new Color with the same [page:.r r], [page:.g g] and [page:.b b]
  131. values as this one.
  132. </p>
  133. <h3>[method:this copy]( [param:Color color] )</h3>
  134. <p>
  135. Copies the [page:.r r], [page:.g g] and [page:.b b] parameters from
  136. [page:Color color] in to this color.
  137. </p>
  138. <h3>[method:this convertLinearToSRGB]()</h3>
  139. <p>Converts this color from `LinearSRGBColorSpace` to `SRGBColorSpace`.</p>
  140. <h3>[method:this convertSRGBToLinear]()</h3>
  141. <p>Converts this color from `SRGBColorSpace` to `LinearSRGBColorSpace`.</p>
  142. <h3>[method:this copyLinearToSRGB]( [param:Color color] )</h3>
  143. <p>
  144. [page:Color color] — Color to copy.<br />
  145. Copies the given color into this color, and then converts this color from
  146. `LinearSRGBColorSpace` to `SRGBColorSpace`.
  147. </p>
  148. <h3>[method:this copySRGBToLinear]( [param:Color color] )</h3>
  149. <p>
  150. [page:Color color] — Color to copy.<br />
  151. Copies the given color into this color, and then converts this color from
  152. `SRGBColorSpace` to `LinearSRGBColorSpace`.
  153. </p>
  154. <h3>[method:Boolean equals]( [param:Color color] )</h3>
  155. <p>
  156. Compares the RGB values of [page:Color color] with those of this object.
  157. Returns true if they are the same, false otherwise.
  158. </p>
  159. <h3>
  160. [method:this fromArray]( [param:Array array], [param:Integer offset] )
  161. </h3>
  162. <p>
  163. [page:Array array] - [page:Array] of floats in the form [ [page:Float r],
  164. [page:Float g], [page:Float b] ].<br />
  165. [page:Integer offset] - An optional offset into the array.<br /><br />
  166. Sets this color's components based on an array formatted like [
  167. [page:Float r], [page:Float g], [page:Float b] ].
  168. </p>
  169. <h3>
  170. [method:this fromBufferAttribute]( [param:BufferAttribute attribute], [param:Integer index] )
  171. </h3>
  172. <p>
  173. [page:BufferAttribute attribute] - the source attribute.<br />
  174. [page:Integer index] - index in the attribute.<br /><br />
  175. Sets this color's components from the [page:BufferAttribute attribute].
  176. </p>
  177. <h3>
  178. [method:Integer getHex]( [param:string colorSpace] = SRGBColorSpace )
  179. </h3>
  180. <p>Returns the hexadecimal value of this color.</p>
  181. <h3>
  182. [method:String getHexString]( [param:string colorSpace] = SRGBColorSpace )
  183. </h3>
  184. <p>
  185. Returns the hexadecimal value of this color as a string (for example,
  186. 'FFFFFF').
  187. </p>
  188. <h3>
  189. [method:Object getHSL]( [param:Object target], [param:string colorSpace] = LinearSRGBColorSpace )
  190. </h3>
  191. <p>
  192. [page:Object target] — the result will be copied into this Object. Adds h,
  193. s and l keys to the object (if not already present).<br /><br />
  194. Convert this Color's [page:.r r], [page:.g g] and [page:.b b] values to
  195. [link:https://en.wikipedia.org/wiki/HSL_and_HSV HSL] format and returns an
  196. object of the form:
  197. <code>
  198. {
  199. h: 0,
  200. s: 0,
  201. l: 0
  202. }
  203. </code>
  204. </p>
  205. <h3>
  206. [method:Color getRGB]( [param:Color target], [param:string colorSpace] = LinearSRGBColorSpace )
  207. </h3>
  208. <p>
  209. [page:Color target] — the result will be copied into this object.<br /><br />
  210. Returns the RGB values of this color as an instance of [page:Color].
  211. </p>
  212. <h3>
  213. [method:String getStyle]( [param:string colorSpace] = SRGBColorSpace )
  214. </h3>
  215. <p>
  216. Returns the value of this color as a CSS style string. Example:
  217. `rgb(255,0,0)`.
  218. </p>
  219. <h3>[method:this lerp]( [param:Color color], [param:Float alpha] )</h3>
  220. <p>
  221. [page:Color color] - color to converge on.<br />
  222. [page:Float alpha] - interpolation factor in the closed interval `[0,
  223. 1]`.<br /><br />
  224. Linearly interpolates this color's RGB values toward the RGB values of the
  225. passed argument. The alpha argument can be thought of as the ratio between
  226. the two colors, where `0.0` is this color and `1.0` is the first argument.
  227. </p>
  228. <h3>
  229. [method:this lerpColors]( [param:Color color1], [param:Color color2], [param:Float alpha] )
  230. </h3>
  231. <p>
  232. [page:Color color1] - the starting [page:Color].<br />
  233. [page:Color color2] - [page:Color] to interpolate towards.<br />
  234. [page:Float alpha] - interpolation factor, typically in the closed
  235. interval `[0, 1]`.<br /><br />
  236. Sets this color to be the color linearly interpolated between [page:Color color1]
  237. and [page:Color color2] where alpha is the percent distance along
  238. the line connecting the two colors - alpha = 0 will be [page:Color color1],
  239. and alpha = 1 will be [page:Color color2].
  240. </p>
  241. <h3>[method:this lerpHSL]( [param:Color color], [param:Float alpha] )</h3>
  242. <p>
  243. [page:Color color] - color to converge on.<br />
  244. [page:Float alpha] - interpolation factor in the closed interval `[0,
  245. 1]`.<br /><br />
  246. Linearly interpolates this color's HSL values toward the HSL values of the
  247. passed argument. It differs from the classic [page:.lerp] by not
  248. interpolating straight from one color to the other, but instead going
  249. through all the hues in between those two colors. The alpha argument can
  250. be thought of as the ratio between the two colors, where 0.0 is this color
  251. and 1.0 is the first argument.
  252. </p>
  253. <h3>[method:this multiply]( [param:Color color] )</h3>
  254. <p>
  255. Multiplies this color's RGB values by the given [page:Color color]'s RGB
  256. values.
  257. </p>
  258. <h3>[method:this multiplyScalar]( [param:Number s] )</h3>
  259. <p>Multiplies this color's RGB values by [page:Number s].</p>
  260. <h3>
  261. [method:this offsetHSL]( [param:Float h], [param:Float s], [param:Float l] )
  262. </h3>
  263. <p>
  264. Adds the given [page:Float h], [page:Float s], and [page:Float l] to this
  265. color's values. Internally, this converts the color's [page:.r r],
  266. [page:.g g] and [page:.b b] values to HSL, adds [page:Float h],
  267. [page:Float s], and [page:Float l], and then converts the color back to
  268. RGB.
  269. </p>
  270. <h3>[method:this set]( [param:Color_Hex_or_String r], [param:Float g], [param:Float b] )</h3>
  271. <p>
  272. [page:Color_Hex_or_String r] - (optional) If arguments [page:Float g] and [page:Float b] are defined, the red component of the color. If they are
  273. not defined, it can be a [link:https://en.wikipedia.org/wiki/Web_colors#Hex_triplet hexadecimal triplet] (recommended), a CSS-style string, or another `Color` instance.<br />
  274. [page:Float g] - (optional) If it is defined, the green component of the color.<br />
  275. [page:Float b] - (optional) If it is defined, the blue component of the color.<br /><br />
  276. See the Constructor above for full details about possible arguments. Delegates to [page:.copy],
  277. [page:.setStyle], [page:.setRGB] or [page:.setHex] depending on input type.
  278. </p>
  279. <h3>[method:this setFromVector3]( [param:Vector3 vector] )</h3>
  280. <p>
  281. Sets this colors's [page:.r r], [page:.g g] and [page:.b b] components
  282. from the x, y, and z components of the specified [page:Vector3 vector].
  283. </p>
  284. <h3>
  285. [method:this setHex]( [param:Integer hex], [param:string colorSpace] = SRGBColorSpace )
  286. </h3>
  287. <p>
  288. [page:Integer hex] —
  289. [link:https://en.wikipedia.org/wiki/Web_colors#Hex_triplet hexadecimal triplet] format.<br /><br />
  290. Sets this color from a hexadecimal value.
  291. </p>
  292. <h3>
  293. [method:this setHSL]( [param:Float h], [param:Float s], [param:Float l], [param:string colorSpace] = LinearSRGBColorSpace )
  294. </h3>
  295. <p>
  296. [page:Float h] — hue value between `0.0` and `1.0` <br />
  297. [page:Float s] — saturation value between `0.0` and `1.0` <br />
  298. [page:Float l] — lightness value between `0.0` and `1.0`<br /><br />
  299. Sets color from HSL values.
  300. </p>
  301. <h3>
  302. [method:this setRGB]( [param:Float r], [param:Float g], [param:Float b], [param:string colorSpace] = LinearSRGBColorSpace )
  303. </h3>
  304. <p>
  305. [page:Float r] — Red channel value between `0.0` and `1.0`.<br />
  306. [page:Float g] — Green channel value between `0.0` and `1.0`.<br />
  307. [page:Float b] — Blue channel value between `0.0` and `1.0`.<br /><br />
  308. Sets this color from RGB values.
  309. </p>
  310. <h3>[method:this setScalar]( [param:Float scalar] )</h3>
  311. <p>
  312. [page:Float scalar] — a value between `0.0` and `1.0`.<br /><br />
  313. Sets all three color components to the value [page:Float scalar].
  314. </p>
  315. <h3>
  316. [method:this setStyle]( [param:String style], [param:string colorSpace] = SRGBColorSpace )
  317. </h3>
  318. <p>
  319. [page:String style] — color as a CSS-style string.<br /><br />
  320. Sets this color from a CSS-style string. For example, "rgb(250, 0,0)",
  321. "rgb(100%, 0%, 0%)", "hsl(0, 100%, 50%)", "#ff0000", "#f00", or "red" ( or
  322. any [link:https://en.wikipedia.org/wiki/X11_color_names#Color_name_chart X11 color name] -
  323. all 140 color names are supported ).<br />
  324. Translucent colors such as "rgba(255, 0, 0, 0.5)" and "hsla(0, 100%, 50%,
  325. 0.5)" are also accepted, but the alpha-channel coordinate will be
  326. discarded.<br /><br />
  327. Note that for X11 color names, multiple words such as Dark Orange become
  328. the string 'darkorange'.
  329. </p>
  330. <h3>
  331. [method:this setColorName]( [param:String style], [param:string colorSpace] = SRGBColorSpace )
  332. </h3>
  333. <p>
  334. [page:String style] — color name ( from
  335. [link:https://en.wikipedia.org/wiki/X11_color_names#Color_name_chart X11 color names] ).<br /><br />
  336. Sets this color from a color name. Faster than [page:.setStyle] method if
  337. you don't need the other CSS-style formats.<br /><br />
  338. For convenience, the list of names is exposed in Color.NAMES as a hash:
  339. <code>
  340. Color.NAMES.aliceblue // returns 0xF0F8FF
  341. </code>
  342. </p>
  343. <h3>[method:this sub]( [param:Color color] )</h3>
  344. <p>
  345. Subtracts the RGB components of the given color from the RGB components of
  346. this color. If this results in a negative component, that component is set
  347. to zero.
  348. </p>
  349. <h3>
  350. [method:Array toArray]( [param:Array array], [param:Integer offset] )
  351. </h3>
  352. <p>
  353. [page:Array array] - An optional array to store the color to. <br />
  354. [page:Integer offset] - An optional offset into the array.<br /><br />
  355. Returns an array of the form [ r, g, b ].
  356. </p>
  357. <h3>[method:Number toJSON]()</h3>
  358. <p>
  359. This methods defines the serialization result of [name]. Returns the color
  360. as a hexadecimal value.
  361. </p>
  362. <h2>Source</h2>
  363. <p>
  364. [link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
  365. </p>
  366. </body>
  367. </html>
粤ICP备19079148号