-
Notifications
You must be signed in to change notification settings - Fork 1
/
function-settings.scss
100 lines (96 loc) · 4.44 KB
/
function-settings.scss
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
@import "settings";
/// функция возвращает преобразованную переменную
@function settings($key) {
@if map-has-key($settings, $key) {
@return _color(#{ map-get($settings, $key) });
}
@return null;
}
/// Преобразование строки в цвет
/// Parses a JSON encoded string to see if it's a CSS color
/// @access private
/// @param {String} $string - JSON string
/// @return {Color | String} - string or number, depending on the match
/// @require {function} _from-hex
/// @require {function} _from-rgb
/// @require {function} _from-hsl
@function _color($string) {
@if type-of($string) == "color" {
@return $string;
}
$string-lower: to-lower-case($string);
$colors: transparent;
//$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;
$keywords: ();
// Filling $keywords with stringified color keywords
@each $color in $colors {
$keywords: append($keywords, $color + "");
}
// Deal with inherit keyword
@if $string-lower == "inherit" {
@return unquote($string);
}
@if index($keywords, $string-lower) {
// Deal with color keywords
@return nth($colors, index($keywords, $string-lower));
} @else if str-slice($string-lower, 1, 1) == '#' {
// Deal with hexadecimal triplets
@return _from-hex($string);
} @else {
// Return string
@return $string;
}
}
/// Cast a JSON encoded string into a hexadecimal color
/// @access private
/// @param {String} $string - JSON string
/// @return {Color | String} - string or hex color depending on the match
/// @require {function} _hex-to-dec
@function _from-hex($string) {
$string-lower: to-lower-case($string);
$r: ""; $g: ""; $b: "";
$hex: "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "a" "b" "c" "d" "e" "f";
$length: str-length($string);
$max: if($length == 4, 1, 2);
// Check for length accuracy
@if $length != 4 and $length != 7 {
@return $string;
}
// Loop from the second character (omitting #)
@for $i from 2 through $length {
$c: str-slice($string-lower, $i, $i);
// If wrong character, return
@if index($hex, $c) == null {
@return $string;
}
@if str-length($r) < $max {
$r: $r + $c;
} @else if str-length($g) < $max {
$g: $g + $c;
} @else if str-length($b) < $max {
$b: $b + $c;
}
}
@if $length == 4 {
$r: $r + $r;
$g: $g + $g;
$b: $b + $b;
}
@return rgb(_hex-to-dec($r), _hex-to-dec($g), _hex-to-dec($b));
}
/// Convert an hexadecimal number to a decimal number
/// @access private
/// @param {String} $string - hexadecimal value
/// @return {Number} - decimal number
@function _hex-to-dec($string) {
$hex: "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "a" "b" "c" "d" "e" "f";
$string: to-lower-case($string);
$length: str-length($string);
$dec: 0;
@for $i from 1 through $length {
$factor: 1 + (15 * ($length - $i));
$index: index($hex, str-slice($string, $i, $i));
$dec: $dec + $factor * ($index - 1);
}
@return $dec;
}