aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/data/color.lux
blob: e53d5d0b7a9f5ce52e197ff99a215106c261ea82 (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
(.require
 [library
  [lux (.except)
   [abstract
    [monad (.only do)]
    [\\specification
     ["$[0]" equivalence]
     ["$[0]" hash]
     ["$[0]" monoid]]]
   [data
    [collection
     ["[0]" list]]]
   [math
    ["[0]" random (.only Random) (.use "[1]#[0]" functor)]
    [number
     ["n" nat]
     ["f" frac]
     ["r" rev]
     ["[0]" int]]]
   [test
    ["_" property (.only Test)]]]]
 [\\library
  ["[0]" / (.only)
   ["[0]" rgb]]]
 ["[0]" /
  ["[1][0]" rgb]
  ["[1][0]" named]
  ["[1][0]" terminal]])

(def .public random
  (Random /.Color)
  (random#each /.of_rgb /rgb.random))

(def scale
  (-> Nat Frac)
  (|>> .int int.frac))

(def square
  (-> Frac Frac)
  (f.pow +2.0))

(def square_root
  (-> Frac Frac)
  (f.pow +0.5))

(def (distance/1 from to)
  (-> Frac Frac Frac)
  (square_root
   (square
    (f.- from to))))

(def (distance/3 from to)
  (-> /.Color /.Color Frac)
  (let [[fr fg fb] (/.rgb from)
        [tr tg tb] (/.rgb to)]
    (square_root
     (all f.+
          (|> (scale (rgb.number tr)) (f.- (scale (rgb.number fr))) square)
          (|> (scale (rgb.number tg)) (f.- (scale (rgb.number fg))) square)
          (|> (scale (rgb.number tb)) (f.- (scale (rgb.number fb))) square)))))

(def rgb_error_margin
  +1.8)

(with_template [<field>]
  [(def (<field> color)
     (-> /.Color Frac)
     (let [[hue saturation luminance] (/.hsl color)]
       <field>))]

  [saturation]
  [luminance]
  )

(def (encoding expected)
  (-> /.Color Test)
  (all _.and
       (_.coverage [/.rgb /.of_rgb]
         (|> expected /.rgb /.of_rgb
             (at /.equivalence = expected)))
       (_.coverage [/.HSL /.hsl /.of_hsl]
         (|> expected /.hsl /.of_hsl
             (distance/3 expected)
             (f.<= ..rgb_error_margin)))
       (_.coverage [/.HSB /.hsb /.of_hsb]
         (|> expected /.hsb /.of_hsb
             (distance/3 expected)
             (f.<= ..rgb_error_margin)))
       (_.coverage [/.CMYK /.cmyk /.of_cmyk]
         (|> expected /.cmyk /.of_cmyk
             (distance/3 expected)
             (f.<= ..rgb_error_margin)))
       ))

(def transformation
  Test
  (do random.monad
    [colorful (|> ..random
                  (random.only (function (_ color) (|> (distance/3 color /.black) (f.>= +100.0))))
                  (random.only (function (_ color) (|> (distance/3 color /.white) (f.>= +100.0)))))
     mediocre (|> ..random
                  (random.only (|>> saturation
                                    ((function (_ saturation)
                                       (and (f.>= +0.25 saturation)
                                            (f.<= +0.75 saturation)))))))
     ratio (|> random.safe_frac (random.only (f.>= +0.5)))]
    (all _.and
         (_.coverage [/.darker /.brighter]
           (and (f.<= (distance/3 colorful /.black)
                      (distance/3 (/.darker ratio colorful) /.black))
                (f.<= (distance/3 colorful /.white)
                      (distance/3 (/.brighter ratio colorful) /.white))))
         (_.coverage [/.interpolated]
           (and (f.<= (distance/3 colorful /.black)
                      (distance/3 (/.interpolated ratio /.black colorful) /.black))
                (f.<= (distance/3 colorful /.white)
                      (distance/3 (/.interpolated ratio /.white colorful) /.white))))
         (_.coverage [/.saturated]
           (f.> (saturation mediocre)
                (saturation (/.saturated ratio mediocre))))
         (_.coverage [/.un_saturated]
           (f.< (saturation mediocre)
                (saturation (/.un_saturated ratio mediocre))))
         (_.coverage [/.gray_scale]
           (let [gray'ed (/.gray_scale mediocre)]
             (and (f.= +0.0
                       (saturation gray'ed))
                  (|> (luminance gray'ed)
                      (f.- (luminance mediocre))
                      f.abs
                      (f.<= ..rgb_error_margin)))))
         )))

(def palette
  Test
  (_.for [/.Spread /.Palette]
         (do [! random.monad]
           [eH (at ! each (|>> f.abs (f.% +0.9) (f.+ +0.05))
                   random.safe_frac)
            .let [eS +0.5]
            variations (at ! each (|>> (n.% 3) (n.+ 2)) random.nat)
            .let [max_spread (f./ (|> variations ++ .int int.frac)
                                  +1.0)
                  min_spread (f./ +2.0 max_spread)
                  spread_space (f.- min_spread max_spread)]
            spread (at ! each (|>> f.abs (f.% spread_space) (f.+ min_spread))
                       random.safe_frac)]
           (`` (all _.and
                    (,, (with_template [<brightness> <palette>]
                          [(_.coverage [<palette>]
                             (let [eB <brightness>
                                   expected (/.of_hsb [eH eS eB])
                                   palette (<palette> spread variations expected)]
                               (and (n.= variations (list.size palette))
                                    (not (list.any? (at /.equivalence = expected) palette)))))]
                          [+1.0 /.analogous]
                          [+0.5 /.monochromatic]
                          ))
                    (,, (with_template [<palette>]
                          [(_.coverage [<palette>]
                             (let [expected (/.of_hsb [eH eS +0.5])
                                   [c0 c1 c2] (<palette> expected)]
                               (and (at /.equivalence = expected c0)
                                    (not (at /.equivalence = expected c1))
                                    (not (at /.equivalence = expected c2)))))]

                          [/.triad]
                          [/.clash]
                          [/.split_complement]))
                    (,, (with_template [<palette>]
                          [(_.coverage [<palette>]
                             (let [expected (/.of_hsb [eH eS +0.5])
                                   [c0 c1 c2 c3] (<palette> expected)]
                               (and (at /.equivalence = expected c0)
                                    (not (at /.equivalence = expected c1))
                                    (not (at /.equivalence = expected c2))
                                    (not (at /.equivalence = expected c3)))))]

                          [/.square]
                          [/.tetradic]))
                    )))))

(def .public test
  Test
  (<| (_.covering /._)
      (_.for [/.Color])
      (do [! random.monad]
        [expected ..random]
        (all _.and
             (_.for [/.equivalence]
                    ($equivalence.spec /.equivalence ..random))
             (_.for [/.hash]
                    ($hash.spec /.hash ..random))
             (_.for [/.addition]
                    ($monoid.spec /.equivalence /.addition ..random))
             (_.for [/.subtraction]
                    ($monoid.spec /.equivalence /.addition ..random))
             
             (..encoding expected)
             (_.coverage [/.complement]
               (let [~expected (/.complement expected)
                     (open "/#[0]") /.equivalence]
                 (and (not (/#= expected ~expected))
                      (/#= expected (/.complement ~expected)))))
             (_.coverage [/.black /.white]
               (and (at /.equivalence = /.white (/.complement /.black))
                    (at /.equivalence = /.black (/.complement /.white))))
             ..transformation
             ..palette
             (_.for [/.Alpha /.Pigment]
                    (all _.and
                         (_.coverage [/.transparent /.opaque]
                           (and (r.= /.opaque (-- /.transparent))
                                (r.= /.transparent (++ /.opaque))))
                         (_.coverage [/.translucent]
                           (r.= /.transparent (r.+ /.translucent /.translucent)))
                         ))

             /rgb.test
             /named.test
             /terminal.test
             ))))