-
Notifications
You must be signed in to change notification settings - Fork 28
/
colors.clj
141 lines (118 loc) · 3.77 KB
/
colors.clj
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
(ns cljss.css.colors
(:require [clojure.spec.alpha :as s]
[cljss.css.units :as units]))
;;
;; Keyword colors
;;
(s/def ::keyword-colors
#{:current-color :transparent
:aquamarine :lime :deepskyblue :darksalmon :antiquewhite
:mediumturquoise :slategrey :slategray :sienna :orange :navajowhite
:lavenderblush :firebrick :orangered :palevioletred :lawngreen
:seashell :lightpink :darkolivegreen :aliceblue :gray :lightsteelblue
:whitesmoke :darkgoldenrod :tan :bisque :white :lightgreen
:darkseagreen :crimson :darkslategray :mistyrose :chocolate :yellow
:cadetblue :navy :ghostwhite :dimgrey :seagreen :green
:mediumseagreen :indigo :olivedrab :cyan :peachpuff :limegreen
:mediumslateblue :violet :sandybrown :yellowgreen :mediumspringgreen
:steelblue :rosybrown :cornflowerblue :ivory :lightgoldenrodyellow
:salmon :darkcyan :peru :cornsilk :lightslategray :blueviolet
:forestgreen :lightseagreen :gold :gainsboro :darkorchid :burlywood
:lightskyblue :chartreuse :snow :moccasin :honeydew :aqua :darkred
:mediumorchid :lightsalmon :saddlebrown :wheat :springgreen
:lightslategrey :darkblue :powderblue :turquoise :blanchedalmond
:papayawhip :slateblue :lightblue :skyblue :red :lightyellow :blue
:palegreen :greenyellow :khaki :maroon :darkgrey :midnightblue
:floralwhite :deeppink :paleturquoise :darkkhaki :azure :indianred
:darkviolet :mediumpurple :fuchsia :coral :mediumvioletred
:lemonchiffon :mediumblue :darkmagenta :goldenrod :darkorange :orchid
:plum :pink :teal :magenta :lightgrey :purple :dodgerblue
:darkturquoise :mintcream :hotpink :thistle :royalblue :darkgreen
:darkslateblue :silver :darkgray :grey :oldlace :mediumaquamarine
:brown :lightgray :olive :lightcoral :tomato :lightcyan :linen
:darkslategrey :lavender :dimgray :palegoldenrod :beige :black})
;;
;; RGB(A) colors
;;
(s/def ::rgb-range
(s/cat
:value
(s/and
number?
#(s/int-in-range? 0 256 %))))
(s/def ::numeric-percentage
(s/alt
:numeric ::rgb-range
:percentage (s/spec ::units/percentage)))
(s/def ::alpha-channel
(s/alt
:numeric
(s/cat
:value
(s/and
number?
float?
#(>= % 0)
#(<= % 1)))))
(s/def ::rgba
(s/cat
:tag #{:rgb :rgba}
:red ::numeric-percentage
:green ::numeric-percentage
:blue ::numeric-percentage
:alpha (s/? ::alpha-channel)))
;;
;; HSL(A) colors
;;
(s/def ::angle-percentage
(s/alt
:numeric ::units/angle
:percentage (s/spec ::units/percentage)))
(s/def ::hsla
(s/cat
:tag #{:hsl :hsla}
:hue ::angle-percentage
:saturation ::angle-percentage
:lightness ::angle-percentage
:alpha (s/? ::alpha-channel)))
;;
;; HEX colors
;;
(s/def ::hex-chars
#{\a \b \c \d \e \f \0 \1 \2 \3 \4 \5 \6 \7 \8 \9})
(s/def ::hex
(s/cat
:tag #{:hex}
:value
(s/and
(s/conformer seq)
(s/+ ::hex-chars)
(s/conformer #(apply str %)))))
;;
;; Generic CSS Color spec
;;
(s/def ::color
(s/or
:keyword ::keyword-colors
:rgba ::rgba
:hsla ::hsla
:hex ::hex))
(comment
(s/conform ::color [:rgba 10 20 190 0.1]))
(defmulti compile-css first)
(defmethod compile-css :keyword [value]
(units/compile-css value))
(defmethod compile-css :rgba [[_ {:keys [red green blue alpha]}]]
(str "rgba("
(->> [red green blue (or alpha [:numeric {:value 1}])]
(map units/compile-css)
(clojure.string/join ", "))
")"))
(defmethod compile-css :hsla [[_ {:keys [hue saturation lightness alpha]}]]
(str "hsla("
(->> [hue saturation lightness (or alpha [:numeric {:value 1}])]
(map units/compile-css)
(clojure.string/join ", "))
")"))
(defmethod compile-css :hex [[_ {:keys [value]}]]
(str "#" value))