Skip to content

Commit 96d8c20

Browse files
committed
Tweak API for fadein() and fadeout() operations.
Previously, we were returning `self` for these operations when impled on fully-opaque colors. This changes the API to diverge from Less' implementation such that, for opaque colors, we convert into the alpha channel equivalent (rgba or hsla) and then perform the color operation as expected.
1 parent 253b76e commit 96d8c20

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

src/hsl.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,12 @@ impl Color for HSL {
127127
self.to_hsla().darken(amount).to_hsl()
128128
}
129129

130-
fn fadein(self, _amount: Ratio) -> Self {
131-
self
130+
fn fadein(self, amount: Ratio) -> Self::Alpha {
131+
self.to_hsla().fadein(amount)
132132
}
133133

134-
fn fadeout(self, _amount: Ratio) -> Self {
135-
self
134+
fn fadeout(self, amount: Ratio) -> Self::Alpha {
135+
self.to_hsla().fadeout(amount)
136136
}
137137

138138
fn fade(self, amount: Ratio) -> Self::Alpha {

src/lib.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ pub trait Color {
152152
fn darken(self, amount: Ratio) -> Self;
153153

154154
/// Decreases the transparency (or increase the opacity) of `self`, making it more opaque.
155-
/// Has no effect on opaque (non-alpha) colors.
155+
/// For opqaue colors, converts into the alpha equivalent of `self`, and then increases the opacity.
156156
/// For more, see Less' [Color Operations](http://lesscss.org/functions/#color-operations-fadein).
157157
///
158158
/// # Examples
@@ -163,12 +163,12 @@ pub trait Color {
163163
/// let cornflower_blue = rgb(100, 149, 237);
164164
///
165165
/// assert_eq!(tomato.fadein(percent(25)), rgba(255, 99, 71, 0.5));
166-
/// assert_eq!(cornflower_blue.fadein(percent(50)), rgb(100, 149, 237));
166+
/// assert_eq!(cornflower_blue.fadein(percent(75)), rgba(100, 149, 237, 1.0));
167167
/// ```
168-
fn fadein(self, amount: Ratio) -> Self;
168+
fn fadein(self, amount: Ratio) -> Self::Alpha;
169169

170170
/// Increases the transparency (or decrease the opacity) of `self`, making it less opaque.
171-
/// Has no effect on opaque (non-alpha) colors.
171+
/// For opqaue colors, converts into the alpha equivalent of `self`, and then decreases the opacity.
172172
/// For more, see Less' [Color Operations](http://lesscss.org/functions/#color-operations-fadeout).
173173
///
174174
/// # Examples
@@ -179,11 +179,11 @@ pub trait Color {
179179
/// let cornflower_blue = rgb(100, 149, 237);
180180
///
181181
/// assert_eq!(tomato.fadeout(percent(25)), rgba(255, 99, 71, 0.25));
182-
/// assert_eq!(cornflower_blue.fadeout(percent(50)), rgb(100, 149, 237));
182+
/// assert_eq!(cornflower_blue.fadeout(percent(75)), rgba(100, 149, 237, 0.25));
183183
/// ```
184-
fn fadeout(self, amount: Ratio) -> Self;
184+
fn fadeout(self, amount: Ratio) -> Self::Alpha;
185185

186-
/// Sets the absolute opacity of `self`.
186+
/// Sets the absolute opacity of `self`, and returns the alpha equivalent.
187187
/// Can be applied to colors whether they already have an opacity value or not.
188188
/// For more, see Less' [Color Operations](http://lesscss.org/functions/#color-operations-fade).
189189
///
@@ -633,12 +633,12 @@ mod css_color_tests {
633633

634634
#[test]
635635
fn can_fadein() {
636-
assert_approximately_eq!(hsl(9, 35, 50).fadein(percent(25)), hsl(9, 35, 50));
636+
assert_approximately_eq!(hsl(9, 35, 50).fadein(percent(25)), hsla(9, 35, 50, 1.0));
637637
assert_approximately_eq!(
638638
hsla(9, 35, 50, 0.5).fadein(percent(25)),
639639
hsla(9, 35, 50, 0.75)
640640
);
641-
assert_approximately_eq!(rgb(172, 96, 83).fadein(percent(25)), rgb(172, 96, 83));
641+
assert_approximately_eq!(rgb(172, 96, 83).fadein(percent(25)), rgba(172, 96, 83, 1.0));
642642
assert_approximately_eq!(
643643
rgba(172, 96, 83, 0.50).fadein(percent(25)),
644644
rgba(172, 96, 83, 0.75)
@@ -647,8 +647,11 @@ mod css_color_tests {
647647

648648
#[test]
649649
fn can_fadeout() {
650-
assert_approximately_eq!(hsl(9, 35, 50).fadeout(percent(25)), hsl(9, 35, 50));
651-
assert_approximately_eq!(rgb(172, 96, 83).fadeout(percent(25)), rgb(172, 96, 83));
650+
assert_approximately_eq!(hsl(9, 35, 50).fadeout(percent(25)), hsla(9, 35, 50, 0.75));
651+
assert_approximately_eq!(
652+
rgb(172, 96, 83).fadeout(percent(25)),
653+
rgba(172, 96, 83, 0.75)
654+
);
652655
assert_approximately_eq!(
653656
hsla(9, 35, 50, 0.60).fadeout(percent(25)),
654657
hsla(9, 35, 50, 0.35)

src/rgb.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ impl Color for RGB {
124124
self.to_rgba().darken(amount).to_rgb()
125125
}
126126

127-
fn fadein(self, _amount: Ratio) -> Self {
128-
self
127+
fn fadein(self, amount: Ratio) -> RGBA {
128+
self.to_rgba().fadein(amount)
129129
}
130130

131-
fn fadeout(self, _amount: Ratio) -> Self {
132-
self
131+
fn fadeout(self, amount: Ratio) -> RGBA {
132+
self.to_rgba().fadeout(amount)
133133
}
134134

135135
fn fade(self, amount: Ratio) -> RGBA {

0 commit comments

Comments
 (0)