| 1 | use csscolorparser::parse; |
| 2 | |
| 3 | #[test] |
| 4 | fn parser() { |
| 5 | let test_data = [ |
| 6 | ["rgb(FROM #abcdef g B r / Alpha)", "#cdefab"], |
| 7 | [ |
| 8 | "rgb(from rgb(from #bad455 g calc(b + 23) r / alpha) b r calc(g - 23))", |
| 9 | "#bad455", |
| 10 | ], |
| 11 | // --- |
| 12 | ["rgb(from #bad455 r g b)", "#bad455"], |
| 13 | ["rgb(from #bad455 b r g / alpha)", "#55bad4"], |
| 14 | ["rgb(from #bad455 255 0 90)", "#ff005a"], |
| 15 | ["rgb(from #bad455 r g b / 0.2)", "#bad45533"], |
| 16 | ["rgb(from #bad455 r g b / calc(alpha / 2))", "#bad45580"], |
| 17 | [ |
| 18 | "rgb(from #bad455 calc(r + 10) calc(g - 15) calc(b * 0.75))", |
| 19 | "#c4c540", |
| 20 | ], |
| 21 | ["rgb(from #bad455 calc((r + g) / 2) b g)", "#c755d4"], |
| 22 | [ |
| 23 | "rgb(from #bad455 127 100 calc(((r + g) + b) / 3))", |
| 24 | "#7f64a1", |
| 25 | ], |
| 26 | // --- |
| 27 | ["hwb(from #bad455 h w b)", "#bad455"], |
| 28 | ["hwb(from #bad455 h b w)", "#90aa2b"], |
| 29 | ["hwb(from #bad455 0 15 10)", "#e62626"], |
| 30 | [ |
| 31 | "hwb(from #bad455 calc(h + 90) calc(w - 5) calc(b + 10))", |
| 32 | "#48bb99", |
| 33 | ], |
| 34 | // --- |
| 35 | ["hsl(from #bad455 h s l)", "#bad455"], |
| 36 | ["hsl(from #bad455 90 50 65)", "#a6d279"], |
| 37 | ["hsl(from #bad455 h l s)", "#bbd45c"], |
| 38 | ["hsl(from #bad455 calc(h - 45) calc(s + 9) l)", "#de8e4b"], |
| 39 | // --- |
| 40 | ["oklab(from #bad455 l a b)", "#bad455"], |
| 41 | ["oklab(from #bad455 l b a)", "#fe9ff5"], |
| 42 | ["oklab(from #bad455 0.75 -0.2 0.23)", "#60ce00"], |
| 43 | ["oklab(from #bad455 calc(l * 0.7) a b)", "#708500"], |
| 44 | // --- |
| 45 | ["oklch(from #bad455 l c h)", "#bad455"], |
| 46 | ["oklch(from #bad455 0.75 0.1 170)", "#66c3a4"], |
| 47 | /*[ |
| 48 | "oklch(from #bad455 calc(l * 1.5) c calc(h + 180))", |
| 49 | "#ffe7ff", |
| 50 | ],*/ |
| 51 | [ |
| 52 | "oklch(from #bad455 calc(l - 0.15) calc(c * 0.7) h)", |
| 53 | "#8fa150", |
| 54 | ], |
| 55 | // --- |
| 56 | ["lab(from #bad455 l a b)", "#bad455"], |
| 57 | ["lab(from #bad455 l a b / calc(alpha / 2))", "#bad45580"], |
| 58 | // --- |
| 59 | ["lch(from #bad455 l c h)", "#bad455"], |
| 60 | ["lch(from #bad455 l c h / calc(alpha * 0.5))", "#bad45580"], |
| 61 | ]; |
| 62 | for [s, hex] in test_data { |
| 63 | assert_eq!(parse(s).unwrap().to_css_hex(), hex, "{:?}", s); |
| 64 | } |
| 65 | |
| 66 | let test_data = [ |
| 67 | "#ffffff", |
| 68 | "#000000", |
| 69 | "#71fe15", |
| 70 | "#d6e3c9", |
| 71 | "#2a7719", |
| 72 | "#b53717", |
| 73 | "#5b0b8d", |
| 74 | "#aff632", |
| 75 | "#65ec8d", |
| 76 | "#d35493", |
| 77 | "#289e5f", |
| 78 | "#b46152", |
| 79 | "#e0afee", |
| 80 | "#ac2be4", |
| 81 | "#233490", |
| 82 | "#1afbc5", |
| 83 | "#e41755", |
| 84 | "#e052ee", |
| 85 | "#4d1b5e", |
| 86 | "#230cde", |
| 87 | "#f8a243", |
| 88 | "#a130d1", |
| 89 | "#b38373", |
| 90 | "#6b9fa203", |
| 91 | "#0e5e0be6", |
| 92 | "#84f9a716", |
| 93 | "#48651550", |
| 94 | "#1adc2cf4", |
| 95 | "#c191a31c", |
| 96 | "#a25518c5", |
| 97 | "#cb33f2c9", |
| 98 | "#89b21d36", |
| 99 | "#cbb97f3e", |
| 100 | ]; |
| 101 | for hex in test_data { |
| 102 | let p = [ |
| 103 | format!("rgb(from {hex} r g b)"), |
| 104 | format!("hwb(from {hex} h w b / alpha)"), |
| 105 | format!("hsl(from {hex} h s l)"), |
| 106 | format!("hsv(from {hex} h s v)"), |
| 107 | format!("lab(from {hex} l a b)"), |
| 108 | format!("lch(from {hex} l c h)"), |
| 109 | format!("oklab(from {hex} l a b)"), |
| 110 | format!("oklch(from {hex} l c h)"), |
| 111 | ]; |
| 112 | for s in p { |
| 113 | let c = parse(&s); |
| 114 | assert!(c.is_ok(), "{:?}", s); |
| 115 | assert_eq!(hex, c.unwrap().to_css_hex()); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | #[test] |
| 121 | fn invalid() { |
| 122 | let test_data = [ |
| 123 | "rgb(from)", |
| 124 | "rgb(from #f00)", |
| 125 | "rgb(from #abx 255 0 0)", |
| 126 | "rgb(from #f00 r g)", |
| 127 | "rgb(from #f00 r g b 0.5)", |
| 128 | "hwb(from #f00 h w b alpha)", |
| 129 | "rgb(from #f00 r g b / alpha 10)", |
| 130 | "hsl(from #f00 h s x)", |
| 131 | "rgb(from hwb(from hsv(90 0.5 v) h w b) 0 0 0)", |
| 132 | // non ascii |
| 133 | "rgb(ā #f00 r g b)", |
| 134 | "rgb(from â r g b)", |
| 135 | "rgb(from #f00 æ g b)", |
| 136 | "rgb(from #f00 r g b / æ)", |
| 137 | "rgb(from #f00 r calc(ã+15) b)", |
| 138 | "rgb(from #f00 calc(1* (r-æ)) g b)", |
| 139 | "rgb(from #f00 r g b / 1 ã)", |
| 140 | "rgbà(from #f00 r g b)", |
| 141 | "æç(from #f00 r g b)", |
| 142 | ]; |
| 143 | for s in test_data { |
| 144 | assert!(parse(s).is_err(), "{:?}", s); |
| 145 | } |
| 146 | } |