sass-color-helpers 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +110 -0
- data/lib/sass-color-helpers.rb +11 -0
- data/stylesheets/_color-helpers.scss +4 -0
- data/stylesheets/color-helpers/_alpha-pick.scss +25 -0
- data/stylesheets/color-helpers/_contrast.scss +53 -0
- data/stylesheets/color-helpers/_hsv-hsl.scss +98 -0
- data/stylesheets/color-helpers/_math.scss +50 -0
- metadata +79 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: aa4987f5606b1e6bb34404ab346bab84369b2de4
|
4
|
+
data.tar.gz: 6e9215bcb8698b72fb50812b3002aeca6cba5282
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 34c4fb8e574d78285158af0dacc2d892a05dc1d1302e1ec4ab28af17d8686cbb551737ac1ca3b91c410551877548177d4a369338e60f6eb6f50d23b62b42be6d
|
7
|
+
data.tar.gz: ee01068dfcc395679af181e0d88d706387bcf1364ca53d2aa83ee4b0c946567570c94c8d5bce326bcfff05a57c66c99a278be2ba59c8617bd90ce7eaf9953591
|
data/README.md
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
# Sass Color Helpers
|
2
|
+
|
3
|
+
Pure Sass, no Ruby. Should therefore be [libsass](http://libsass.org/) compatible. Also available as a Compass extension for those who still want that.
|
4
|
+
|
5
|
+
A collection of color helpers (and some related math helpers). Handles HSL to HSV/HSB conversion, calculation of contrast (and warnings for worst case scenarios) and estimation of semi-transparent colors – everything to make it easier and more fool-proof to implement a target design and in the end get as good of a design as possible.
|
6
|
+
|
7
|
+
Lastly also some candy for the geeks: nth root and decimal exponent powers function. Because graphics can be tough.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
**Bower**:
|
12
|
+
|
13
|
+
```bash
|
14
|
+
bower install sass-color-helpers --save
|
15
|
+
```
|
16
|
+
|
17
|
+
Then either manually include the individual helpers from the `stylesheets/` folder or set the load path of something like [grunt-contrib-sass](https://npmjs.org/package/grunt-contrib-sass) to look for files in the bower folder – or use something like [grunt-bower-install](https://www.npmjs.org/package/grunt-bower-install) to automate it all.
|
18
|
+
|
19
|
+
**Compass**:
|
20
|
+
|
21
|
+
Install as a Compass extension through ruby gems: `gem install sass-color-helpers`
|
22
|
+
|
23
|
+
## Helpers
|
24
|
+
|
25
|
+
### Alpha Picker / Estimation
|
26
|
+
|
27
|
+
```scss
|
28
|
+
$alpha: ch-calculate-alpha(#FFF, #526D4E, #DFE4DF);
|
29
|
+
$actionColor: ch-calculate-top-color($alpha, #BEDBA1, #89D338);
|
30
|
+
```
|
31
|
+
|
32
|
+
Originaly published at: https://gist.github.com/voxpelli/8452877
|
33
|
+
|
34
|
+
#### Methods:
|
35
|
+
|
36
|
+
* `ch-calculate-alpha($topColor, $bottomColor, $targetColor)` – to calculate an alpha from a defined color and colors picked from a reference image
|
37
|
+
* `ch-calculate-top-color($alpha, $bottomColor, $targetColor)` – to calculate a color from a defined alpha and colors picked from a reference image
|
38
|
+
|
39
|
+
### Contrast
|
40
|
+
|
41
|
+
```scss
|
42
|
+
$contrast: ch-color-contrast($backgroundColor, $textColor);
|
43
|
+
|
44
|
+
@if ($contrast < 3) {
|
45
|
+
@warn "Contrast ratio of #{$textColor} on #{$backgroundColor} is pretty bad, just #{$contrast}";
|
46
|
+
}
|
47
|
+
```
|
48
|
+
|
49
|
+
Originally published at: https://gist.github.com/voxpelli/6304812
|
50
|
+
|
51
|
+
Based on code from: https://github.com/LeaVerou/contrast-ratio
|
52
|
+
|
53
|
+
#### Methods:
|
54
|
+
|
55
|
+
* `ch-color-contrast($color1, $color2)` – when given a background color and a front color it returns the [contrast ratio](http://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef) between those two
|
56
|
+
* `ch-best-color-contrast($base, $colors, $tolerance: 0)` – when given a background color and a list of front colors it will return the first front color with the best contrast ratio. If tolerance is set to something higher than 0 than a front color later in the list will only beat a previous color if it improves the contrast ratio by at least that much – useful if one has a preferably color that one wants to use in all but the most extreme cases.
|
57
|
+
|
58
|
+
### HSV/HSB to HSL
|
59
|
+
|
60
|
+
```scss
|
61
|
+
$hsv: ch-color-to-hsv($color);
|
62
|
+
|
63
|
+
$v: nth($hsv, 3);
|
64
|
+
$s: nth($hsv, 2);
|
65
|
+
$h: nth($hsv, 1);
|
66
|
+
|
67
|
+
$v: max(0%, min(100%, $v + $tweak));
|
68
|
+
|
69
|
+
$color: ch-hsv-to-color($h, $s, $v);
|
70
|
+
```
|
71
|
+
|
72
|
+
Originally published at: https://gist.github.com/voxpelli/1069204
|
73
|
+
|
74
|
+
Based on code from: http://ariya.blogspot.com/2008/07/converting-between-hsl-and-hsv.html
|
75
|
+
|
76
|
+
#### Methods:
|
77
|
+
|
78
|
+
* `ch-hsv-to-hsl($h, $s: 0, $v: 0)` – transforms a hsv value given by either individual parameters, or by a list as the first parameter, to a list of hsl values
|
79
|
+
* `ch-hsl-to-hsv($h, $ss: 0, $ll: 0)` – transforms a hsl value given by either individual parameters, a list values as the first parameter or as a color as the first parameter, to a list of hsv values
|
80
|
+
* `ch-color-to-hsv($color)` – alias for `ch-hsl-to-hsv($color)`
|
81
|
+
* `ch-hsv-to-color($h, $s: 0, $v: 0)` – shorthand for a `ch-hsv-to-hsl($h, $s, $v)` followed by a `hsl()` transforming the hsl values to a color
|
82
|
+
* `ch-brightness($h, $s: 0, $v: 0, $adjustment: 0)` – shorthand for changing the brightness of a color. If first argument is either a list of hsv values or a color then the second argument is the adjustment to apply. Otherwise the first threee arguments are the individal hsv values and the adjustment the fourth argument.
|
83
|
+
|
84
|
+
### Math
|
85
|
+
|
86
|
+
Originally published at: https://gist.github.com/voxpelli/6304812
|
87
|
+
|
88
|
+
Based on code from: http://rosettacode.org/wiki/Greatest_common_divisor#JavaScript and http://rosettacode.org/wiki/Nth_root#JavaScript
|
89
|
+
|
90
|
+
#### Methods:
|
91
|
+
|
92
|
+
* `ch-pow($base, $exponent, $prec: 12)` – calculates $base raised to the power of $exponent where $exponent can be a decimal number – and if so, $prec defines the precision of the calculation
|
93
|
+
* `ch-nth-root($num, $n: 2, $prec: 12)` – calculates the nth root of a number with the defined precision. Used by `ch-pow()`.
|
94
|
+
* `ch-gcd($a, $b)` – finds the greatest common divisor between the two values
|
95
|
+
* `ch-max($v1, $v2)` – returns the biggest of the two values
|
96
|
+
* `ch-min($v1, $v2)` – returns the smaller of the two values
|
97
|
+
|
98
|
+
## License
|
99
|
+
|
100
|
+
MIT, [http://voxpelli.mit-license.org](http://voxpelli.mit-license.org)
|
101
|
+
|
102
|
+
## Changelog
|
103
|
+
|
104
|
+
### 2.0.0
|
105
|
+
|
106
|
+
* Repackaged as a Compass extension so that the code can be installed through either Compass or Bower (or manually)
|
107
|
+
|
108
|
+
### 1.0.0
|
109
|
+
|
110
|
+
* First packaged version of the scripts. They were previously published as gists on GitHub over a period of time.
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# Based on https://github.com/Team-Sass/Compass-Extension-Template
|
2
|
+
|
3
|
+
require 'compass'
|
4
|
+
|
5
|
+
extension_path = File.expand_path(File.join(File.dirname(__FILE__), ".."))
|
6
|
+
Compass::Frameworks.register('sass-color-helpers', :path => extension_path)
|
7
|
+
|
8
|
+
module SassColorHelpers
|
9
|
+
VERSION = "2.0.0"
|
10
|
+
DATE = "2014-06-16"
|
11
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
@function ch-calculate-alpha($topColor, $bottomColor, $targetColor) {
|
2
|
+
$topRgb: red($topColor), green($topColor), blue($topColor);
|
3
|
+
$bottomRgb: red($bottomColor), green($bottomColor), blue($bottomColor);
|
4
|
+
$targetRgb: red($targetColor), green($targetColor), blue($targetColor);
|
5
|
+
$alphaAvg: 0;
|
6
|
+
|
7
|
+
@for $i from 1 through 3 {
|
8
|
+
$alphaAvg: ($alphaAvg * ($i - 1) + (nth($targetRgb, $i) - nth($bottomRgb, $i)) / (nth($topRgb, $i) - nth($bottomRgb, $i))) / $i;
|
9
|
+
}
|
10
|
+
|
11
|
+
@return $alphaAvg;
|
12
|
+
}
|
13
|
+
|
14
|
+
@function ch-calculate-top-color($alpha, $bottomColor, $targetColor) {
|
15
|
+
$topRgb: ();
|
16
|
+
$bottomRgb: red($bottomColor), green($bottomColor), blue($bottomColor);
|
17
|
+
$targetRgb: red($targetColor), green($targetColor), blue($targetColor);
|
18
|
+
|
19
|
+
@for $i from 1 through 3 {
|
20
|
+
$topRgb: append($topRgb, (nth($targetRgb, $i) - (1 - $alpha) * nth($bottomRgb, $i)) / $alpha);
|
21
|
+
}
|
22
|
+
|
23
|
+
@return rgb(nth($topRgb, 1), nth($topRgb, 2), nth($topRgb, 3));
|
24
|
+
}
|
25
|
+
|
@@ -0,0 +1,53 @@
|
|
1
|
+
@function ch-color-luminance($color) {
|
2
|
+
// Adapted from: https://github.com/LeaVerou/contrast-ratio/blob/gh-pages/color.js
|
3
|
+
// Formula: http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
|
4
|
+
$rgba: red($color), green($color), blue($color);
|
5
|
+
$rgba2: ();
|
6
|
+
|
7
|
+
@for $i from 1 through 3 {
|
8
|
+
$rgb: nth($rgba, $i);
|
9
|
+
$rgb: $rgb / 255;
|
10
|
+
|
11
|
+
$rgb: if($rgb < .03928, $rgb / 12.92, ch-pow(($rgb + .055) / 1.055, 2.4));
|
12
|
+
|
13
|
+
$rgba2: append($rgba2, $rgb);
|
14
|
+
}
|
15
|
+
|
16
|
+
@return .2126 * nth($rgba2, 1) + .7152 * nth($rgba2, 2) + 0.0722 * nth($rgba2, 3);
|
17
|
+
}
|
18
|
+
|
19
|
+
@function ch-color-contrast($color1, $color2) {
|
20
|
+
// Adapted from: https://github.com/LeaVerou/contrast-ratio/blob/gh-pages/color.js
|
21
|
+
// Formula: http://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef
|
22
|
+
$luminance1: ch-color-luminance($color1) + .05;
|
23
|
+
$luminance2: ch-color-luminance($color2) + .05;
|
24
|
+
$ratio: $luminance1 / $luminance2;
|
25
|
+
|
26
|
+
@if $luminance2 > $luminance1 {
|
27
|
+
$ratio: 1 / $ratio;
|
28
|
+
}
|
29
|
+
|
30
|
+
$ratio: round($ratio * 10) / 10;
|
31
|
+
|
32
|
+
@return $ratio;
|
33
|
+
}
|
34
|
+
|
35
|
+
@function ch-best-color-contrast($base, $colors, $tolerance: 0) {
|
36
|
+
$best: nth($colors, 1);
|
37
|
+
$contrast: ch-color-contrast($base, $best);
|
38
|
+
|
39
|
+
@for $i from 2 through length($colors) {
|
40
|
+
$currentColor: nth($colors, $i);
|
41
|
+
$currentContrast: ch-color-contrast($base, $currentColor);
|
42
|
+
@if ($currentContrast - $contrast > $tolerance) {
|
43
|
+
$best: $currentColor;
|
44
|
+
$contrast: $currentContrast;
|
45
|
+
}
|
46
|
+
}
|
47
|
+
|
48
|
+
@if ($contrast < 3) {
|
49
|
+
@warn "Contrast ratio of #{$best} on #{$base} is pretty bad, just #{$contrast}";
|
50
|
+
}
|
51
|
+
|
52
|
+
@return $best;
|
53
|
+
}
|
@@ -0,0 +1,98 @@
|
|
1
|
+
// Ported from http://ariya.blogspot.com/2008/07/converting-between-hsl-and-hsv.html
|
2
|
+
|
3
|
+
@function ch-hsv-to-hsl($h, $s: 0, $v: 0) {
|
4
|
+
@if type_of($h) == 'list' {
|
5
|
+
$v: nth($h, 3);
|
6
|
+
$s: nth($h, 2);
|
7
|
+
$h: nth($h, 1);
|
8
|
+
}
|
9
|
+
|
10
|
+
@if unit($h) == 'deg' {
|
11
|
+
$h: 3.1415 * 2 * ($h / 360deg);
|
12
|
+
}
|
13
|
+
@if unit($s) == '%' {
|
14
|
+
$s: 0 + ($s / 100%);
|
15
|
+
}
|
16
|
+
@if unit($v) == '%' {
|
17
|
+
$v: 0 + ($v / 100%);
|
18
|
+
}
|
19
|
+
|
20
|
+
$ss: $s * $v;
|
21
|
+
$ll: (2 - $s) * $v;
|
22
|
+
|
23
|
+
@if ($ll == 2 or $ll == 0) {
|
24
|
+
$ss: 0;
|
25
|
+
} @else if $ll <= 1 {
|
26
|
+
$ss: $ss / $ll;
|
27
|
+
} @else {
|
28
|
+
$ss: $ss / (2 - $ll);
|
29
|
+
}
|
30
|
+
|
31
|
+
$ll: $ll / 2;
|
32
|
+
|
33
|
+
@return 360deg * $h / (3.1415 * 2), percentage(max(0, min(1, $ss))), percentage(max(0, min(1, $ll)));
|
34
|
+
}
|
35
|
+
|
36
|
+
@function ch-hsl-to-hsv($h, $ss: 0, $ll: 0) {
|
37
|
+
@if type_of($h) == 'list' {
|
38
|
+
$ll: nth($h, 3);
|
39
|
+
$ss: nth($h, 2);
|
40
|
+
$h: nth($h, 1);
|
41
|
+
} @else if type_of($h) == 'color' {
|
42
|
+
$ll: lightness($h);
|
43
|
+
$ss: saturation($h);
|
44
|
+
$h: hue($h);
|
45
|
+
}
|
46
|
+
|
47
|
+
@if unit($h) == 'deg' {
|
48
|
+
$h: 3.1415 * 2 * ($h / 360deg);
|
49
|
+
}
|
50
|
+
@if unit($ss) == '%' {
|
51
|
+
$ss: 0 + ($ss / 100%);
|
52
|
+
}
|
53
|
+
@if unit($ll) == '%' {
|
54
|
+
$ll: 0 + ($ll / 100%);
|
55
|
+
}
|
56
|
+
|
57
|
+
$ll: $ll * 2;
|
58
|
+
|
59
|
+
@if $ll <= 1 {
|
60
|
+
$ss: $ss * $ll;
|
61
|
+
} @else {
|
62
|
+
$ss: $ss * (2 - $ll);
|
63
|
+
}
|
64
|
+
|
65
|
+
$v: ($ll + $ss) / 2;
|
66
|
+
|
67
|
+
$s: 0;
|
68
|
+
@if $ll + $ss != 0 {
|
69
|
+
$s: (2 * $ss) / ($ll + $ss);
|
70
|
+
}
|
71
|
+
|
72
|
+
@return 360deg * $h / (3.1415 * 2), percentage(max(0, min(1, $s))), percentage(max(0, min(1, $v)));
|
73
|
+
}
|
74
|
+
|
75
|
+
@function ch-color-to-hsv($color) {
|
76
|
+
@return ch-hsl-to-hsv($color);
|
77
|
+
}
|
78
|
+
|
79
|
+
@function ch-hsv-to-color($h, $s: 0, $v: 0) {
|
80
|
+
$hsl: ch-hsv-to-hsl($h, $s, $v);
|
81
|
+
@return hsl(nth($hsl, 1), nth($hsl, 2), nth($hsl, 3));
|
82
|
+
}
|
83
|
+
|
84
|
+
@function ch-brightness($h, $s: 0, $v: 0, $adjustment: 0) {
|
85
|
+
@if type_of($h) == 'color' {
|
86
|
+
$h: ch-color-to-hsv($h);
|
87
|
+
}
|
88
|
+
@if type_of($h) == 'list' {
|
89
|
+
$adjustment: $s;
|
90
|
+
$v: nth($h, 3);
|
91
|
+
$s: nth($h, 2);
|
92
|
+
$h: nth($h, 1);
|
93
|
+
}
|
94
|
+
|
95
|
+
$v: ch-max(0%, ch-min(100%, $v + $adjustment));
|
96
|
+
|
97
|
+
@return ch-hsv-to-color($h, $s, $v);
|
98
|
+
}
|
@@ -0,0 +1,50 @@
|
|
1
|
+
@function ch-max($v1, $v2) {
|
2
|
+
@return if($v1 > $v2, $v1, $v2);
|
3
|
+
}
|
4
|
+
|
5
|
+
@function ch-min($v1, $v2) {
|
6
|
+
@return if($v1 < $v2, $v1, $v2);
|
7
|
+
}
|
8
|
+
|
9
|
+
@function ch-gcd($a, $b) {
|
10
|
+
// From: http://rosettacode.org/wiki/Greatest_common_divisor#JavaScript
|
11
|
+
@if ($b != 0) {
|
12
|
+
@return ch-gcd($b, $a % $b);
|
13
|
+
} @else {
|
14
|
+
@return abs($a);
|
15
|
+
}
|
16
|
+
}
|
17
|
+
|
18
|
+
@function ch-pow($base, $exponent, $prec: 12) {
|
19
|
+
// Handles decimal exponents by trying to convert them into a fraction and then use a nthRoot-algorithm for parts of the calculation
|
20
|
+
@if (floor($exponent) != $exponent) {
|
21
|
+
$prec2 : ch-pow(10, $prec);
|
22
|
+
$exponent: round($exponent * $prec2);
|
23
|
+
$denominator: ch-gcd($exponent, $prec2);
|
24
|
+
@return ch-nth-root(ch-pow($base, $exponent / $denominator), $prec2 / $denominator, $prec);
|
25
|
+
}
|
26
|
+
|
27
|
+
$value: $base;
|
28
|
+
@if $exponent > 1 {
|
29
|
+
@for $i from 2 through $exponent {
|
30
|
+
$value: $value * $base;
|
31
|
+
}
|
32
|
+
} @else if $exponent < 1 {
|
33
|
+
@for $i from 0 through -$exponent {
|
34
|
+
$value: $value / $base;
|
35
|
+
}
|
36
|
+
}
|
37
|
+
|
38
|
+
@return $value;
|
39
|
+
}
|
40
|
+
|
41
|
+
@function ch-nth-root($num, $n: 2, $prec: 12) {
|
42
|
+
// From: http://rosettacode.org/wiki/Nth_root#JavaScript
|
43
|
+
$x: 1;
|
44
|
+
|
45
|
+
@for $i from 0 through $prec {
|
46
|
+
$x: 1 / $n * (($n - 1) * $x + ($num / ch-pow($x, $n - 1)));
|
47
|
+
}
|
48
|
+
|
49
|
+
@return $x;
|
50
|
+
}
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sass-color-helpers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Pelle Wessman
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-06-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sass
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.2.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.2.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: compass
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.12.1
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.12.1
|
41
|
+
description: Sass Color Helpers
|
42
|
+
email:
|
43
|
+
- pelle@kodfabrik.se
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- README.md
|
49
|
+
- lib/sass-color-helpers.rb
|
50
|
+
- stylesheets/_color-helpers.scss
|
51
|
+
- stylesheets/color-helpers/_alpha-pick.scss
|
52
|
+
- stylesheets/color-helpers/_contrast.scss
|
53
|
+
- stylesheets/color-helpers/_hsv-hsl.scss
|
54
|
+
- stylesheets/color-helpers/_math.scss
|
55
|
+
homepage: https://github.com/voxpelli/sass-color-helpers
|
56
|
+
licenses: []
|
57
|
+
metadata: {}
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: 1.3.6
|
72
|
+
requirements: []
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 2.2.2
|
75
|
+
signing_key:
|
76
|
+
specification_version: 4
|
77
|
+
summary: A collection of Sass color helpers (and some related math helpers) to make
|
78
|
+
it easier and more fool-proof to implement a target design.
|
79
|
+
test_files: []
|