1
1
import chai from "chai" ;
2
+ import { Color } from "./color.js" ;
2
3
import { Swatch } from "./swatch.js" ;
3
4
import { type Rgb } from "culori/fn" ;
5
+ import { _white } from "./utilities/color-constants.js" ;
4
6
5
7
const { expect } = chai ;
6
8
@@ -12,6 +14,9 @@ const opacityColor: Rgb = { mode: "rgb", r: 0.2, g: 0.4, b: 0.6, alpha: 0.501960
12
14
const opacityHex = "#33669980" ;
13
15
const opacityRgba = "rgba(51, 102, 153, 0.5)" ;
14
16
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
+
15
20
describe ( "Swatch" , ( ) => {
16
21
it ( "should create a Swatch from the provided object" , ( ) => {
17
22
const swatch = Swatch . from ( greyObject ) ;
@@ -29,7 +34,16 @@ describe("Swatch", () => {
29
34
expect ( swatch . toColorString ( ) ) . to . equal ( greyHex ) ;
30
35
} ) ;
31
36
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" , ( ) => {
33
47
const swatch = Swatch . parse ( greyHex ) ! ;
34
48
35
49
expect ( swatch ) . to . be . instanceof ( Swatch ) ;
@@ -43,4 +57,59 @@ describe("Swatch", () => {
43
57
expect ( swatch . color ) . to . deep . equal ( opacityColor ) ;
44
58
expect ( swatch . toColorString ( ) ) . to . equal ( opacityRgba ) ;
45
59
} ) ;
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
+ } ) ;
46
115
} ) ;
0 commit comments