1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
use graphics::gradient::ast::{Color, ColorStop, Distance};
use graphics::gradient::stops::{color_to_string, normalize_color_stops};
fn stop(color: Color, length: Option<Distance>) -> ColorStop {
ColorStop { color, length }
}
fn literal(name: &str) -> Color {
Color::Literal(name.to_string())
}
#[test]
fn color_string_round_trip() {
assert_eq!(color_to_string(&Color::Hex("abc".into())), "#abc");
assert_eq!(color_to_string(&literal("red")), "red");
assert_eq!(
color_to_string(&Color::Rgb(vec!["1".into(), "2".into(), "3".into()])),
"rgb(1,2,3)"
);
assert_eq!(
color_to_string(&Color::Rgba(vec!["1".into(), "2".into(), "3".into(), "0.5".into()])),
"rgba(1,2,3,0.5)"
);
assert_eq!(
color_to_string(&Color::Hsl(["120".into(), "50".into(), "50".into()])),
"hsl(120,50,50)"
);
assert_eq!(
color_to_string(&Color::Hsla(["120".into(), "50".into(), "50".into(), "0.5".into()])),
"hsla(120,50,50,0.5)"
);
assert_eq!(color_to_string(&Color::Var("--x".into())), "var(--x)");
}
#[test]
fn evenly_distributed_stops() {
let stops = normalize_color_stops(
&[stop(literal("red"), None), stop(literal("blue"), None)],
100.0,
);
assert_eq!(stops[0].offset, 0.0);
assert_eq!(stops[1].offset, 1.0);
let stops = normalize_color_stops(
&[
stop(literal("a"), None),
stop(literal("b"), None),
stop(literal("c"), None),
],
100.0,
);
assert_eq!(stops[1].offset, 0.5);
}
#[test]
fn explicit_and_mixed_offsets() {
let stops = normalize_color_stops(
&[
stop(literal("red"), Some(Distance::Percent("20".into()))),
stop(literal("green"), None),
stop(literal("blue"), Some(Distance::Percent("80".into()))),
],
100.0,
);
assert_eq!(stops[0].offset, 0.2);
assert_eq!(stops[1].offset, 0.5);
assert_eq!(stops[2].offset, 0.8);
}
#[test]
fn px_and_em_offsets_use_gradient_length() {
let stops = normalize_color_stops(
&[
stop(literal("red"), Some(Distance::Px("50".into()))),
stop(literal("blue"), Some(Distance::Em("6.25".into()))),
],
200.0,
);
assert_eq!(stops[0].offset, 0.25);
assert!((stops[1].offset - 0.5).abs() < 1e-9);
}
#[test]
fn offsets_clamped() {
let stops = normalize_color_stops(
&[
stop(literal("red"), Some(Distance::Percent("-20".into()))),
stop(literal("blue"), Some(Distance::Percent("150".into()))),
],
100.0,
);
assert_eq!(stops[0].offset, 0.0);
assert_eq!(stops[1].offset, 1.0);
}
#[test]
fn leading_unpositioned_stops() {
let stops = normalize_color_stops(
&[
stop(literal("a"), None),
stop(literal("b"), None),
stop(literal("c"), Some(Distance::Percent("90".into()))),
],
100.0,
);
assert_eq!(stops[0].offset, 0.0);
assert_eq!(stops[1].offset, 0.45);
assert_eq!(stops[2].offset, 0.9);
}
#[test]
fn trailing_unpositioned_stops() {
let stops = normalize_color_stops(
&[
stop(literal("a"), Some(Distance::Percent("10".into()))),
stop(literal("b"), None),
stop(literal("c"), None),
],
100.0,
);
assert_eq!(stops[0].offset, 0.1);
assert!((stops[1].offset - 0.55).abs() < 1e-9);
assert_eq!(stops[2].offset, 1.0);
}
#[test]
fn transparent_stop_adopts_previous_color() {
let stops = normalize_color_stops(
&[
stop(Color::Hex("ff0000".into()), None),
stop(literal("transparent"), None),
stop(Color::Hex("0000ff".into()), None),
],
100.0,
);
assert_eq!(stops[1].color, "rgba(255,0,0,0)");
}
#[test]
fn transparent_stop_with_unparseable_donors_unchanged() {
let stops = normalize_color_stops(
&[
stop(literal("red"), None),
stop(literal("transparent"), None),
stop(literal("blue"), None),
],
100.0,
);
assert_eq!(stops[1].color, "transparent");
}
#[test]
fn transparent_stop_adopts_next_color_when_first() {
let stops = normalize_color_stops(
&[
stop(literal("transparent"), None),
stop(Color::Hex("00ff00".into()), None),
],
100.0,
);
assert_eq!(stops[1].color, "#00ff00");
assert_eq!(stops[0].color, "rgba(0,255,0,0)");
}
#[test]
fn all_transparent_stops_unchanged() {
let stops = normalize_color_stops(
&[
stop(literal("transparent"), None),
stop(Color::Rgba(vec!["0".into(), "0".into(), "0".into(), "0".into()]), None),
],
100.0,
);
assert_eq!(stops[0].color, "transparent");
assert_eq!(stops[1].color, "rgba(0,0,0,0)");
}
|