aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/color/hsb.lux
blob: 9bf403cf2edffebd3635af1b99a4ccc8b8d7891b (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
... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

(.require
 [library
  [lux (.except)
   [abstract
    [equivalence (.only Equivalence)]]
   [control
    [function
     [predicate (.only Predicate)]]]
   [math
    [number
     ["f" frac]
     ["[0]" int]]]
   [meta
    [type
     ["[0]" nominal]]]]]
 [//
  ["[0]" rgb (.only RGB)]])

(type .public Value
  Frac)

(with_template [<value> <name>]
  [(def .public <name>
     Value
     <value>)]

  [+0.0 least]
  [+1.0 most]
  )

(def .public (value? it)
  (Predicate Frac)
  (not (or (f.< ..least it)
           (f.> ..most it))))

(def .public value
  (-> Frac
      Value)
  (|>> (f.max ..least)
       (f.min ..most)))

(nominal.def .public HSB
  (Record
   [#hue Value
    #saturation Value
    #brightness Value])

  (def .public equivalence
    (Equivalence HSB)
    (implementation
     (def (= left right)
       (`` (and (,, (with_template [<slot>]
                      [(f.= (the <slot> (nominal.representation left))
                            (the <slot> (nominal.representation right)))]

                      [#hue]
                      [#saturation]
                      [#brightness]
                      )))))))

  (with_template [<name> <slot>]
    [(def .public <name>
       (-> HSB
           Value)
       (|>> nominal.representation
            (the <slot>)))]

    [hue #hue]
    [saturation #saturation]
    [brightness #brightness]
    )

  (def .public (hsb hue saturation brightness)
    (-> Frac Frac Frac
        HSB)
    (nominal.abstraction
     [#hue (..value hue)
      #saturation (..value saturation)
      #brightness (..value brightness)]))

  (def top
    (-- rgb.limit))

  (def rgb_factor
    (|> top .int int.frac))

  (def down
    (-> Nat
        Frac)
    (|>> .int int.frac (f./ rgb_factor)))

  (def up
    (-> Frac
        Nat)
    (|>> (f.* rgb_factor) f.round f.int .nat))

  (def .public (of_rgb it)
    (-> RGB
        HSB)
    (let [red (..down (the rgb.#red it))
          green (..down (the rgb.#green it))
          blue (..down (the rgb.#blue it))

          brightness (all f.max red green blue)
          range (all f.min red green blue)

          chroma (|> brightness (f.- range))
          saturation (if (f.= +0.0 brightness)
                       +0.0
                       (|> chroma (f./ brightness)))]
      (nominal.abstraction
       [#hue (cond (f.= +0.0 chroma)
                   ... Achromatic
                   +0.0
                   ... Chromatic
                   (and (f.= brightness red)
                        (not (f.= red blue)))
                   (|> green (f.- blue)
                       (f./ chroma)
                       (f.+ +0.0)
                       (f./ +6.0))

                   (f.= brightness green)
                   (|> blue (f.- red)
                       (f./ chroma)
                       (f.+ +2.0)
                       (f./ +6.0))

                   ... (f.= brightness blue)
                   (|> red (f.- green)
                       (f./ chroma)
                       (f.+ +4.0)
                       (f./ +6.0)))
        #saturation saturation
        #brightness brightness])))

  (def .public (rgb it)
    (-> HSB
        RGB)
    (let [[hue saturation brightness] (nominal.representation it)
          hue (|> hue (f.* +6.0))
          i (f.floor hue)
          f (|> hue (f.- i))
          p (|> +1.0 (f.- saturation) (f.* brightness))
          q (|> +1.0 (f.- (f.* f saturation)) (f.* brightness))
          t (|> +1.0 (f.- (|> +1.0 (f.- f) (f.* saturation))) (f.* brightness))
          v brightness
          mod (|> i (f.% +6.0) f.int .nat)
          
          red (when mod 0 v 1 q 2 p 3 p 4 t 5 v _ (undefined))
          green (when mod 0 t 1 v 2 v 3 q 4 p 5 p _ (undefined))
          blue (when mod 0 p 1 p 2 t 3 v 4 v 5 q _ (undefined))]
      (rgb.rgb (..up red)
               (..up green)
               (..up blue))))
  )