11import chai from "chai" ;
2+ import { Color } from "./color.js" ;
23import { Swatch } from "./swatch.js" ;
34import { type Rgb } from "culori/fn" ;
5+ import { _white } from "./utilities/color-constants.js" ;
46
57const { expect } = chai ;
68
@@ -12,6 +14,9 @@ const opacityColor: Rgb = { mode: "rgb", r: 0.2, g: 0.4, b: 0.6, alpha: 0.501960
1214const opacityHex = "#33669980" ;
1315const opacityRgba = "rgba(51, 102, 153, 0.5)" ;
1416
17+ const lightGreyObject = { r : 0.75 , g : 0.75 , b : 0.75 , alpha : undefined } ;
18+ const darkGreyObject = { r : 0.25 , g : 0.25 , b : 0.25 , alpha : undefined } ;
19+
1520describe ( "Swatch" , ( ) => {
1621 it ( "should create a Swatch from the provided object" , ( ) => {
1722 const swatch = Swatch . from ( greyObject ) ;
@@ -29,7 +34,16 @@ describe("Swatch", () => {
2934 expect ( swatch . toColorString ( ) ) . to . equal ( greyHex ) ;
3035 } ) ;
3136
32- it ( "should create a Swatch from the provided hex swatch" , ( ) => {
37+ it ( "should create a Swatch from a Color" , ( ) => {
38+ const color = Color . from ( greyObject ) ;
39+ const swatch = Swatch . fromColor ( color ) ;
40+
41+ expect ( swatch ) . to . be . instanceof ( Swatch ) ;
42+ expect ( swatch . color ) . to . deep . equal ( greyColor ) ;
43+ expect ( swatch . toColorString ( ) ) . to . equal ( greyHex ) ;
44+ } ) ;
45+
46+ it ( "should create a Swatch from the provided hex color" , ( ) => {
3347 const swatch = Swatch . parse ( greyHex ) ! ;
3448
3549 expect ( swatch ) . to . be . instanceof ( Swatch ) ;
@@ -43,4 +57,59 @@ describe("Swatch", () => {
4357 expect ( swatch . color ) . to . deep . equal ( opacityColor ) ;
4458 expect ( swatch . toColorString ( ) ) . to . equal ( opacityRgba ) ;
4559 } ) ;
60+
61+ it ( "should create a light Swatch as an overlay" , ( ) => {
62+ const lightGrey = Swatch . from ( lightGreyObject ) ;
63+ const grey = Swatch . from ( greyObject ) ;
64+ const swatch = Swatch . asOverlay ( lightGrey , grey ) ! ;
65+
66+ expect ( swatch ) . to . be . instanceof ( Swatch ) ;
67+ expect ( swatch . color ) . to . deep . equal ( { mode : "rgb" , r : 1 , g : 1 , b : 1 , alpha : 0.5 } ) ;
68+ } ) ;
69+
70+ it ( "should create a dark Swatch as an overlay" , ( ) => {
71+ const darkGrey = Swatch . from ( darkGreyObject ) ;
72+ const grey = Swatch . from ( greyObject ) ;
73+ const swatch = Swatch . asOverlay ( darkGrey , grey ) ! ;
74+
75+ expect ( swatch ) . to . be . instanceof ( Swatch ) ;
76+ expect ( swatch . color ) . to . deep . equal ( { mode : "rgb" , r : 0 , g : 0 , b : 0 , alpha : 0.5 } ) ;
77+ } ) ;
78+
79+ it ( "should provide the correct relative luminance" , ( ) => {
80+ const swatch = Swatch . from ( greyObject ) ;
81+
82+ expect ( swatch . relativeLuminance ) . to . approximately ( 0.21 , 0.01 ) ;
83+ } ) ;
84+
85+ it ( "should provide a string representation" , ( ) => {
86+ const swatch = Swatch . from ( greyObject ) ;
87+
88+ expect ( swatch . toString ( ) ) . to . equal ( greyHex ) ;
89+ } ) ;
90+
91+ it ( "should provide a css representation" , ( ) => {
92+ const swatch = Swatch . from ( greyObject ) ;
93+
94+ expect ( swatch . createCSS ( ) ) . to . equal ( greyHex ) ;
95+ } ) ;
96+
97+ it ( "should provide its contrast with another value" , ( ) => {
98+ const swatch = Swatch . from ( greyObject ) ;
99+
100+ expect ( swatch . contrast ( _white ) ) . to . approximately ( 3.9 , 0.1 ) ;
101+ } ) ;
102+
103+ it ( "should provide a transparent equivalent" , ( ) => {
104+ const swatch = Swatch . from ( greyObject ) ;
105+
106+ const halfTransparent = swatch . toTransparent ( 0.5 ) ;
107+ expect ( halfTransparent ) . to . be . instanceof ( Swatch ) ;
108+ expect ( halfTransparent . color ) . to . deep . equal ( Object . assign ( greyColor , { alpha : 0.5 } ) ) ;
109+ expect ( halfTransparent . toColorString ( ) ) . to . equal ( "rgba(128, 128, 128, 0.5)" ) ;
110+ expect ( halfTransparent . toString ( ) ) . to . equal ( "rgba(128, 128, 128, 0.5)" ) ;
111+ expect ( halfTransparent . createCSS ( ) ) . to . equal ( "rgba(128, 128, 128, 0.5)" ) ;
112+ expect ( halfTransparent . relativeLuminance ) . to . approximately ( 0.21 , 0.01 ) ;
113+ expect ( halfTransparent . contrast ( _white ) ) . to . approximately ( 3.9 , 0.1 ) ;
114+ } ) ;
46115} ) ;
0 commit comments