Color.tests.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /* global QUnit */
  2. import { Color } from '../../../../src/math/Color.js';
  3. import { ColorManagement } from '../../../../src/math/ColorManagement.js';
  4. import { eps } from '../../utils/math-constants.js';
  5. import { CONSOLE_LEVEL } from '../../utils/console-wrapper.js';
  6. import { SRGBColorSpace } from '../../../../src/constants.js';
  7. export default QUnit.module( 'Maths', () => {
  8. QUnit.module( 'Color', () => {
  9. const colorManagementEnabled = ColorManagement.enabled;
  10. QUnit.testDone( () => {
  11. ColorManagement.enabled = colorManagementEnabled;
  12. } );
  13. // INSTANCING
  14. QUnit.test( 'Instancing', ( assert ) => {
  15. ColorManagement.enabled = false; // TODO: Update and enable.
  16. // default ctor
  17. let c = new Color();
  18. assert.ok( c.r, 'Red: ' + c.r );
  19. assert.ok( c.g, 'Green: ' + c.g );
  20. assert.ok( c.b, 'Blue: ' + c.b );
  21. // rgb ctor
  22. c = new Color( 1, 1, 1 );
  23. assert.ok( c.r == 1, 'Passed' );
  24. assert.ok( c.g == 1, 'Passed' );
  25. assert.ok( c.b == 1, 'Passed' );
  26. } );
  27. // EXPOSED CONSTANTS
  28. QUnit.test( 'Color.NAMES', ( assert ) => {
  29. ColorManagement.enabled = false; // TODO: Update and enable.
  30. assert.ok( Color.NAMES.aliceblue == 0xF0F8FF, 'Exposed Color.NAMES' );
  31. } );
  32. // PUBLIC STUFF
  33. QUnit.test( 'isColor', ( assert ) => {
  34. ColorManagement.enabled = false; // TODO: Update and enable.
  35. const a = new Color();
  36. assert.ok( a.isColor === true, 'Passed!' );
  37. const b = new Object();
  38. assert.ok( ! b.isColor, 'Passed!' );
  39. } );
  40. QUnit.test( 'set', ( assert ) => {
  41. ColorManagement.enabled = false; // TODO: Update and enable.
  42. const a = new Color();
  43. const b = new Color( 0.5, 0, 0 );
  44. const c = new Color( 0xFF0000 );
  45. const d = new Color( 0, 1.0, 0 );
  46. const e = new Color( 0.5, 0.5, 0.5 );
  47. a.set( b );
  48. assert.ok( a.equals( b ), 'Set with Color instance' );
  49. a.set( 0xFF0000 );
  50. assert.ok( a.equals( c ), 'Set with number' );
  51. a.set( 'rgb(0,255,0)' );
  52. assert.ok( a.equals( d ), 'Set with style' );
  53. a.set( 0.5, 0.5, 0.5 );
  54. assert.ok( a.equals( e ), 'Set with r,g,b components' );
  55. } );
  56. QUnit.test( 'setScalar', ( assert ) => {
  57. ColorManagement.enabled = false; // TODO: Update and enable.
  58. const c = new Color();
  59. c.setScalar( 0.5 );
  60. assert.ok( c.r == 0.5, 'Red: ' + c.r );
  61. assert.ok( c.g == 0.5, 'Green: ' + c.g );
  62. assert.ok( c.b == 0.5, 'Blue: ' + c.b );
  63. } );
  64. QUnit.test( 'setHex', ( assert ) => {
  65. ColorManagement.enabled = false; // TODO: Update and enable.
  66. const c = new Color();
  67. c.setHex( 0xFA8072 );
  68. assert.ok( c.getHex() == 0xFA8072, 'Hex: ' + c.getHex() );
  69. assert.ok( c.r == 0xFA / 0xFF, 'Red: ' + c.r );
  70. assert.ok( c.g == 0x80 / 0xFF, 'Green: ' + c.g );
  71. assert.ok( c.b == 0x72 / 0xFF, 'Blue: ' + c.b );
  72. } );
  73. QUnit.test( 'setRGB', ( assert ) => {
  74. ColorManagement.enabled = true;
  75. const c = new Color();
  76. c.setRGB( 0.3, 0.5, 0.7 );
  77. assert.equal( c.r, 0.3, 'Red: ' + c.r + ' (srgb-linear)' );
  78. assert.equal( c.g, 0.5, 'Green: ' + c.g + ' (srgb-linear)' );
  79. assert.equal( c.b, 0.7, 'Blue: ' + c.b + ' (srgb-linear)' );
  80. c.setRGB( 0.3, 0.5, 0.7, SRGBColorSpace );
  81. assert.equal( c.r.toFixed( 3 ), 0.073, 'Red: ' + c.r + ' (srgb)' );
  82. assert.equal( c.g.toFixed( 3 ), 0.214, 'Green: ' + c.g + ' (srgb)' );
  83. assert.equal( c.b.toFixed( 3 ), 0.448, 'Blue: ' + c.b + ' (srgb)' );
  84. } );
  85. QUnit.test( 'setHSL', ( assert ) => {
  86. ColorManagement.enabled = false; // TODO: Update and enable.
  87. const c = new Color();
  88. const hsl = { h: 0, s: 0, l: 0 };
  89. c.setHSL( 0.75, 1.0, 0.25 );
  90. c.getHSL( hsl );
  91. assert.ok( hsl.h == 0.75, 'hue: ' + hsl.h );
  92. assert.ok( hsl.s == 1.00, 'saturation: ' + hsl.s );
  93. assert.ok( hsl.l == 0.25, 'lightness: ' + hsl.l );
  94. } );
  95. QUnit.test( 'setStyle', ( assert ) => {
  96. ColorManagement.enabled = false; // TODO: Update and enable.
  97. const a = new Color();
  98. let b = new Color( 8 / 255, 25 / 255, 178 / 255 );
  99. a.setStyle( 'rgb(8,25,178)' );
  100. assert.ok( a.equals( b ), 'Passed' );
  101. b = new Color( 8 / 255, 25 / 255, 178 / 255 );
  102. a.setStyle( 'rgba(8,25,178,200)' );
  103. assert.ok( a.equals( b ), 'Passed' );
  104. let hsl = { h: 0, s: 0, l: 0 };
  105. a.setStyle( 'hsl(270,50%,75%)' );
  106. a.getHSL( hsl );
  107. assert.ok( hsl.h == 0.75, 'hue: ' + hsl.h );
  108. assert.ok( hsl.s == 0.5, 'saturation: ' + hsl.s );
  109. assert.ok( hsl.l == 0.75, 'lightness: ' + hsl.l );
  110. hsl = { h: 0, s: 0, l: 0 };
  111. a.setStyle( 'hsl(270,50%,75%)' );
  112. a.getHSL( hsl );
  113. assert.ok( hsl.h == 0.75, 'hue: ' + hsl.h );
  114. assert.ok( hsl.s == 0.5, 'saturation: ' + hsl.s );
  115. assert.ok( hsl.l == 0.75, 'lightness: ' + hsl.l );
  116. a.setStyle( '#F8A' );
  117. assert.ok( a.r == 0xFF / 255, 'Red: ' + a.r );
  118. assert.ok( a.g == 0x88 / 255, 'Green: ' + a.g );
  119. assert.ok( a.b == 0xAA / 255, 'Blue: ' + a.b );
  120. a.setStyle( '#F8ABC1' );
  121. assert.ok( a.r == 0xF8 / 255, 'Red: ' + a.r );
  122. assert.ok( a.g == 0xAB / 255, 'Green: ' + a.g );
  123. assert.ok( a.b == 0xC1 / 255, 'Blue: ' + a.b );
  124. a.setStyle( 'aliceblue' );
  125. assert.ok( a.r == 0xF0 / 255, 'Red: ' + a.r );
  126. assert.ok( a.g == 0xF8 / 255, 'Green: ' + a.g );
  127. assert.ok( a.b == 0xFF / 255, 'Blue: ' + a.b );
  128. } );
  129. QUnit.test( 'setColorName', ( assert ) => {
  130. ColorManagement.enabled = false; // TODO: Update and enable.
  131. const c = new Color();
  132. const res = c.setColorName( 'aliceblue' );
  133. assert.ok( c.getHex() == 0xF0F8FF, 'Hex: ' + c.getHex() );
  134. assert.ok( c == res, 'Returns Self' );
  135. } );
  136. QUnit.test( 'clone', ( assert ) => {
  137. ColorManagement.enabled = false; // TODO: Update and enable.
  138. const c = new Color( 'teal' );
  139. const c2 = c.clone();
  140. assert.ok( c2.getHex() == 0x008080, 'Hex c2: ' + c2.getHex() );
  141. } );
  142. QUnit.test( 'copy', ( assert ) => {
  143. ColorManagement.enabled = false; // TODO: Update and enable.
  144. const a = new Color( 'teal' );
  145. const b = new Color();
  146. b.copy( a );
  147. assert.ok( b.r == 0x00 / 255, 'Red: ' + b.r );
  148. assert.ok( b.g == 0x80 / 255, 'Green: ' + b.g );
  149. assert.ok( b.b == 0x80 / 255, 'Blue: ' + b.b );
  150. } );
  151. QUnit.test( 'copySRGBToLinear', ( assert ) => {
  152. ColorManagement.enabled = false; // TODO: Update and enable.
  153. const c = new Color();
  154. const c2 = new Color();
  155. c2.setRGB( 0.3, 0.5, 0.9 );
  156. c.copySRGBToLinear( c2 );
  157. assert.numEqual( c.r, 0.09, 'Red c: ' + c.r + ' Red c2: ' + c2.r );
  158. assert.numEqual( c.g, 0.25, 'Green c: ' + c.g + ' Green c2: ' + c2.g );
  159. assert.numEqual( c.b, 0.81, 'Blue c: ' + c.b + ' Blue c2: ' + c2.b );
  160. } );
  161. QUnit.test( 'copyLinearToSRGB', ( assert ) => {
  162. ColorManagement.enabled = false; // TODO: Update and enable.
  163. const c = new Color();
  164. const c2 = new Color();
  165. c2.setRGB( 0.09, 0.25, 0.81 );
  166. c.copyLinearToSRGB( c2 );
  167. assert.numEqual( c.r, 0.3, 'Red c: ' + c.r + ' Red c2: ' + c2.r );
  168. assert.numEqual( c.g, 0.5, 'Green c: ' + c.g + ' Green c2: ' + c2.g );
  169. assert.numEqual( c.b, 0.9, 'Blue c: ' + c.b + ' Blue c2: ' + c2.b );
  170. } );
  171. QUnit.test( 'convertSRGBToLinear', ( assert ) => {
  172. ColorManagement.enabled = false; // TODO: Update and enable.
  173. const c = new Color();
  174. c.setRGB( 0.3, 0.5, 0.9 );
  175. c.convertSRGBToLinear();
  176. assert.numEqual( c.r, 0.09, 'Red: ' + c.r );
  177. assert.numEqual( c.g, 0.25, 'Green: ' + c.g );
  178. assert.numEqual( c.b, 0.81, 'Blue: ' + c.b );
  179. } );
  180. QUnit.test( 'convertLinearToSRGB', ( assert ) => {
  181. ColorManagement.enabled = false; // TODO: Update and enable.
  182. const c = new Color();
  183. c.setRGB( 4, 9, 16 );
  184. c.convertLinearToSRGB();
  185. assert.numEqual( c.r, 1.82, 'Red: ' + c.r );
  186. assert.numEqual( c.g, 2.58, 'Green: ' + c.g );
  187. assert.numEqual( c.b, 3.29, 'Blue: ' + c.b );
  188. } );
  189. QUnit.test( 'getHex', ( assert ) => {
  190. ColorManagement.enabled = false; // TODO: Update and enable.
  191. const c = new Color( 'red' );
  192. const res = c.getHex();
  193. assert.ok( res == 0xFF0000, 'Hex: ' + res );
  194. } );
  195. QUnit.test( 'getHexString', ( assert ) => {
  196. ColorManagement.enabled = false; // TODO: Update and enable.
  197. const c = new Color( 'tomato' );
  198. const res = c.getHexString();
  199. assert.ok( res == 'ff6347', 'Hex: ' + res );
  200. } );
  201. QUnit.test( 'getHSL', ( assert ) => {
  202. ColorManagement.enabled = false; // TODO: Update and enable.
  203. const c = new Color( 0x80ffff );
  204. const hsl = { h: 0, s: 0, l: 0 };
  205. c.getHSL( hsl );
  206. assert.ok( hsl.h == 0.5, 'hue: ' + hsl.h );
  207. assert.ok( hsl.s == 1.0, 'saturation: ' + hsl.s );
  208. assert.ok( ( Math.round( parseFloat( hsl.l ) * 100 ) / 100 ) == 0.75, 'lightness: ' + hsl.l );
  209. } );
  210. QUnit.test( 'getRGB', ( assert ) => {
  211. ColorManagement.enabled = true;
  212. const c = new Color( 'plum' );
  213. const t = { r: 0, g: 0, b: 0 };
  214. c.getRGB( t );
  215. assert.equal( t.r.toFixed( 3 ), 0.723, 'r (srgb-linear)' );
  216. assert.equal( t.g.toFixed( 3 ), 0.352, 'g (srgb-linear)' );
  217. assert.equal( t.b.toFixed( 3 ), 0.723, 'b (srgb-linear)' );
  218. c.getRGB( t, SRGBColorSpace );
  219. assert.equal( t.r.toFixed( 3 ), ( 221 / 255 ).toFixed( 3 ), 'r (srgb)' );
  220. assert.equal( t.g.toFixed( 3 ), ( 160 / 255 ).toFixed( 3 ), 'g (srgb)' );
  221. assert.equal( t.b.toFixed( 3 ), ( 221 / 255 ).toFixed( 3 ), 'b (srgb)' );
  222. } );
  223. QUnit.test( 'getStyle', ( assert ) => {
  224. ColorManagement.enabled = true;
  225. const c = new Color( 'plum' );
  226. assert.equal( c.getStyle(), 'rgb(221,160,221)', 'style: srgb' );
  227. } );
  228. QUnit.test( 'offsetHSL', ( assert ) => {
  229. ColorManagement.enabled = false; // TODO: Update and enable.
  230. const a = new Color( 'hsl(120,50%,50%)' );
  231. const b = new Color( 0.36, 0.84, 0.648 );
  232. a.offsetHSL( 0.1, 0.1, 0.1 );
  233. assert.ok( Math.abs( a.r - b.r ) <= eps, 'Check r' );
  234. assert.ok( Math.abs( a.g - b.g ) <= eps, 'Check g' );
  235. assert.ok( Math.abs( a.b - b.b ) <= eps, 'Check b' );
  236. } );
  237. QUnit.test( 'add', ( assert ) => {
  238. ColorManagement.enabled = false; // TODO: Update and enable.
  239. const a = new Color( 0x0000FF );
  240. const b = new Color( 0xFF0000 );
  241. const c = new Color( 0xFF00FF );
  242. a.add( b );
  243. assert.ok( a.equals( c ), 'Check new value' );
  244. } );
  245. QUnit.test( 'addColors', ( assert ) => {
  246. ColorManagement.enabled = false; // TODO: Update and enable.
  247. const a = new Color( 0x0000FF );
  248. const b = new Color( 0xFF0000 );
  249. const c = new Color( 0xFF00FF );
  250. const d = new Color();
  251. d.addColors( a, b );
  252. assert.ok( d.equals( c ), 'Passed' );
  253. } );
  254. QUnit.test( 'addScalar', ( assert ) => {
  255. ColorManagement.enabled = false; // TODO: Update and enable.
  256. const a = new Color( 0.1, 0.0, 0.0 );
  257. const b = new Color( 0.6, 0.5, 0.5 );
  258. a.addScalar( 0.5 );
  259. assert.ok( a.equals( b ), 'Check new value' );
  260. } );
  261. QUnit.test( 'sub', ( assert ) => {
  262. ColorManagement.enabled = false; // TODO: Update and enable.
  263. const a = new Color( 0x0000CC );
  264. const b = new Color( 0xFF0000 );
  265. const c = new Color( 0x0000AA );
  266. a.sub( b );
  267. assert.strictEqual( a.getHex(), 0xCC, 'Difference too large' );
  268. a.sub( c );
  269. assert.strictEqual( a.getHex(), 0x22, 'Difference fine' );
  270. } );
  271. QUnit.test( 'multiply', ( assert ) => {
  272. ColorManagement.enabled = false; // TODO: Update and enable.
  273. const a = new Color( 1, 0, 0.5 );
  274. const b = new Color( 0.5, 1, 0.5 );
  275. const c = new Color( 0.5, 0, 0.25 );
  276. a.multiply( b );
  277. assert.ok( a.equals( c ), 'Check new value' );
  278. } );
  279. QUnit.test( 'multiplyScalar', ( assert ) => {
  280. ColorManagement.enabled = false; // TODO: Update and enable.
  281. const a = new Color( 0.25, 0, 0.5 );
  282. const b = new Color( 0.5, 0, 1 );
  283. a.multiplyScalar( 2 );
  284. assert.ok( a.equals( b ), 'Check new value' );
  285. } );
  286. QUnit.test( 'lerp', ( assert ) => {
  287. ColorManagement.enabled = false; // TODO: Update and enable.
  288. const c = new Color();
  289. const c2 = new Color();
  290. c.setRGB( 0, 0, 0 );
  291. c.lerp( c2, 0.2 );
  292. assert.ok( c.r == 0.2, 'Red: ' + c.r );
  293. assert.ok( c.g == 0.2, 'Green: ' + c.g );
  294. assert.ok( c.b == 0.2, 'Blue: ' + c.b );
  295. } );
  296. QUnit.todo( 'lerpColors', ( assert ) => {
  297. // lerpColors( color1, color2, alpha )
  298. assert.ok( false, 'everything\'s gonna be alright' );
  299. } );
  300. QUnit.todo( 'lerpHSL', ( assert ) => {
  301. // lerpHSL( color, alpha )
  302. assert.ok( false, 'everything\'s gonna be alright' );
  303. } );
  304. QUnit.test( 'equals', ( assert ) => {
  305. ColorManagement.enabled = false; // TODO: Update and enable.
  306. const a = new Color( 0.5, 0.0, 1.0 );
  307. const b = new Color( 0.5, 1.0, 0.0 );
  308. assert.strictEqual( a.r, b.r, 'Components: r is equal' );
  309. assert.notStrictEqual( a.g, b.g, 'Components: g is not equal' );
  310. assert.notStrictEqual( a.b, b.b, 'Components: b is not equal' );
  311. assert.notOk( a.equals( b ), 'equals(): a not equal b' );
  312. assert.notOk( b.equals( a ), 'equals(): b not equal a' );
  313. a.copy( b );
  314. assert.strictEqual( a.r, b.r, 'Components after copy(): r is equal' );
  315. assert.strictEqual( a.g, b.g, 'Components after copy(): g is equal' );
  316. assert.strictEqual( a.b, b.b, 'Components after copy(): b is equal' );
  317. assert.ok( a.equals( b ), 'equals() after copy(): a equals b' );
  318. assert.ok( b.equals( a ), 'equals() after copy(): b equals a' );
  319. } );
  320. QUnit.test( 'fromArray', ( assert ) => {
  321. ColorManagement.enabled = false; // TODO: Update and enable.
  322. const a = new Color();
  323. const array = [ 0.5, 0.6, 0.7, 0, 1, 0 ];
  324. a.fromArray( array );
  325. assert.strictEqual( a.r, 0.5, 'No offset: check r' );
  326. assert.strictEqual( a.g, 0.6, 'No offset: check g' );
  327. assert.strictEqual( a.b, 0.7, 'No offset: check b' );
  328. a.fromArray( array, 3 );
  329. assert.strictEqual( a.r, 0, 'With offset: check r' );
  330. assert.strictEqual( a.g, 1, 'With offset: check g' );
  331. assert.strictEqual( a.b, 0, 'With offset: check b' );
  332. } );
  333. QUnit.test( 'toArray', ( assert ) => {
  334. ColorManagement.enabled = false; // TODO: Update and enable.
  335. const r = 0.5, g = 1.0, b = 0.0;
  336. const a = new Color( r, g, b );
  337. let array = a.toArray();
  338. assert.strictEqual( array[ 0 ], r, 'No array, no offset: check r' );
  339. assert.strictEqual( array[ 1 ], g, 'No array, no offset: check g' );
  340. assert.strictEqual( array[ 2 ], b, 'No array, no offset: check b' );
  341. array = [];
  342. a.toArray( array );
  343. assert.strictEqual( array[ 0 ], r, 'With array, no offset: check r' );
  344. assert.strictEqual( array[ 1 ], g, 'With array, no offset: check g' );
  345. assert.strictEqual( array[ 2 ], b, 'With array, no offset: check b' );
  346. array = [];
  347. a.toArray( array, 1 );
  348. assert.strictEqual( array[ 0 ], undefined, 'With array and offset: check [0]' );
  349. assert.strictEqual( array[ 1 ], r, 'With array and offset: check r' );
  350. assert.strictEqual( array[ 2 ], g, 'With array and offset: check g' );
  351. assert.strictEqual( array[ 3 ], b, 'With array and offset: check b' );
  352. } );
  353. QUnit.todo( 'fromBufferAttribute', ( assert ) => {
  354. // fromBufferAttribute( attribute, index )
  355. assert.ok( false, 'everything\'s gonna be alright' );
  356. } );
  357. QUnit.test( 'toJSON', ( assert ) => {
  358. ColorManagement.enabled = false; // TODO: Update and enable.
  359. const a = new Color( 0.0, 0.0, 0.0 );
  360. const b = new Color( 0.0, 0.5, 0.0 );
  361. const c = new Color( 1.0, 0.0, 0.0 );
  362. const d = new Color( 1.0, 1.0, 1.0 );
  363. assert.strictEqual( a.toJSON(), 0x000000, 'Check black' );
  364. assert.strictEqual( b.toJSON(), 0x008000, 'Check half-blue' );
  365. assert.strictEqual( c.toJSON(), 0xFF0000, 'Check red' );
  366. assert.strictEqual( d.toJSON(), 0xFFFFFF, 'Check white' );
  367. } );
  368. // OTHERS - FUNCTIONAL
  369. QUnit.test( 'copyHex', ( assert ) => {
  370. ColorManagement.enabled = false; // TODO: Update and enable.
  371. const c = new Color();
  372. const c2 = new Color( 0xF5FFFA );
  373. c.copy( c2 );
  374. assert.ok( c.getHex() == c2.getHex(), 'Hex c: ' + c.getHex() + ' Hex c2: ' + c2.getHex() );
  375. } );
  376. QUnit.test( 'copyColorString', ( assert ) => {
  377. ColorManagement.enabled = false; // TODO: Update and enable.
  378. const c = new Color();
  379. const c2 = new Color( 'ivory' );
  380. c.copy( c2 );
  381. assert.ok( c.getHex() == c2.getHex(), 'Hex c: ' + c.getHex() + ' Hex c2: ' + c2.getHex() );
  382. } );
  383. QUnit.test( 'setWithNum', ( assert ) => {
  384. ColorManagement.enabled = false; // TODO: Update and enable.
  385. const c = new Color();
  386. c.set( 0xFF0000 );
  387. assert.ok( c.r == 1, 'Red: ' + c.r );
  388. assert.ok( c.g === 0, 'Green: ' + c.g );
  389. assert.ok( c.b === 0, 'Blue: ' + c.b );
  390. } );
  391. QUnit.test( 'setWithString', ( assert ) => {
  392. ColorManagement.enabled = false; // TODO: Update and enable.
  393. const c = new Color();
  394. c.set( 'silver' );
  395. assert.ok( c.getHex() == 0xC0C0C0, 'Hex c: ' + c.getHex() );
  396. } );
  397. QUnit.test( 'setStyleRGBRed', ( assert ) => {
  398. ColorManagement.enabled = false; // TODO: Update and enable.
  399. const c = new Color();
  400. c.setStyle( 'rgb(255,0,0)' );
  401. assert.ok( c.r == 1, 'Red: ' + c.r );
  402. assert.ok( c.g === 0, 'Green: ' + c.g );
  403. assert.ok( c.b === 0, 'Blue: ' + c.b );
  404. } );
  405. QUnit.test( 'setStyleRGBARed', ( assert ) => {
  406. ColorManagement.enabled = false; // TODO: Update and enable.
  407. const c = new Color();
  408. console.level = CONSOLE_LEVEL.ERROR;
  409. c.setStyle( 'rgba(255,0,0,0.5)' );
  410. console.level = CONSOLE_LEVEL.DEFAULT;
  411. assert.ok( c.r == 1, 'Red: ' + c.r );
  412. assert.ok( c.g === 0, 'Green: ' + c.g );
  413. assert.ok( c.b === 0, 'Blue: ' + c.b );
  414. } );
  415. QUnit.test( 'setStyleRGBRedWithSpaces', ( assert ) => {
  416. ColorManagement.enabled = false; // TODO: Update and enable.
  417. const c = new Color();
  418. c.setStyle( 'rgb( 255 , 0, 0 )' );
  419. assert.ok( c.r == 1, 'Red: ' + c.r );
  420. assert.ok( c.g === 0, 'Green: ' + c.g );
  421. assert.ok( c.b === 0, 'Blue: ' + c.b );
  422. } );
  423. QUnit.test( 'setStyleRGBARedWithSpaces', ( assert ) => {
  424. ColorManagement.enabled = false; // TODO: Update and enable.
  425. const c = new Color();
  426. c.setStyle( 'rgba( 255, 0, 0 , 1 )' );
  427. assert.ok( c.r == 1, 'Red: ' + c.r );
  428. assert.ok( c.g === 0, 'Green: ' + c.g );
  429. assert.ok( c.b === 0, 'Blue: ' + c.b );
  430. } );
  431. QUnit.test( 'setStyleRGBPercent', ( assert ) => {
  432. ColorManagement.enabled = false; // TODO: Update and enable.
  433. const c = new Color();
  434. c.setStyle( 'rgb(100%,50%,10%)' );
  435. assert.ok( c.r == 1, 'Red: ' + c.r );
  436. assert.ok( c.g == 0.5, 'Green: ' + c.g );
  437. assert.ok( c.b == 0.1, 'Blue: ' + c.b );
  438. } );
  439. QUnit.test( 'setStyleRGBAPercent', ( assert ) => {
  440. ColorManagement.enabled = false; // TODO: Update and enable.
  441. const c = new Color();
  442. console.level = CONSOLE_LEVEL.ERROR;
  443. c.setStyle( 'rgba(100%,50%,10%, 0.5)' );
  444. console.level = CONSOLE_LEVEL.DEFAULT;
  445. assert.ok( c.r == 1, 'Red: ' + c.r );
  446. assert.ok( c.g == 0.5, 'Green: ' + c.g );
  447. assert.ok( c.b == 0.1, 'Blue: ' + c.b );
  448. } );
  449. QUnit.test( 'setStyleRGBPercentWithSpaces', ( assert ) => {
  450. ColorManagement.enabled = false; // TODO: Update and enable.
  451. const c = new Color();
  452. c.setStyle( 'rgb( 100% ,50% , 10% )' );
  453. assert.ok( c.r == 1, 'Red: ' + c.r );
  454. assert.ok( c.g == 0.5, 'Green: ' + c.g );
  455. assert.ok( c.b == 0.1, 'Blue: ' + c.b );
  456. } );
  457. QUnit.test( 'setStyleRGBAPercentWithSpaces', ( assert ) => {
  458. ColorManagement.enabled = false; // TODO: Update and enable.
  459. const c = new Color();
  460. console.level = CONSOLE_LEVEL.ERROR;
  461. c.setStyle( 'rgba( 100% ,50% , 10%, 0.5 )' );
  462. console.level = CONSOLE_LEVEL.DEFAULT;
  463. assert.ok( c.r == 1, 'Red: ' + c.r );
  464. assert.ok( c.g == 0.5, 'Green: ' + c.g );
  465. assert.ok( c.b == 0.1, 'Blue: ' + c.b );
  466. } );
  467. QUnit.test( 'setStyleHSLRed', ( assert ) => {
  468. ColorManagement.enabled = false; // TODO: Update and enable.
  469. const c = new Color();
  470. c.setStyle( 'hsl(360,100%,50%)' );
  471. assert.ok( c.r == 1, 'Red: ' + c.r );
  472. assert.ok( c.g === 0, 'Green: ' + c.g );
  473. assert.ok( c.b === 0, 'Blue: ' + c.b );
  474. } );
  475. QUnit.test( 'setStyleHSLARed', ( assert ) => {
  476. ColorManagement.enabled = false; // TODO: Update and enable.
  477. const c = new Color();
  478. console.level = CONSOLE_LEVEL.ERROR;
  479. c.setStyle( 'hsla(360,100%,50%,0.5)' );
  480. console.level = CONSOLE_LEVEL.DEFAULT;
  481. assert.ok( c.r == 1, 'Red: ' + c.r );
  482. assert.ok( c.g === 0, 'Green: ' + c.g );
  483. assert.ok( c.b === 0, 'Blue: ' + c.b );
  484. } );
  485. QUnit.test( 'setStyleHSLRedWithSpaces', ( assert ) => {
  486. ColorManagement.enabled = false; // TODO: Update and enable.
  487. const c = new Color();
  488. c.setStyle( 'hsl(360, 100% , 50% )' );
  489. assert.ok( c.r == 1, 'Red: ' + c.r );
  490. assert.ok( c.g === 0, 'Green: ' + c.g );
  491. assert.ok( c.b === 0, 'Blue: ' + c.b );
  492. } );
  493. QUnit.test( 'setStyleHSLARedWithSpaces', ( assert ) => {
  494. ColorManagement.enabled = false; // TODO: Update and enable.
  495. const c = new Color();
  496. console.level = CONSOLE_LEVEL.ERROR;
  497. c.setStyle( 'hsla( 360, 100% , 50%, 0.5 )' );
  498. console.level = CONSOLE_LEVEL.DEFAULT;
  499. assert.ok( c.r == 1, 'Red: ' + c.r );
  500. assert.ok( c.g === 0, 'Green: ' + c.g );
  501. assert.ok( c.b === 0, 'Blue: ' + c.b );
  502. } );
  503. QUnit.test( 'setStyleHSLRedWithDecimals', ( assert ) => {
  504. ColorManagement.enabled = false; // TODO: Update and enable.
  505. const c = new Color();
  506. c.setStyle( 'hsl(360,100.0%,50.0%)' );
  507. assert.ok( c.r == 1, 'Red: ' + c.r );
  508. assert.ok( c.g === 0, 'Green: ' + c.g );
  509. assert.ok( c.b === 0, 'Blue: ' + c.b );
  510. } );
  511. QUnit.test( 'setStyleHSLARedWithDecimals', ( assert ) => {
  512. ColorManagement.enabled = false; // TODO: Update and enable.
  513. const c = new Color();
  514. console.level = CONSOLE_LEVEL.ERROR;
  515. c.setStyle( 'hsla(360,100.0%,50.0%,0.5)' );
  516. console.level = CONSOLE_LEVEL.DEFAULT;
  517. assert.ok( c.r == 1, 'Red: ' + c.r );
  518. assert.ok( c.g === 0, 'Green: ' + c.g );
  519. assert.ok( c.b === 0, 'Blue: ' + c.b );
  520. } );
  521. QUnit.test( 'setStyleHexSkyBlue', ( assert ) => {
  522. ColorManagement.enabled = false; // TODO: Update and enable.
  523. const c = new Color();
  524. c.setStyle( '#87CEEB' );
  525. assert.ok( c.getHex() == 0x87CEEB, 'Hex c: ' + c.getHex() );
  526. } );
  527. QUnit.test( 'setStyleHexSkyBlueMixed', ( assert ) => {
  528. ColorManagement.enabled = false; // TODO: Update and enable.
  529. const c = new Color();
  530. c.setStyle( '#87cEeB' );
  531. assert.ok( c.getHex() == 0x87CEEB, 'Hex c: ' + c.getHex() );
  532. } );
  533. QUnit.test( 'setStyleHex2Olive', ( assert ) => {
  534. ColorManagement.enabled = false; // TODO: Update and enable.
  535. const c = new Color();
  536. c.setStyle( '#F00' );
  537. assert.ok( c.getHex() == 0xFF0000, 'Hex c: ' + c.getHex() );
  538. } );
  539. QUnit.test( 'setStyleHex2OliveMixed', ( assert ) => {
  540. ColorManagement.enabled = false; // TODO: Update and enable.
  541. const c = new Color();
  542. c.setStyle( '#f00' );
  543. assert.ok( c.getHex() == 0xFF0000, 'Hex c: ' + c.getHex() );
  544. } );
  545. QUnit.test( 'setStyleColorName', ( assert ) => {
  546. ColorManagement.enabled = false; // TODO: Update and enable.
  547. const c = new Color();
  548. c.setStyle( 'powderblue' );
  549. assert.ok( c.getHex() == 0xB0E0E6, 'Hex c: ' + c.getHex() );
  550. } );
  551. QUnit.test( 'iterable', ( assert ) => {
  552. ColorManagement.enabled = false; // TODO: Update and enable.
  553. const c = new Color( 0.5, 0.75, 1 );
  554. const array = [ ...c ];
  555. assert.strictEqual( array[ 0 ], 0.5, 'Color is iterable.' );
  556. assert.strictEqual( array[ 1 ], 0.75, 'Color is iterable.' );
  557. assert.strictEqual( array[ 2 ], 1, 'Color is iterable.' );
  558. } );
  559. } );
  560. } );
粤ICP备19079148号