@@ -156,6 +156,13 @@ defmodule Scenic.Color do
156
156
yellow_green: { 0x9A , 0xCD , 0x32 }
157
157
}
158
158
159
+ @ g :color_g
160
+ @ ga :color_ga
161
+ @ rgb :color_rgb
162
+ @ rgba :color_rgba
163
+ @ hsv :color_hsv
164
+ @ hsl :color_hsl
165
+
159
166
@ moduledoc """
160
167
APIs to create and work with colors.
161
168
@@ -180,15 +187,15 @@ defmodule Scenic.Color do
180
187
For HSL and HSV, h is a float between 0 and 360, while the s, v and l values
181
188
are floats between 0 and 100.
182
189
183
- | Format | Implicit | Explicit |
184
- |---------------| ------------------------| -----------|
185
- | Named Color | *na* | See the Named Color Table |
186
- | Grayscale | `g` | `{:g , g}` |
187
- | Gray, Alpha | `{g, a}` | `{:g , {g, a}}` |
188
- | Red, Green, Blue | `{r, g, b}` | `{:rgb, {r, g, b}}` |
189
- | Red, Green, Blue, Alpha | `{r, g, b, a}` | `{:rgba, {r, g, b, a}}` |
190
- | Hue, Saturation, Value | *na* | `{:hsv, {h, s, v}}` |
191
- | Hue, Saturation, Lightness | *na* | `{:hsl, {h, s, l}}` |
190
+ | Format | Implicit | Explicit |
191
+ |----------------------------| ----------------|------------------ -----------|
192
+ | Named Color | *na* | See the Named Color Table |
193
+ | Grayscale | `g` | `{:#{ @ g } , g}` |
194
+ | Gray, Alpha | `{g, a}` | `{:#{ @ ga } , {g, a}}` |
195
+ | Red, Green, Blue | `{r, g, b}` | `{:#{ @ rgb } , {r, g, b}}` |
196
+ | Red, Green, Blue, Alpha | `{r, g, b, a}` | `{:#{ @ rgba } , {r, g, b, a}}` |
197
+ | Hue, Saturation, Value | *na* | `{:#{ @ hsv } , {h, s, v}}` |
198
+ | Hue, Saturation, Lightness | *na* | `{:#{ @ hsl } , {h, s, l}}` |
192
199
193
200
194
201
## Named Colors
@@ -204,26 +211,29 @@ defmodule Scenic.Color do
204
211
205
212
## Additional Named Colors
206
213
207
- | Name | Value |
208
- |---------------|------------------------|
209
- | `:clear` | `{0x80, 0x80, 0x80, 0x00}` |
214
+ | Name | Value |
215
+ |---------------|----------------------------- |
216
+ | `:clear` | `{0x80, 0x80, 0x80, 0x00}` |
210
217
| `:transparent` | `{0x80, 0x80, 0x80, 0x00}` |
211
218
212
219
## Converting Between Color Formats
213
220
214
- By using the functions `to_g`, `to_ga`, `to_rgb`, `to_rgb`, `to_hsl`, and `to_hsv`
215
- you can convert between any implicit or explicit color type to any explicit color type.
221
+ By using the functions `to_g/1`, `to_ga/1`, `to_rgb/1`, `to_rgb/1`,
222
+ `to_hsl/1`, and `to_hsv/1` you can convert between any implicit or explicit
223
+ color type to any explicit color type.
216
224
"""
217
225
218
- # import IEx
219
-
220
226
@ g :color_g
221
227
@ ga :color_ga
222
228
@ rgb :color_rgb
223
229
@ rgba :color_rgba
224
230
@ hsv :color_hsv
225
231
@ hsl :color_hsl
226
232
233
+ # Epsilon value from JS
234
+ # https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/EPSILON
235
+ @ epsilon 2.22044e-16
236
+
227
237
@ type implicit ::
228
238
atom
229
239
| { name :: atom , a :: integer }
@@ -534,9 +544,9 @@ defmodule Scenic.Color do
534
544
l = ( max + min ) / 2
535
545
536
546
s =
537
- case delta do
538
- 0 -> 0
539
- d -> d / ( 1 - abs ( 2 * l - 1 ) )
547
+ cond do
548
+ delta < @ epsilon -> 0. 0
549
+ true -> delta / ( 1 - abs ( 2 * l - 1 ) )
540
550
end
541
551
542
552
{ h , s * 100 , l * 100 }
0 commit comments