Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support + tests for alpha colors #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions stylesheets/color-helpers/_hsv-hsl.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Ported from http://ariya.blogspot.com/2008/07/converting-between-hsl-and-hsv.html

@function ch-hsv-to-hsl($h, $s: 0, $v: 0) {
@function ch-hsv-to-hsl($h, $s: 0, $v: 0, $a: 1) {
@if type_of($h) == 'list' {
$a: if(length($h) > 3, nth($h, 4), 1);
$v: nth($h, 3);
$s: nth($h, 2);
$h: nth($h, 1);
Expand Down Expand Up @@ -30,15 +31,17 @@

$ll: $ll / 2;

@return 360deg * $h / (3.1415 * 2), percentage(max(0, min(1, $ss))), percentage(max(0, min(1, $ll)));
@return 360deg * $h / (3.1415 * 2), percentage(max(0, min(1, $ss))), percentage(max(0, min(1, $ll))), $a;
}

@function ch-hsl-to-hsv($h, $ss: 0, $ll: 0) {
@function ch-hsl-to-hsv($h, $ss: 0, $ll: 0, $a: 1) {
@if type_of($h) == 'list' {
$a: if(length($h) > 3, nth($h, 4), 1);
$ll: nth($h, 3);
$ss: nth($h, 2);
$h: nth($h, 1);
} @else if type_of($h) == 'color' {
$a: alpha($h);
$ll: lightness($h);
$ss: saturation($h);
$h: hue($h);
Expand Down Expand Up @@ -69,30 +72,33 @@
$s: (2 * $ss) / ($ll + $ss);
}

@return 360deg * $h / (3.1415 * 2), percentage(max(0, min(1, $s))), percentage(max(0, min(1, $v)));
@return 360deg * $h / (3.1415 * 2), percentage(max(0, min(1, $s))), percentage(max(0, min(1, $v))), $a;
}

@function ch-color-to-hsv($color) {
@return ch-hsl-to-hsv($color);
}

@function ch-hsv-to-color($h, $s: 0, $v: 0) {
$hsl: ch-hsv-to-hsl($h, $s, $v);
@return hsl(nth($hsl, 1), nth($hsl, 2), nth($hsl, 3));
@function ch-hsv-to-color($h, $s: 0, $v: 0, $a: 1) {
$hsl: ch-hsv-to-hsl($h, $s, $v, $a);
@return hsla(nth($hsl, 1), nth($hsl, 2), nth($hsl, 3), nth($hsl, 4));
}

@function ch-brightness($h, $s: 0, $v: 0, $adjustment: 0) {
$a: 1;

@if type_of($h) == 'color' {
$h: ch-color-to-hsv($h);
}
@if type_of($h) == 'list' {
$adjustment: $s;
$a: if(length($h) > 3, nth($h, 4), 1);
$v: nth($h, 3);
$s: nth($h, 2);
$h: nth($h, 1);
}

$v: ch-max(0%, ch-min(100%, $v + $adjustment));

@return ch-hsv-to-color($h, $s, $v);
@return ch-hsv-to-color($h, $s, $v, $a);
}
61 changes: 58 additions & 3 deletions tests/specs/hsl-hsv.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@include it("should expect a color to be converted correctly to HSV") {
$hsv: ch-hsl-to-hsv(#951);

@include should(expect($hsv), to(have-length-of(3)));
@include should(expect($hsv), to(have-length-of(4)));
@include should(expect(nth($hsv, 1)), to(be-close-to(30deg, 0)));
@include should(expect(nth($hsv, 2)), to(be-close-to(89%, 0)));
@include should(expect(nth($hsv, 3)), to(be-close-to(60%, 0)));
Expand All @@ -11,7 +11,6 @@
@include it("should expect another color to be converted correctly to HSV") {
$hsv: ch-hsl-to-hsv(#EFBF5D);

@include should(expect($hsv), to(have-length-of(3)));
@include should(expect(nth($hsv, 1)), to(be-close-to(40deg, 0)));
@include should(expect(nth($hsv, 2)), to(be-close-to(61%, 0)));
@include should(expect(nth($hsv, 3)), to(be-close-to(94%, 0)));
Expand All @@ -23,6 +22,62 @@
$finalColor: ch-hsv-to-color($hsv);

@include should(expect($finalColor), to(have-type-of(color)));
@include should(expect($finalColor), to(equal($finalColor)));
@include should(expect($finalColor), to(equal($initialColor)));
}

@include it("should expect a color with alpha to be converted back and forth with no loss") {
$initialColor: rgba(153, 85, 17, .5);
$hsv: ch-hsl-to-hsv($initialColor);
$finalColor: ch-hsv-to-color($hsv);

@include should(expect($finalColor), to(have-type-of(color)));
@include should(expect($finalColor), to(equal($initialColor)));
}

@include it("should expect a hsl list to be converted correctly to HSV") {
$color: #951;
$list: hue($color), saturation($color), lightness($color);

$hsv: ch-hsl-to-hsv($list);

@include should(expect(nth($hsv, 1)), to(be-close-to(30deg, 0)));
@include should(expect(nth($hsv, 2)), to(be-close-to(89%, 0)));
@include should(expect(nth($hsv, 3)), to(be-close-to(60%, 0)));
}

@include it("should expect hsl arguments to be converted correctly to HSV") {
$color: #951;

$hsv: ch-hsl-to-hsv(hue($color), saturation($color), lightness($color));

@include should(expect(nth($hsv, 1)), to(be-close-to(30deg, 0)));
@include should(expect(nth($hsv, 2)), to(be-close-to(89%, 0)));
@include should(expect(nth($hsv, 3)), to(be-close-to(60%, 0)));
}

@include it("should expect brightness to be tweaked correctly") {
$color: rgba(153, 85, 17, .5);
$finalColor: ch-brightness($color, 20);

@include should(expect($finalColor), to(have-type-of(color)));
@include should(expect($finalColor), to(equal(rgba(204, 113, 23, .5))));
}

@include it("should expect brightness to accept list") {
$list: ch-color-to-hsv(#951);

$finalColor: ch-brightness($list, 20);

@include should(expect($finalColor), to(have-type-of(color)));
@include should(expect($finalColor), to(equal(#CC7117)));
}

@include it("should expect brightness to accept arguments") {
$list: ch-color-to-hsv(#951);

$finalColor: ch-brightness(nth($list, 1), nth($list, 2), nth($list, 3), 20);

@include should(expect($finalColor), to(have-type-of(color)));
@include should(expect($finalColor), to(equal(#CC7117)));
}
}