-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHex.elm
176 lines (138 loc) · 3.53 KB
/
Hex.elm
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
module Hex exposing (darken, lighten)
import String
import Char
import List
import Color
cutHash : String -> String
cutHash hexColor =
if String.startsWith "#" hexColor
then String.dropLeft 1 hexColor
else hexColor
fixLength : String -> String
fixLength hexColor =
case String.length hexColor of
3 -> hexColor
|> String.toList
|> List.map (\n -> String.fromChar n)
|> List.map (\n -> String.repeat 2 n)
|> String.join ""
6 -> hexColor
default -> "FFFFFF"
getHexInt : Char -> Int
getHexInt char =
if Char.isDigit char
then getIntFromChar char
else getIntFromHexChar char
getIntFromHexChar : Char -> Int
getIntFromHexChar char =
case Char.toLower char of
'a' -> 10
'b' -> 11
'c' -> 12
'd' -> 13
'e' -> 14
'f' -> 15
default -> 0
getIntFromChar : Char -> Int
getIntFromChar char =
case String.toInt (String.fromChar char) of
Ok value ->
value
Err error ->
0
getDecimalIntFromHexString : String -> Int
getDecimalIntFromHexString str =
str
|> String.toList
|> List.map (\n -> getHexInt n)
|> List.reverse
|> List.indexedMap (,)
|> List.map (\(index, n) -> n * 16^index )
|> List.sum
getR : String -> Int
getR color =
color
|> String.slice 0 2
|> getDecimalIntFromHexString
getG : String -> Int
getG color =
color
|> String.slice 2 4
|> getDecimalIntFromHexString
getB : String -> Int
getB color =
color
|> String.slice 4 6
|> getDecimalIntFromHexString
toRgb : String -> Color.Color
toRgb hexColor =
let cleanColor = hexColor
|> cutHash
|> fixLength
in
Color.rgb (getR cleanColor) (getG cleanColor) (getB cleanColor)
decimalToHexString : Int -> String
decimalToHexString decimal =
let remainder = decimal % 16
quotient = decimal // 16
in
if quotient > 16 then
(decimalToHexString quotient) ++ (hexToString remainder)
else
(hexToString quotient) ++ (hexToString remainder)
hexToString : Int -> String
hexToString decimal =
if decimal <= 9
then toString decimal
else case decimal of
10 -> "A"
11 -> "B"
12 -> "C"
13 -> "D"
14 -> "E"
15 -> "F"
default -> ""
makeDarker : Float -> Color.Color -> Color.Color
makeDarker percentage color =
let hsl = Color.toHsl color
lightness = adjustLightness color percentage (-)
in
Color.hsla hsl.hue hsl.saturation lightness hsl.alpha
makeLighter : Float -> Color.Color -> Color.Color
makeLighter percentage color =
let hsl = Color.toHsl color
lightness = adjustLightness color percentage (+)
in
Color.hsla hsl.hue hsl.saturation lightness hsl.alpha
adjustLightness : Color.Color -> Float -> (Float -> Float -> number) -> Float
adjustLightness color percentage adjust =
let hslValues = Color.toHsl color
pc = percentageToFloat percentage
in
hslValues
|> .lightness
|> adjust pc
|> abs
percentageToFloat : Float -> Float
percentageToFloat percentage =
percentage / 100
toCssHexString : Color.Color -> String
toCssHexString color =
let rgb = Color.toRgb color
red = decimalToHexString rgb.red
green = decimalToHexString rgb.green
blue = decimalToHexString rgb.blue
in
"#" ++ red ++ green ++ blue
darken : String -> Float -> String
darken cssHexColorString amount =
cssHexColorString
|> toRgb
|> makeDarker amount
|> toCssHexString
lighten : String -> Float -> String
lighten cssHexColorString amount =
cssHexColorString
|> toRgb
|> makeLighter amount
|> toCssHexString