SassyCast 1.1.2 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/README.md +14 -112
- data/lib/SassyCast.rb +15 -9
- data/stylesheets/_options.scss +39 -0
- data/stylesheets/{private/bool → bool}/_bool.scss +2 -2
- data/stylesheets/color/_color.scss +52 -0
- data/stylesheets/color/helpers/_from-hex.scss +52 -0
- data/stylesheets/color/helpers/_from-hsl.scss +46 -0
- data/stylesheets/color/helpers/_from-rgb.scss +46 -0
- data/stylesheets/{private/color → color}/helpers/_hex-to-dec.scss +5 -8
- data/stylesheets/helpers/throw.scss +15 -0
- data/stylesheets/list/_list.scss +19 -0
- data/stylesheets/map/_map.scss +31 -0
- data/stylesheets/{private/null → null}/_null.scss +1 -1
- data/stylesheets/number/_number.scss +75 -0
- data/stylesheets/{private/number → number}/helpers/_find-digits.scss +1 -1
- data/stylesheets/{private/number → number}/helpers/_find-integer.scss +1 -1
- data/stylesheets/{private/number → number}/helpers/_pow.scss +0 -0
- data/stylesheets/number/helpers/_unit.scss +15 -0
- data/stylesheets/{private/string → string}/_string.scss +2 -2
- metadata +20 -29
- data/stylesheets/SassyCast.scss +0 -2
- data/stylesheets/private/_all.scss +0 -16
- data/stylesheets/private/color/_color.scss +0 -55
- data/stylesheets/private/color/helpers/_from-hex.scss +0 -39
- data/stylesheets/private/color/helpers/_from-hsl.scss +0 -53
- data/stylesheets/private/color/helpers/_from-rgb.scss +0 -54
- data/stylesheets/private/color/helpers/_get-color-value.scss +0 -21
- data/stylesheets/private/list/_list.scss +0 -26
- data/stylesheets/private/map/_map.scss +0 -28
- data/stylesheets/private/number/_number.scss +0 -59
- data/stylesheets/private/number/helpers/_length.scss +0 -17
- data/stylesheets/public/_all.scss +0 -7
- data/stylesheets/public/bool/_bool.scss +0 -3
- data/stylesheets/public/color/_color.scss +0 -3
- data/stylesheets/public/list/_list.scss +0 -3
- data/stylesheets/public/map/_map.scss +0 -3
- data/stylesheets/public/null/_null.scss +0 -3
- data/stylesheets/public/number/_number.scss +0 -3
- data/stylesheets/public/string/_string.scss +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0c1556381a6b01b0f805f92e2d09cc42a1ae1fe7
|
4
|
+
data.tar.gz: 415763f8aaf4c3f34c41ea65508a4461be8630f5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 56999187e7973b5c2b447db8ecf5819d8c1c155db67a03e78c8664a67bb9344b93efbc8d62836d081e13d0c354842310558d771cac291bb0d37d13766c25fdde
|
7
|
+
data.tar.gz: 56117204566f9c156200f644d72a54b5dd0fb52ff4231c564175b3e28021ed3c701175d02b1a7880edb6450185ab553d434d39ecb6d23aa0567961eccdd28906
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,14 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
* `2.0.0`:
|
4
|
+
* No more distinction between public and private APIs. The public API (`to-*` functions) remains unchanged, but the private one (`_sc-to-*` functions) has been removed.
|
5
|
+
* A strict mode toggle has been added (`$sc-strict-mode`): SassyCast will throw when not able to convert to color / number, or return a default value with a warning in non-strict mode.
|
6
|
+
* Default value for color casting in non-strict mode is now `transparent`, not `null`, to make conversion always return the expected type.
|
7
|
+
* Default value for color casting in non-strict mode can be configured through `$sc-non-strict-default-color` variable.
|
8
|
+
* Default value for number casting in non-strict mode can be configured through `$sc-non-strict-default-number` variable.
|
9
|
+
* `$keep` parameter for `to-list(..)` does not exist anymore.
|
10
|
+
* `to-number(..)` now correctly cast stringified numbers with units `deg`, `rad`, `grad`, `turn`, `s`, `ms`, `dpi`, `dpcm`, `dppx`, `Hz` and `kHz`.
|
11
|
+
* Code quality has been dramatically improved.
|
3
12
|
* `1.1.1`: improved things here and there and added SassDoc compliant comments
|
4
13
|
* `1.1.0`: dissociated error checking from function logic, and fixed some minor things
|
5
14
|
* `1.0.0`: `to-list` improvements, stable API
|
data/README.md
CHANGED
@@ -1,136 +1,38 @@
|
|
1
|
-
SassyCast
|
2
|
-
=========
|
1
|
+
# SassyCast
|
3
2
|
|
4
3
|
SassyCast is a simple API for type conversion in Sass. Read [API documentation](http://hugogiraudel.com/SassyCast).
|
5
4
|
|
6
|
-
|
5
|
+
## Installation
|
7
6
|
|
8
|
-
Clone repository or download [from Github](https://github.com/HugoGiraudel/SassyCast/archive/master.zip).
|
9
|
-
|
10
|
-
## Install via ruby gems
|
11
7
|
```
|
12
8
|
gem install SassyCast
|
13
|
-
````
|
14
|
-
|
15
|
-
## Install via bower
|
16
|
-
```
|
17
|
-
bower install sassy-cast
|
18
|
-
```
|
19
|
-
|
20
|
-
## `to-bool`
|
21
|
-
|
22
|
-
``` scss
|
23
|
-
to-bool(false); // false
|
24
|
-
to-bool(0); // false
|
25
|
-
to-bool(null); // false
|
26
|
-
to-bool(""); // false
|
27
|
-
to-bool(true); // true
|
28
|
-
to-bool(1); // true
|
29
|
-
to-bool(()); // true
|
30
|
-
to-bool(string); // true
|
31
|
-
to-bool("quoted string"); // true
|
32
|
-
to-bool(this is a list); // true
|
33
|
-
to-bool((this: is a map)); // true
|
34
|
-
to-bool(1337); // true
|
35
|
-
to-bool(#000); // true
|
36
9
|
```
|
37
10
|
|
38
|
-
## `to-number`
|
39
|
-
|
40
|
-
``` scss
|
41
|
-
to-number(1337); // 1337
|
42
|
-
to-number("0"); // 0
|
43
|
-
to-number("42"); // 42
|
44
|
-
to-number("4.2"); // 4.2
|
45
|
-
to-number("-10"); // -10
|
46
|
-
to-number("-10px"); // -10px
|
47
|
-
to-number(true); // 1
|
48
|
-
to-number(false); // 0
|
49
|
-
to-number(string); // 0
|
50
|
-
to-number(this is a list); // 0
|
51
|
-
to-number((this: is a map)); // 0
|
52
|
-
to-number(null); // 0
|
53
|
-
to-number(#000); // 0
|
54
11
|
```
|
55
|
-
|
56
|
-
## `to-list`
|
57
|
-
|
58
|
-
``` scss
|
59
|
-
to-list(0); // 0
|
60
|
-
to-list(1); // 1
|
61
|
-
to-list(string); // string
|
62
|
-
to-list(this is a list); // this is a list
|
63
|
-
to-list(this: is a map); // this, is a map
|
64
|
-
to-list(true); // true
|
65
|
-
to-list(false); // false
|
66
|
-
to-list(null); // null
|
67
|
-
to-list(#000); // #333
|
12
|
+
bower install sassy-cast
|
68
13
|
```
|
69
14
|
|
70
|
-
##
|
15
|
+
## Notes
|
71
16
|
|
72
|
-
|
73
|
-
to-string(0); // "0"
|
74
|
-
to-string(1); // "1"
|
75
|
-
to-string(true); // "true"
|
76
|
-
to-string(false); // "false"
|
77
|
-
to-string(null); // "null"
|
78
|
-
to-string(this is a list); // "this is a list"
|
79
|
-
to-string(this, is, a, list); // "this, is, a, list"
|
80
|
-
to-string((this: is a map)); // "(this: is a map)"
|
81
|
-
to-string((this: is, a, map)); // "(this: is, a, map)"
|
82
|
-
to-string(#000); // "#333"
|
83
|
-
```
|
84
|
-
|
85
|
-
## `to-null`
|
17
|
+
SassCast has a strict mode in which it will throw errors when failing to cast values (most notably to colors and numbers). You can enable strict-mode with:
|
86
18
|
|
87
|
-
```
|
88
|
-
|
89
|
-
to-null(0); // null
|
90
|
-
to-null(1); // null
|
91
|
-
to-null(string); // null
|
92
|
-
to-null(this is a list); // null
|
93
|
-
to-null(this: is a map); // null
|
94
|
-
to-null(true); // null
|
95
|
-
to-null(false); // null
|
96
|
-
to-null(#333); // null
|
19
|
+
```scss
|
20
|
+
$sc-strict-mode: true;
|
97
21
|
```
|
98
22
|
|
99
|
-
|
23
|
+
In non-strict mode, when a value cannot be converted to a number, SassyCast will warn and return `0`. You can change this value with:
|
100
24
|
|
101
|
-
```
|
102
|
-
|
103
|
-
to-map(1); // (1: 1)
|
104
|
-
to-map(true); // (1: true)
|
105
|
-
to-map(false); // (1: false)
|
106
|
-
to-map(null); // (1: null)
|
107
|
-
to-map(string); // (1: string)
|
108
|
-
to-map(this is a list); // (1: this, 2: is, 3: a, 4: map)
|
109
|
-
to-map(this: is a map); // (this: is a map)
|
25
|
+
```scss
|
26
|
+
$sc-non-strict-default-number: 0;
|
110
27
|
```
|
111
28
|
|
112
|
-
|
29
|
+
In non-strict mode, when a value cannot be converted to a color, SassyCast will warn and return `transparent`. You can change this value with:
|
113
30
|
|
114
|
-
```
|
115
|
-
|
116
|
-
to-color(1); // null
|
117
|
-
to-color(black); // black
|
118
|
-
to-color(#000); // black
|
119
|
-
to-color("#000"); // black
|
120
|
-
to-color(true); // null
|
121
|
-
to-color(false); // null
|
122
|
-
to-color(null); // null
|
123
|
-
to-color(string); // null
|
124
|
-
to-color(this is a list); // null
|
125
|
-
to-color((this: is a map)); // null
|
31
|
+
```scss
|
32
|
+
$sc-non-strict-default-color: transparent;
|
126
33
|
```
|
127
34
|
|
128
|
-
|
129
|
-
|
130
|
-
* When trying to cast something to a number that cannot be converted, it returns `0`
|
131
|
-
* When trying to cast something to a color that cannot be converted, it returns `null`
|
132
|
-
* Casting to a map use `1` as key (and index in case of a list)
|
133
|
-
* Color formats are sometimes converted automatically by Sass; when casting a color to string, the resulting string can be different from the color input
|
35
|
+
Note that color formats are sometimes converted automatically by Sass depending on the type of syntaxe used (compressed, expanded, etc.). When casting a color to string, the resulting string can be different from the color input.
|
134
36
|
|
135
37
|
## Credits
|
136
38
|
|
data/lib/SassyCast.rb
CHANGED
@@ -1,14 +1,20 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
base_directory = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
2
|
+
stylesheets_path = File.join(base_directory, 'stylesheets')
|
3
|
+
|
4
|
+
if (defined? Compass)
|
5
|
+
Compass::Frameworks.register('SassyCast', :stylesheets_directory => stylesheets_path)
|
6
|
+
else
|
7
|
+
# compass not found, register on the Sass path via the environment.
|
8
|
+
if ENV.has_key?("SASS_PATH")
|
9
|
+
ENV["SASS_PATH"] = ENV["SASS_PATH"] + File::PATH_SEPARATOR + stylesheets_path
|
10
|
+
else
|
11
|
+
ENV["SASS_PATH"] = stylesheets_path
|
12
|
+
end
|
13
|
+
end
|
4
14
|
|
5
15
|
# Version is a number. If a version contains alphas, it will be created as a prerelease version
|
6
16
|
# Date is in the form of YYYY-MM-DD
|
7
17
|
module SassyCast
|
8
|
-
VERSION = "
|
9
|
-
DATE = "2016-
|
10
|
-
end
|
11
|
-
|
12
|
-
module Sass::Script::Functions
|
13
|
-
|
18
|
+
VERSION = "2.0.0"
|
19
|
+
DATE = "2016-06-03"
|
14
20
|
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
/// Toggle strict mode, in which script will throw when not able to cast a value
|
2
|
+
/// into a certain type (mostly color and number). In non-strict mode, it will
|
3
|
+
/// return the default value from the given type.
|
4
|
+
/// @access public
|
5
|
+
/// @type Bool
|
6
|
+
$sc-strict-mode: false !default;
|
7
|
+
|
8
|
+
/// Default return value for the `to-number(..)` function when running in
|
9
|
+
/// non-strict mode.
|
10
|
+
/// @access public
|
11
|
+
/// @type Number
|
12
|
+
$sc-non-strict-default-number: 0 !default;
|
13
|
+
|
14
|
+
/// Default return value for the `to-color(..)` function when running in
|
15
|
+
/// non-strict mode.
|
16
|
+
/// @access public
|
17
|
+
/// @type Number
|
18
|
+
$sc-non-strict-default-color: transparent !default;
|
19
|
+
|
20
|
+
/// Internal map for dynamically accessing default values for non-strict mode.
|
21
|
+
/// @access private
|
22
|
+
/// @type Map
|
23
|
+
$sc-non-strict-defaults: (
|
24
|
+
'number': $sc-non-strict-default-number,
|
25
|
+
'color': $sc-non-strict-default-color,
|
26
|
+
);
|
27
|
+
|
28
|
+
/// Internal constants map.
|
29
|
+
/// @access private
|
30
|
+
/// @type Map
|
31
|
+
$sc-constants: (
|
32
|
+
'DECIMAL_SPACE': ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'),
|
33
|
+
'HEXADECIMAL_SPACE': ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'),
|
34
|
+
'COLOR_KEYWORDS': (transparent, aliceblue, antiquewhite, aqua, aquamarine, azure, beige, bisque, black, blanchedalmond, blue, blueviolet, brown, burlywood, cadetblue, chartreuse, chocolate, coral, cornflowerblue, cornsilk, crimson, cyan, darkblue, darkcyan, darkgoldenrod, darkgray, darkgreen, darkgrey, darkkhaki, darkmagenta, darkolivegreen, darkorange, darkorchid, darkred, darksalmon, darkseagreen, darkslateblue, darkslategray, darkslategrey, darkturquoise, darkviolet, deeppink, deepskyblue, dimgray, dimgrey, dodgerblue, firebrick, floralwhite, forestgreen, fuchsia, gainsboro, ghostwhite, gold, goldenrod, gray, green, greenyellow, grey, honeydew, hotpink, indianred, indigo, ivory, khaki, lavender, lavenderblush, lawngreen, lemonchiffon, lightblue, lightcoral, lightcyan, lightgoldenrodyellow, lightgray, lightgreen, lightgrey, lightpink, lightsalmon, lightseagreen, lightskyblue, lightslategray, lightslategrey, lightsteelblue, lightyellow, lime, limegreen, linen, magenta, maroon, mediumaquamarine, mediumblue, mediumorchid, mediumpurple, mediumseagreen, mediumslateblue, mediumspringgreen, mediumturquoise, mediumvioletred, midnightblue, mintcream, mistyrose, moccasin, navajowhite, navy, oldlace, olive, olivedrab, orange, orangered, orchid, palegoldenrod, palegreen, paleturquoise, palevioletred, papayawhip, peachpuff, peru, pink, plum, powderblue, purple, rebeccapurple, red, rosybrown, royalblue, saddlebrown, salmon, sandybrown, seagreen, seashell, sienna, silver, skyblue, slateblue, slategray, slategrey, snow, springgreen, steelblue, tan, teal, thistle, tomato, turquoise, violet, wheat, white, whitesmoke, yellow, yellowgreen),
|
35
|
+
'STRINGIFIED_COLOR_KEYWORDS': ('transparent', 'aliceblue', 'antiquewhite', 'aqua', 'aquamarine', 'azure', 'beige', 'bisque', 'black', 'blanchedalmond', 'blue', 'blueviolet', 'brown', 'burlywood', 'cadetblue', 'chartreuse', 'chocolate', 'coral', 'cornflowerblue', 'cornsilk', 'crimson', 'cyan', 'darkblue', 'darkcyan', 'darkgoldenrod', 'darkgray', 'darkgreen', 'darkgrey', 'darkkhaki', 'darkmagenta', 'darkolivegreen', 'darkorange', 'darkorchid', 'darkred', 'darksalmon', 'darkseagreen', 'darkslateblue', 'darkslategray', 'darkslategrey', 'darkturquoise', 'darkviolet', 'deeppink', 'deepskyblue', 'dimgray', 'dimgrey', 'dodgerblue', 'firebrick', 'floralwhite', 'forestgreen', 'fuchsia', 'gainsboro', 'ghostwhite', 'gold', 'goldenrod', 'gray', 'green', 'greenyellow', 'grey', 'honeydew', 'hotpink', 'indianred', 'indigo', 'ivory', 'khaki', 'lavender', 'lavenderblush', 'lawngreen', 'lemonchiffon', 'lightblue', 'lightcoral', 'lightcyan', 'lightgoldenrodyellow', 'lightgray', 'lightgreen', 'lightgrey', 'lightpink', 'lightsalmon', 'lightseagreen', 'lightskyblue', 'lightslategray', 'lightslategrey', 'lightsteelblue', 'lightyellow', 'lime', 'limegreen', 'linen', 'magenta', 'maroon', 'mediumaquamarine', 'mediumblue', 'mediumorchid', 'mediumpurple', 'mediumseagreen', 'mediumslateblue', 'mediumspringgreen', 'mediumturquoise', 'mediumvioletred', 'midnightblue', 'mintcream', 'mistyrose', 'moccasin', 'navajowhite', 'navy', 'oldlace', 'olive', 'olivedrab', 'orange', 'orangered', 'orchid', 'palegoldenrod', 'palegreen', 'paleturquoise', 'palevioletred', 'papayawhip', 'peachpuff', 'peru', 'pink', 'plum', 'powderblue', 'purple', 'rebeccapurple', 'red', 'rosybrown', 'royalblue', 'saddlebrown', 'salmon', 'sandybrown', 'seagreen', 'seashell', 'sienna', 'silver', 'skyblue', 'slateblue', 'slategray', 'slategrey', 'snow', 'springgreen', 'steelblue', 'tan', 'teal', 'thistle', 'tomato', 'turquoise', 'violet', 'wheat', 'white', 'whitesmoke', 'yellow', 'yellowgreen'),
|
36
|
+
'UNITS': ('px', 'cm', 'mm', '%', 'ch', 'pc', 'in', 'em', 'rem', 'pt', 'ex', 'vw', 'vh', 'vmin', 'vmax', 'ms', 's', 'deg', 'rad', 'grad', 'turn', 'Hz', 'kHz', 'dpi', 'dpcm', 'dppx'),
|
37
|
+
'UNIT_VALUES': (1px, 1cm, 1mm, 1%, 1ch, 1pc, 1in, 1em, 1rem, 1pt, 1ex, 1vw, 1vh, 1vmin, 1vmax, 1ms, 1s, 1deg, 1rad, 1grad, 1turn, 1Hz, 1kHz, 1dpi, 1dpcm, 1dppx),
|
38
|
+
'FALSEY_VALUES': (false, null, '', 0),
|
39
|
+
);
|
@@ -1,8 +1,8 @@
|
|
1
1
|
/// Convert to bool
|
2
2
|
/// @param {*} $value - value to cast
|
3
3
|
/// @return {Bool}
|
4
|
-
@function
|
5
|
-
@if (
|
4
|
+
@function to-bool($value) {
|
5
|
+
@if index(map-get($sc-constants, 'FALSEY_VALUES'), $value) {
|
6
6
|
@return false;
|
7
7
|
}
|
8
8
|
|
@@ -0,0 +1,52 @@
|
|
1
|
+
/// Convert to color
|
2
|
+
/// @param {*} $value - value to cast
|
3
|
+
/// @return {color | null}
|
4
|
+
@function to-color($value) {
|
5
|
+
$type: type-of($value);
|
6
|
+
|
7
|
+
// If the value is already a color, we can safely return it.
|
8
|
+
@if ($type == 'color') {
|
9
|
+
@return $value;
|
10
|
+
}
|
11
|
+
|
12
|
+
// If the type is not a string, there is no way we can convert it to a color,
|
13
|
+
// so we warn and return the default color value (or throw in strict mode).
|
14
|
+
@if ($type != 'string') {
|
15
|
+
@return _sc-throw($value, 'color');
|
16
|
+
}
|
17
|
+
|
18
|
+
$value-lower: to-lower-case($value);
|
19
|
+
|
20
|
+
// If the given value is a stringified color keyword, return the accurate
|
21
|
+
// color keyword.
|
22
|
+
$color-keyword-index: index(
|
23
|
+
map-get($sc-constants, 'STRINGIFIED_COLOR_KEYWORDS'),
|
24
|
+
$value-lower
|
25
|
+
);
|
26
|
+
|
27
|
+
@if $color-keyword-index {
|
28
|
+
@return nth(map-get($sc-constants, 'COLOR_KEYWORDS'), $color-keyword-index);
|
29
|
+
}
|
30
|
+
|
31
|
+
// If the color starts with a hash, assume it’s a hexadecimal color triplet
|
32
|
+
// and try to convert it to an actual color.
|
33
|
+
@else if (str-slice($value-lower, 1, 1) == '#') {
|
34
|
+
@return _sc-from-hex($value);
|
35
|
+
}
|
36
|
+
|
37
|
+
// If the color starts with `rgb`, assume it’s a rgb(..) function call and try
|
38
|
+
// to convert it to an actual color.
|
39
|
+
@else if (str-slice($value-lower, 1, 3) == 'rgb') {
|
40
|
+
@return _sc-from-rgb($value);
|
41
|
+
}
|
42
|
+
|
43
|
+
// If the color starts with `hsl`, assume it’s a hsl(..) function call and try
|
44
|
+
// to convert it to an actual color.
|
45
|
+
@else if (str-slice($value-lower, 1, 3) == 'hsl') {
|
46
|
+
@return _sc-from-hsl($value);
|
47
|
+
}
|
48
|
+
|
49
|
+
// If everything has failed, arn and return the default color value (or throw
|
50
|
+
// in strict mode).
|
51
|
+
@return _sc-throw($value, 'color');
|
52
|
+
}
|
@@ -0,0 +1,52 @@
|
|
1
|
+
/// Cast a string into a hexadecimal color
|
2
|
+
/// @access private
|
3
|
+
/// @param {string} $string - string
|
4
|
+
/// @return {Color | String} - string or hex color depending on the match
|
5
|
+
/// @require {function} _sc-hex-to-dec
|
6
|
+
@function _sc-from-hex($string) {
|
7
|
+
$string: to-lower-case($string);
|
8
|
+
$r: '';
|
9
|
+
$g: '';
|
10
|
+
$b: '';
|
11
|
+
$hex: map-get($sc-constants, 'HEXADECIMAL_SPACE');
|
12
|
+
$length: str-length($string);
|
13
|
+
$max: if($length == 4, 1, 2);
|
14
|
+
|
15
|
+
// If the length of the value is neither 4, nor 7, it cannot be a valid
|
16
|
+
// hexadecimal triplet, therefore we warn and return the default color value
|
17
|
+
// (or throw in strict mode).
|
18
|
+
@if ($length != 4 and $length != 7) {
|
19
|
+
@return _sc-throw($string, 'color');
|
20
|
+
}
|
21
|
+
|
22
|
+
// Loop through the characters from the value, starting from the second one
|
23
|
+
// to omit the initial hash.
|
24
|
+
@for $i from 2 through $length {
|
25
|
+
$c: str-slice($string, $i, $i);
|
26
|
+
|
27
|
+
// If the character is not part of the hexadecimal space, the color cannot
|
28
|
+
// be a valid hexadecimal triplet, therefore we warn and return the default
|
29
|
+
// color value (or throw in strict mode).
|
30
|
+
@if not index($hex, $c) {
|
31
|
+
@return _sc-throw($string, 'color');
|
32
|
+
}
|
33
|
+
|
34
|
+
@if (str-length($r) < $max) {
|
35
|
+
$r: ($r + $c);
|
36
|
+
} @else if (str-length($g) < $max) {
|
37
|
+
$g: ($g + $c);
|
38
|
+
} @else if (str-length($b) < $max) {
|
39
|
+
$b: ($b + $c);
|
40
|
+
}
|
41
|
+
}
|
42
|
+
|
43
|
+
// If the triplet is a short one, duplicated each found valud to compose a
|
44
|
+
// long version, necessary for the _sc-hex-to-dec(..) calls.
|
45
|
+
@if $length == 4 {
|
46
|
+
$r: $r + $r;
|
47
|
+
$g: $g + $g;
|
48
|
+
$b: $b + $b;
|
49
|
+
}
|
50
|
+
|
51
|
+
@return rgb(_sc-hex-to-dec($r), _sc-hex-to-dec($g), _sc-hex-to-dec($b));
|
52
|
+
}
|
@@ -0,0 +1,46 @@
|
|
1
|
+
/// Cast a string representation of a hsl(..) call into a color
|
2
|
+
/// @access private
|
3
|
+
/// @param {string} $string - string
|
4
|
+
/// @return {Color} - Color
|
5
|
+
@function _sc-from-hsl($value) {
|
6
|
+
$value: to-lower-case($value);
|
7
|
+
$channels: ('h': '', 's': '', 'l': '', 'a': '');
|
8
|
+
$channel-keys: map-keys($channels);
|
9
|
+
$current-channel: nth($channel-keys, 1);
|
10
|
+
$discarded-characters: join($channel-keys, (' ', '(', ')'));
|
11
|
+
|
12
|
+
@for $i from 1 through str-length($value) {
|
13
|
+
$character: str-slice($value, $i, $i);
|
14
|
+
|
15
|
+
// If the character is neither the part of the function name, nor a space or
|
16
|
+
// a parenthese, we proceed further.
|
17
|
+
@if not index($discarded-characters, $character) {
|
18
|
+
// If the current character is a comma, it means we change the current
|
19
|
+
// channel to the next one.
|
20
|
+
@if ($character == ',') {
|
21
|
+
$current-channel-index: index($channel-keys, $current-channel);
|
22
|
+
$current-channel: nth($channel-keys, $current-channel-index + 1);
|
23
|
+
}
|
24
|
+
|
25
|
+
// If the current character is not a comma, it means it is part of the
|
26
|
+
// value for the current channel and therefore should be saved.
|
27
|
+
@else {
|
28
|
+
$current-channel-value: map-get($channels, $current-channel);
|
29
|
+
$channels: map-merge($channels, ($current-channel: $current-channel-value + $character));
|
30
|
+
}
|
31
|
+
}
|
32
|
+
}
|
33
|
+
|
34
|
+
$hue: to-number(map-get($channels, 'h'));
|
35
|
+
$saturation: to-number(map-get($channels, 's'));
|
36
|
+
$lightness: to-number(map-get($channels, 'l'));
|
37
|
+
$alpha: map-get($channels, 'a');
|
38
|
+
|
39
|
+
// If there is no alpha channel registered, return a classic hsl(..) call.
|
40
|
+
@if ($alpha == '') {
|
41
|
+
@return hsl($hue, $saturation, $lightness);
|
42
|
+
}
|
43
|
+
|
44
|
+
// If there is a registered alpha channel, return a hsla(..) call.
|
45
|
+
@return hsla($hue, $saturation, $lightness, to-number($alpha));
|
46
|
+
}
|
@@ -0,0 +1,46 @@
|
|
1
|
+
/// Cast a string representation of a rgb(..) call into a color
|
2
|
+
/// @access private
|
3
|
+
/// @param {String} $string - string
|
4
|
+
/// @return {Color} - Color
|
5
|
+
@function _sc-from-rgb($value) {
|
6
|
+
$value: to-lower-case($value);
|
7
|
+
$channels: ('r': '', 'g': '', 'b': '', 'a': '');
|
8
|
+
$channel-keys: map-keys($channels);
|
9
|
+
$current-channel: nth($channel-keys, 1);
|
10
|
+
$discarded-characters: join($channel-keys, (' ', '(', ')'));
|
11
|
+
|
12
|
+
@for $i from 1 through str-length($value) {
|
13
|
+
$character: str-slice($value, $i, $i);
|
14
|
+
|
15
|
+
// If the character is neither the part of the function name, nor a space or
|
16
|
+
// a parenthese, we proceed further.
|
17
|
+
@if not index($discarded-characters, $character) {
|
18
|
+
// If the current character is a comma, it means we change the current
|
19
|
+
// channel to the next one.
|
20
|
+
@if ($character == ',') {
|
21
|
+
$current-channel-index: index($channel-keys, $current-channel);
|
22
|
+
$current-channel: nth($channel-keys, $current-channel-index + 1);
|
23
|
+
}
|
24
|
+
|
25
|
+
// If the current character is not a comma, it means it is part of the
|
26
|
+
// value for the current channel and therefore should be saved.
|
27
|
+
@else {
|
28
|
+
$current-channel-value: map-get($channels, $current-channel);
|
29
|
+
$channels: map-merge($channels, ($current-channel: $current-channel-value + $character));
|
30
|
+
}
|
31
|
+
}
|
32
|
+
}
|
33
|
+
|
34
|
+
$red: to-number(map-get($channels, 'r'));
|
35
|
+
$green: to-number(map-get($channels, 'g'));
|
36
|
+
$blue: to-number(map-get($channels, 'b'));
|
37
|
+
$alpha: map-get($channels, 'a');
|
38
|
+
|
39
|
+
// If there is no alpha channel registered, return a classic hsl(..) call.
|
40
|
+
@if ($alpha == '') {
|
41
|
+
@return rgb($red, $green, $blue);
|
42
|
+
}
|
43
|
+
|
44
|
+
// If there is a registered alpha channel, return a rgba(..) call.
|
45
|
+
@return rgba($red, $green, $blue, to-number($alpha));
|
46
|
+
}
|
@@ -5,18 +5,15 @@
|
|
5
5
|
@function _sc-hex-to-dec($string) {
|
6
6
|
$string: to-lower-case($string);
|
7
7
|
$length: str-length($string);
|
8
|
-
$hex: (
|
9
|
-
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
10
|
-
'a', 'b', 'c', 'd', 'e', 'f'
|
11
|
-
);
|
8
|
+
$hex: map-get($sc-constants, 'HEXADECIMAL_SPACE');
|
12
9
|
|
13
|
-
$
|
10
|
+
$decimal: 0;
|
14
11
|
|
15
12
|
@for $i from 1 through $length {
|
16
|
-
$factor: 1 + (
|
13
|
+
$factor: 1 + (length($hex) * ($length - $i));
|
17
14
|
$index: index($hex, str-slice($string, $i, $i));
|
18
|
-
$
|
15
|
+
$decimal: $decimal + $factor * ($index - 1);
|
19
16
|
}
|
20
17
|
|
21
|
-
@return $
|
18
|
+
@return $decimal;
|
22
19
|
}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/// Helper function to throw when running in strict mode, or warn and return
|
2
|
+
/// default value for expected type when running in non-strict mode.
|
3
|
+
/// @param {*} $value
|
4
|
+
/// @param {String} $expected-type
|
5
|
+
@function _sc-throw($value, $expected-type) {
|
6
|
+
$return-value: map-get($sc-non-strict-defaults, $expected-type);
|
7
|
+
|
8
|
+
@if $sc-strict-mode {
|
9
|
+
@error 'Could not cast `#{inspect($value)}` to #{$expected-type}.';
|
10
|
+
} @else {
|
11
|
+
@warn 'Could not cast `#{inspect($value)}` to #{$expected-type}; returning `#{$return-value}`.';
|
12
|
+
}
|
13
|
+
|
14
|
+
@return $return-value;
|
15
|
+
}
|
@@ -0,0 +1,19 @@
|
|
1
|
+
/// Convert to list
|
2
|
+
/// @param {*} $value - value to cast
|
3
|
+
/// @return {List}
|
4
|
+
@function to-list($value) {
|
5
|
+
$type: type-of($value);
|
6
|
+
|
7
|
+
// If the value is already a list, we can safely return it.
|
8
|
+
@if ($type == 'list') {
|
9
|
+
@return $value;
|
10
|
+
}
|
11
|
+
|
12
|
+
// If the value is a map, we return a zip of keys and values (aka a list-map).
|
13
|
+
@if ($type == 'map') {
|
14
|
+
@return zip(map-keys($value), map-values($value));
|
15
|
+
}
|
16
|
+
|
17
|
+
// Else we wrap the item in a single-item list.
|
18
|
+
@return ($value,);
|
19
|
+
}
|
@@ -0,0 +1,31 @@
|
|
1
|
+
/// Convert to map
|
2
|
+
/// @param {*} $value - value to cast
|
3
|
+
/// @return {Map}
|
4
|
+
@function to-map($value) {
|
5
|
+
$type: type-of($value);
|
6
|
+
|
7
|
+
// If the value is already a map, we can safely return it.
|
8
|
+
@if ($type == 'map') {
|
9
|
+
@return $value;
|
10
|
+
}
|
11
|
+
|
12
|
+
// If the type is a list, we return an empty list if it has a no item or we
|
13
|
+
// map each item to its index in the list.
|
14
|
+
@if ($type == 'list') {
|
15
|
+
$length: length($value);
|
16
|
+
|
17
|
+
@if ($length == 0) {
|
18
|
+
@return ();
|
19
|
+
}
|
20
|
+
|
21
|
+
$map: ();
|
22
|
+
|
23
|
+
@for $i from 1 through $length {
|
24
|
+
$map: map-merge($map, ($i: nth($value, $i)));
|
25
|
+
}
|
26
|
+
|
27
|
+
@return $map;
|
28
|
+
}
|
29
|
+
|
30
|
+
@return (1: $value);
|
31
|
+
}
|
@@ -0,0 +1,75 @@
|
|
1
|
+
/// Cast a value to a number if possible or return 0
|
2
|
+
/// @param {String} $value - complete source
|
3
|
+
/// @return {Number}
|
4
|
+
@function to-number($value) {
|
5
|
+
$type: type-of($value);
|
6
|
+
|
7
|
+
// If the value is already a number, we can safely return it.
|
8
|
+
@if ($type == 'number') {
|
9
|
+
@return $value;
|
10
|
+
}
|
11
|
+
|
12
|
+
// If the value is the `true` boolean, we return 1.
|
13
|
+
@if ($value == true) {
|
14
|
+
@return 1;
|
15
|
+
}
|
16
|
+
|
17
|
+
// If the value is the `false` boolean, we return 0.
|
18
|
+
@if ($value == false) {
|
19
|
+
@return 0;
|
20
|
+
}
|
21
|
+
|
22
|
+
// If the type is not a string, there is no way we can convert it to a number,
|
23
|
+
// so we warn and return the default number value (or throw in strict mode).
|
24
|
+
@if ($type != 'string') {
|
25
|
+
@return _sc-throw($value, 'number');
|
26
|
+
}
|
27
|
+
|
28
|
+
// At this point we have discarded all the edge cases so we can start trying
|
29
|
+
// to cast the value into a number.
|
30
|
+
$pointer: 1;
|
31
|
+
$result: 0;
|
32
|
+
$first-character: str-slice($value, $pointer, $pointer);
|
33
|
+
$allowed-first-character: join(('-', '.'), map-get($sc-constants, 'DECIMAL_SPACE'));
|
34
|
+
|
35
|
+
// We perform an early check for errors. If value starts with neither a number
|
36
|
+
// nor a minus sign, it cannot be casted to a number. Therefore we warn and
|
37
|
+
// return the default number value (or throw in strict mode).
|
38
|
+
@if not index($allowed-first-character, $first-character) {
|
39
|
+
@return _sc-throw($value, 'number');
|
40
|
+
}
|
41
|
+
|
42
|
+
// In case the value starts with a dot, we assume it is a float and pad it
|
43
|
+
// with a zero.
|
44
|
+
@if ($first-character == '.') {
|
45
|
+
$value: '0' + $value;
|
46
|
+
}
|
47
|
+
|
48
|
+
// We find the integer part of the value.
|
49
|
+
$find-integer: _sc-find-integer($value, $pointer);
|
50
|
+
$pointer: nth($find-integer, 1);
|
51
|
+
$result: nth($find-integer, 2);
|
52
|
+
|
53
|
+
// If there still is a dot, it means we have to check for the digits part of
|
54
|
+
// the value.
|
55
|
+
@if (str-slice($value, $pointer, $pointer) == '.') {
|
56
|
+
$find-digits: _sc-find-digits($value, $pointer);
|
57
|
+
$pointer: nth($find-digits, 1);
|
58
|
+
$digits: nth($find-digits, 2);
|
59
|
+
$result: ($result + $digits);
|
60
|
+
}
|
61
|
+
|
62
|
+
// If the first character is a minus symbol, it means the number is negative
|
63
|
+
// so we can multiply it per -1.
|
64
|
+
@if ($first-character == '-') {
|
65
|
+
$result: ($result * -1);
|
66
|
+
}
|
67
|
+
|
68
|
+
// If we are still not done with the evaluation, it means there could be a CSS
|
69
|
+
// unit, so we check for it.
|
70
|
+
@if ($pointer <= str-length($value)) {
|
71
|
+
$result: _sc-unit($result, str-slice($value, $pointer));
|
72
|
+
}
|
73
|
+
|
74
|
+
@return $result;
|
75
|
+
}
|
@@ -6,7 +6,7 @@
|
|
6
6
|
@function _sc-find-integer($source, $pointer) {
|
7
7
|
$source: to-lower-case($source);
|
8
8
|
$length: str-length($source);
|
9
|
-
$numbers: (
|
9
|
+
$numbers: map-get($sc-constants, 'DECIMAL_SPACE');
|
10
10
|
$result: 0;
|
11
11
|
|
12
12
|
@while ($pointer <= $length) {
|
File without changes
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/// Tries to find a unit that would match a CSS length
|
2
|
+
/// @access private
|
3
|
+
/// @param {Number} $number - number
|
4
|
+
/// @param {String} $unit - potential unit
|
5
|
+
/// @return {Number} length (0 if cast failed)
|
6
|
+
@function _sc-unit($number, $unit) {
|
7
|
+
$units: map-get($sc-constants, 'UNIT_VALUES');
|
8
|
+
$index: index(map-get($sc-constants, 'UNITS'), $unit);
|
9
|
+
|
10
|
+
@if not $index {
|
11
|
+
@return _sc-throw($number, 'number');
|
12
|
+
}
|
13
|
+
|
14
|
+
@return ($number * nth($units, $index));
|
15
|
+
}
|
@@ -1,9 +1,10 @@
|
|
1
1
|
/// Convert to string
|
2
2
|
/// @param {*} $value - value to cast
|
3
3
|
/// @return {String}
|
4
|
-
@function
|
4
|
+
@function to-string($value) {
|
5
5
|
$type: type-of($value);
|
6
6
|
|
7
|
+
// If the value is already a string, we can safely return it.
|
7
8
|
@if ($type == 'string') {
|
8
9
|
@return $value;
|
9
10
|
}
|
@@ -12,6 +13,5 @@
|
|
12
13
|
@warn 'Beware! Sass does some color conversion. The resulting string may be different from the color input.';
|
13
14
|
}
|
14
15
|
|
15
|
-
|
16
16
|
@return inspect($value);
|
17
17
|
}
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: SassyCast
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hugo Giraudel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Sass API for type conversion.
|
14
14
|
email:
|
@@ -20,32 +20,23 @@ files:
|
|
20
20
|
- CHANGELOG.md
|
21
21
|
- README.md
|
22
22
|
- lib/SassyCast.rb
|
23
|
-
- stylesheets/
|
24
|
-
- stylesheets/
|
25
|
-
- stylesheets/
|
26
|
-
- stylesheets/
|
27
|
-
- stylesheets/
|
28
|
-
- stylesheets/
|
29
|
-
- stylesheets/
|
30
|
-
- stylesheets/
|
31
|
-
- stylesheets/
|
32
|
-
- stylesheets/
|
33
|
-
- stylesheets/
|
34
|
-
- stylesheets/
|
35
|
-
- stylesheets/
|
36
|
-
- stylesheets/
|
37
|
-
- stylesheets/
|
38
|
-
- stylesheets/
|
39
|
-
- stylesheets/
|
40
|
-
- stylesheets/private/string/_string.scss
|
41
|
-
- stylesheets/public/_all.scss
|
42
|
-
- stylesheets/public/bool/_bool.scss
|
43
|
-
- stylesheets/public/color/_color.scss
|
44
|
-
- stylesheets/public/list/_list.scss
|
45
|
-
- stylesheets/public/map/_map.scss
|
46
|
-
- stylesheets/public/null/_null.scss
|
47
|
-
- stylesheets/public/number/_number.scss
|
48
|
-
- stylesheets/public/string/_string.scss
|
23
|
+
- stylesheets/_options.scss
|
24
|
+
- stylesheets/bool/_bool.scss
|
25
|
+
- stylesheets/color/_color.scss
|
26
|
+
- stylesheets/color/helpers/_from-hex.scss
|
27
|
+
- stylesheets/color/helpers/_from-hsl.scss
|
28
|
+
- stylesheets/color/helpers/_from-rgb.scss
|
29
|
+
- stylesheets/color/helpers/_hex-to-dec.scss
|
30
|
+
- stylesheets/helpers/throw.scss
|
31
|
+
- stylesheets/list/_list.scss
|
32
|
+
- stylesheets/map/_map.scss
|
33
|
+
- stylesheets/null/_null.scss
|
34
|
+
- stylesheets/number/_number.scss
|
35
|
+
- stylesheets/number/helpers/_find-digits.scss
|
36
|
+
- stylesheets/number/helpers/_find-integer.scss
|
37
|
+
- stylesheets/number/helpers/_pow.scss
|
38
|
+
- stylesheets/number/helpers/_unit.scss
|
39
|
+
- stylesheets/string/_string.scss
|
49
40
|
homepage: https://github.com/HugoGiraudel/SassyCast/
|
50
41
|
licenses: []
|
51
42
|
metadata: {}
|
@@ -65,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
65
56
|
version: 1.3.6
|
66
57
|
requirements: []
|
67
58
|
rubyforge_project: SassyCast
|
68
|
-
rubygems_version: 2.
|
59
|
+
rubygems_version: 2.4.6
|
69
60
|
signing_key:
|
70
61
|
specification_version: 4
|
71
62
|
summary: SassyCast is a Sass-powered API for type conversion.
|
data/stylesheets/SassyCast.scss
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
@import "bool/bool";
|
2
|
-
@import "number/helpers/find-integer";
|
3
|
-
@import "number/helpers/find-digits";
|
4
|
-
@import "number/helpers/pow";
|
5
|
-
@import "number/helpers/length";
|
6
|
-
@import "number/number";
|
7
|
-
@import "string/string";
|
8
|
-
@import "list/list";
|
9
|
-
@import "map/map";
|
10
|
-
@import "null/null";
|
11
|
-
@import "color/helpers/from-rgb";
|
12
|
-
@import "color/helpers/from-hsl";
|
13
|
-
@import "color/helpers/from-hex";
|
14
|
-
@import "color/helpers/get-color-value";
|
15
|
-
@import "color/helpers/hex-to-dec";
|
16
|
-
@import "color/color";
|
@@ -1,55 +0,0 @@
|
|
1
|
-
/// Convert to color
|
2
|
-
/// @param {*} $value - value to cast
|
3
|
-
/// @return {color | null}
|
4
|
-
@function _sc-to-color($value) {
|
5
|
-
$type: type-of($value);
|
6
|
-
|
7
|
-
@if ($type == 'color') {
|
8
|
-
@return $value;
|
9
|
-
}
|
10
|
-
|
11
|
-
@if ($type != 'string') {
|
12
|
-
//@warn 'Could not cast `#{inspect($value)}` to color.';
|
13
|
-
@return null;
|
14
|
-
}
|
15
|
-
|
16
|
-
$value-lower: to-lower-case($value);
|
17
|
-
$colors: transparent black silver gray white maroon red purple fuchsia green lime olive yellow navy blue teal aqua aliceblue antiquewhite aqua aquamarine azure beige bisque black blanchedalmond blue blueviolet brown burlywood cadetblue chartreuse chocolate coral cornflowerblue cornsilk crimson cyan darkblue darkcyan darkgoldenrod darkgray darkgreen darkgrey darkkhaki darkmagenta darkolivegreen darkorange darkorchid darkred darksalmon darkseagreen darkslateblue darkslategray darkslategrey darkturquoise darkviolet deeppink deepskyblue dimgray dimgrey dodgerblue firebrick floralwhite forestgreen fuchsia gainsboro ghostwhite gold goldenrod gray green greenyellow grey honeydew hotpink indianred indigo ivory khaki lavender lavenderblush lawngreen lemonchiffon lightblue lightcoral lightcyan lightgoldenrodyellow lightgray lightgreen lightgrey lightpink lightsalmon lightseagreen lightskyblue lightslategray lightslategrey lightsteelblue lightyellow lime limegreen linen magenta maroon mediumaquamarine mediumblue mediumorchid mediumpurple mediumseagreen mediumslateblue mediumspringgreen mediumturquoise mediumvioletred midnightblue mintcream mistyrose moccasin navajowhite navy oldlace olive olivedrab orange orangered orchid palegoldenrod palegreen paleturquoise palevioletred papayawhip peachpuff peru pink plum powderblue purple red rosybrown royalblue saddlebrown salmon sandybrown seagreen seashell sienna silver skyblue slateblue slategray slategrey snow springgreen steelblue tan teal thistle tomato turquoise violet wheat white whitesmoke yellow yellowgreen;
|
18
|
-
$keywords: ();
|
19
|
-
|
20
|
-
// Filling $keywords with stringified color keywords
|
21
|
-
@each $color in $colors {
|
22
|
-
$keywords: append($keywords, $color + '');
|
23
|
-
}
|
24
|
-
|
25
|
-
// Deal with inherit keyword
|
26
|
-
@if ($value-lower == 'inherit') {
|
27
|
-
@return unquote($value);
|
28
|
-
}
|
29
|
-
|
30
|
-
// Deal with color keywords
|
31
|
-
@if index($keywords, $value-lower) {
|
32
|
-
@return nth($colors, index($keywords, $value-lower));
|
33
|
-
}
|
34
|
-
|
35
|
-
// Deal with hexadecimal triplets
|
36
|
-
@else if (str-slice($value-lower, 1, 1) == '#') {
|
37
|
-
@return _sc-from-hex($value);
|
38
|
-
}
|
39
|
-
|
40
|
-
// Deal with rgb(a) colors
|
41
|
-
@else if (str-slice($value-lower, 1, 3) == 'rgb') {
|
42
|
-
@return _sc-from-rgb($value);
|
43
|
-
}
|
44
|
-
|
45
|
-
// Deal with hsl(a) colors
|
46
|
-
@else if (str-slice($value-lower, 1, 3) == 'hsl') {
|
47
|
-
@return _sc-from-hsl($value);
|
48
|
-
}
|
49
|
-
|
50
|
-
// Return value
|
51
|
-
@else {
|
52
|
-
//@warn 'Could not cast `#{inspect($value)}` to color.';
|
53
|
-
@return null;
|
54
|
-
}
|
55
|
-
}
|
@@ -1,39 +0,0 @@
|
|
1
|
-
/// Cast a string into a hexadecimal color
|
2
|
-
/// @access private
|
3
|
-
/// @param {string} $string - string
|
4
|
-
/// @return {Color | String} - string or hex color depending on the match
|
5
|
-
/// @require {function} _hex-to-dec
|
6
|
-
@function _sc-from-hex($string) {
|
7
|
-
$string-lower: to-lower-case($string);
|
8
|
-
$r: ''; $g: ''; $b: '';
|
9
|
-
$hex: '0' '1' '2' '3' '4' '5' '6' '7' '8' '9' 'a' 'b' 'c' 'd' 'e' 'f';
|
10
|
-
$length: str-length($string);
|
11
|
-
$max: if($length == 4, 1, 2);
|
12
|
-
|
13
|
-
// Check for length accuracy
|
14
|
-
@if ($length != 4 and $length != 7) {
|
15
|
-
@return $string;
|
16
|
-
}
|
17
|
-
|
18
|
-
// Loop from the second character (omitting #)
|
19
|
-
@for $i from 2 through $length {
|
20
|
-
$c: str-slice($string-lower, $i, $i);
|
21
|
-
|
22
|
-
// If wrong character, return
|
23
|
-
@if not index($hex, $c) {
|
24
|
-
@return $string;
|
25
|
-
}
|
26
|
-
|
27
|
-
@if str-length($r) < $max { $r: $r + $c }
|
28
|
-
@else if str-length($g) < $max { $g: $g + $c }
|
29
|
-
@else if str-length($b) < $max { $b: $b + $c }
|
30
|
-
}
|
31
|
-
|
32
|
-
@if $length == 4 {
|
33
|
-
$r: $r + $r;
|
34
|
-
$g: $g + $g;
|
35
|
-
$b: $b + $b;
|
36
|
-
}
|
37
|
-
|
38
|
-
@return rgb(_sc-hex-to-dec($r), _sc-hex-to-dec($g), _sc-hex-to-dec($b));
|
39
|
-
}
|
@@ -1,53 +0,0 @@
|
|
1
|
-
/// Cast a string into a hsl color
|
2
|
-
/// @access private
|
3
|
-
/// @param {string} $string - string
|
4
|
-
/// @return {Color | String} - string or hsl color depending on the match
|
5
|
-
/// @require {function} _get-color-value
|
6
|
-
@function _sc-from-hsl($string) {
|
7
|
-
$frags: ();
|
8
|
-
$string-lower: to-lower-case($string);
|
9
|
-
$is-alpha: str-slice($string-lower, 4, 4) == 'a';
|
10
|
-
$length: str-length($string);
|
11
|
-
$start: str-index($string, "(");
|
12
|
-
|
13
|
-
@for $i from $start through $length {
|
14
|
-
$token: str-slice($string-lower, $i, $i);
|
15
|
-
@if ($token == ' ') {
|
16
|
-
// @continue;
|
17
|
-
} @else if ($token == '(' or $token == ',') {
|
18
|
-
$frags: append($frags, "");
|
19
|
-
} @else if ($token == ')') {
|
20
|
-
@if (length($frags) != if($is-alpha, 4, 3)) {
|
21
|
-
@return $string; // Parsing error
|
22
|
-
}
|
23
|
-
|
24
|
-
$hue: _sc-get-color-value(nth($frags, 1));
|
25
|
-
$saturation: _sc-get-color-value(nth($frags, 2));
|
26
|
-
$lightness: _sc-get-color-value(nth($frags, 3));
|
27
|
-
|
28
|
-
@if (not $hue or not $saturation or not $lightness) {
|
29
|
-
@return $string;
|
30
|
-
}
|
31
|
-
|
32
|
-
@if $is-alpha {
|
33
|
-
@if (length($frags) != 4) {
|
34
|
-
@return $string; // No alpha channel found
|
35
|
-
}
|
36
|
-
|
37
|
-
$alpha: _sc-get-color-value(nth($frags, 4));
|
38
|
-
|
39
|
-
@if not $alpha {
|
40
|
-
@return $string; // Error parsing alpha channel
|
41
|
-
}
|
42
|
-
|
43
|
-
@return hsla($hue, $saturation, $lightness, $alpha);
|
44
|
-
}
|
45
|
-
|
46
|
-
@return hsl($hue, $saturation, $lightness);
|
47
|
-
} @else {
|
48
|
-
$frags: set-nth($frags, length($frags), nth($frags, length($frags)) + $token);
|
49
|
-
}
|
50
|
-
}
|
51
|
-
|
52
|
-
@return $string;
|
53
|
-
}
|
@@ -1,54 +0,0 @@
|
|
1
|
-
/// Cast a string into a rgb color
|
2
|
-
/// @access private
|
3
|
-
/// @param {String} $string - string
|
4
|
-
/// @return {Color | String} - string or rgb color depending on the match
|
5
|
-
/// @require {function} _get-color-value
|
6
|
-
@function _sc-from-rgb($string) {
|
7
|
-
$string-lower: to-lower-case($string);
|
8
|
-
$frags: ();
|
9
|
-
$is-alpha: str-slice($string-lower, 4, 4) == 'a';
|
10
|
-
$start: str-index($string, '(');
|
11
|
-
$length: str-length($string);
|
12
|
-
|
13
|
-
@for $i from $start through $length {
|
14
|
-
$token: str-slice($string-lower, $i, $i);
|
15
|
-
|
16
|
-
@if ($token == ' ') {
|
17
|
-
// @continue;
|
18
|
-
} @else if ($token == '(' or $token == ',') {
|
19
|
-
$frags: append($frags, '');
|
20
|
-
} @else if ($token == ')') {
|
21
|
-
@if length($frags) != if($is-alpha, 4, 3) {
|
22
|
-
@return $string; // Parsing error
|
23
|
-
}
|
24
|
-
|
25
|
-
$red: _sc-get-color-value(nth($frags, 1));
|
26
|
-
$green: _sc-get-color-value(nth($frags, 2));
|
27
|
-
$blue: _sc-get-color-value(nth($frags, 3));
|
28
|
-
|
29
|
-
@if (not $red or not $green or not $blue) {
|
30
|
-
@return $string;
|
31
|
-
}
|
32
|
-
|
33
|
-
@if $is-alpha {
|
34
|
-
@if length($frags) != 4 {
|
35
|
-
@return $string; // No alpha channel found
|
36
|
-
}
|
37
|
-
|
38
|
-
$alpha: _sc-get-color-value(nth($frags, 4));
|
39
|
-
|
40
|
-
@if not $alpha {
|
41
|
-
@return $string; // Error parsing alpha channel
|
42
|
-
}
|
43
|
-
|
44
|
-
@return rgba($red, $green, $blue, $alpha);
|
45
|
-
}
|
46
|
-
|
47
|
-
@return rgb($red, $green, $blue);
|
48
|
-
} @else {
|
49
|
-
$frags: set-nth($frags, length($frags), nth($frags, length($frags)) + $token);
|
50
|
-
}
|
51
|
-
}
|
52
|
-
|
53
|
-
@return $string;
|
54
|
-
}
|
@@ -1,21 +0,0 @@
|
|
1
|
-
/// Cast a stringified number / stringified percentage into number type
|
2
|
-
/// @access private
|
3
|
-
/// @param {string} $string - string
|
4
|
-
/// @return {Number} - unitless number or percentage
|
5
|
-
/// @require {function} to-number
|
6
|
-
@function _sc-get-color-value($string) {
|
7
|
-
$first: str-slice($string, 1, 1);
|
8
|
-
|
9
|
-
// Pad <1 values with a leading 0
|
10
|
-
@if ($first == '.') {
|
11
|
-
$string: '0' + $string;
|
12
|
-
}
|
13
|
-
|
14
|
-
$last: str-slice($string, -1, -1);
|
15
|
-
|
16
|
-
@if ($last == '%') {
|
17
|
-
@return _sc-to-number(str-slice($string, 1, -2), 2) * 1%;
|
18
|
-
}
|
19
|
-
|
20
|
-
@return _sc-to-number($string);
|
21
|
-
}
|
@@ -1,26 +0,0 @@
|
|
1
|
-
/// Convert to list
|
2
|
-
/// @param {*} $value - value to cast
|
3
|
-
/// @param {String} $keep ('both') - whether to keep `keys`, `values` or `both` when casting a map
|
4
|
-
/// @return {List}
|
5
|
-
@function _sc-to-list($value, $keep: 'both') {
|
6
|
-
$type: type-of($value);
|
7
|
-
|
8
|
-
@if ($type == 'list') {
|
9
|
-
@return $value;
|
10
|
-
}
|
11
|
-
|
12
|
-
@if ($type == 'map') {
|
13
|
-
$keys: map-keys($value);
|
14
|
-
$values: map-values($value);
|
15
|
-
|
16
|
-
@if ($keep == 'values') {
|
17
|
-
@return $values;
|
18
|
-
} @else if ($keep == 'keys') {
|
19
|
-
@return $keys;
|
20
|
-
} @else {
|
21
|
-
@return zip($keys, $values);
|
22
|
-
}
|
23
|
-
}
|
24
|
-
|
25
|
-
@return ($value,);
|
26
|
-
}
|
@@ -1,28 +0,0 @@
|
|
1
|
-
/// Convert to map
|
2
|
-
/// @param {*} $value - value to cast
|
3
|
-
/// @return {Map}
|
4
|
-
@function _sc-to-map($value) {
|
5
|
-
$type: type-of($value);
|
6
|
-
|
7
|
-
@if ($type == 'map') {
|
8
|
-
@return $value;
|
9
|
-
}
|
10
|
-
|
11
|
-
@if ($type == 'list') {
|
12
|
-
$length: length($value);
|
13
|
-
|
14
|
-
@if ($length == 0) {
|
15
|
-
@return ();
|
16
|
-
} @else {
|
17
|
-
$map: ();
|
18
|
-
|
19
|
-
@for $i from 1 through $length {
|
20
|
-
$map: map-merge($map, ($i: nth($value, $i)));
|
21
|
-
}
|
22
|
-
|
23
|
-
@return $map;
|
24
|
-
}
|
25
|
-
}
|
26
|
-
|
27
|
-
@return (1: $value);
|
28
|
-
}
|
@@ -1,59 +0,0 @@
|
|
1
|
-
/// Cast a value to a number if possible or return 0
|
2
|
-
/// @param {String} $value - complete source
|
3
|
-
/// @return {Number}
|
4
|
-
@function _sc-to-number($value) {
|
5
|
-
$type: type-of($value);
|
6
|
-
|
7
|
-
// In case everything's good
|
8
|
-
@if ($type == 'number') {
|
9
|
-
@return $value;
|
10
|
-
}
|
11
|
-
|
12
|
-
// Boolean to number
|
13
|
-
@if ($value == true) {
|
14
|
-
@return 1;
|
15
|
-
}
|
16
|
-
|
17
|
-
// Fail
|
18
|
-
@if ($type != 'string') {
|
19
|
-
@return 0;
|
20
|
-
}
|
21
|
-
|
22
|
-
// Trying to cast
|
23
|
-
$pointer: 1;
|
24
|
-
$result: 0;
|
25
|
-
$allowed: ('-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'); // Allowed characted to start with
|
26
|
-
$first: str-slice($value, $pointer, $pointer); // First character of the number
|
27
|
-
$minus: $first == '-'; // Is it negative?
|
28
|
-
|
29
|
-
// Early check for errors
|
30
|
-
@if not index($allowed, $first) {
|
31
|
-
@warn 'Could not cast `#{inspect($value)}` into number.';
|
32
|
-
@return 0;
|
33
|
-
}
|
34
|
-
|
35
|
-
// Find the integer part
|
36
|
-
$find-integer: _sc-find-integer($value, $pointer);
|
37
|
-
$pointer: nth($find-integer, 1);
|
38
|
-
$result: nth($find-integer, 2);
|
39
|
-
|
40
|
-
// Find digits
|
41
|
-
@if (str-slice($value, $pointer, $pointer) == '.') {
|
42
|
-
$find-digits: _sc-find-digits($value, $pointer);
|
43
|
-
$pointer: nth($find-digits, 1);
|
44
|
-
$digits: nth($find-digits, 2);
|
45
|
-
$result: ($result + $digits);
|
46
|
-
}
|
47
|
-
|
48
|
-
// In case of negative
|
49
|
-
@if $minus {
|
50
|
-
$result: ($result * -1);
|
51
|
-
}
|
52
|
-
|
53
|
-
// In case of possible CSS unit
|
54
|
-
@if ($pointer < str-length($value)) {
|
55
|
-
$result: _sc-length($result, str-slice($value, $pointer));
|
56
|
-
}
|
57
|
-
|
58
|
-
@return $result;
|
59
|
-
}
|
@@ -1,17 +0,0 @@
|
|
1
|
-
/// Tries to find a unit that would match a CSS length
|
2
|
-
/// @access private
|
3
|
-
/// @param {Number} $number - number
|
4
|
-
/// @param {String} $unit - potential unit
|
5
|
-
/// @return {Number} length (0 if cast failed)
|
6
|
-
@function _sc-length($number, $unit) {
|
7
|
-
$strings: 'px' 'cm' 'mm' '%' 'ch' 'pc' 'in' 'em' 'rem' 'pt' 'pc' 'ex' 'vw' 'vh' 'vmin' 'vmax';
|
8
|
-
$units: 1px 1cm 1mm 1% 1ch 1pc 1in 1em 1rem 1pt 1pc 1ex 1vw 1vh 1vmin 1vmax;
|
9
|
-
$index: index($strings, $unit);
|
10
|
-
|
11
|
-
@if not $index {
|
12
|
-
@warn 'Unknown unit `#{inspect($unit)}`.';
|
13
|
-
@return 0;
|
14
|
-
}
|
15
|
-
|
16
|
-
@return ($number * nth($units, $index));
|
17
|
-
}
|