@@ -156,6 +156,13 @@ defmodule Scenic.Color do
156156 yellow_green: { 0x9A , 0xCD , 0x32 }
157157 }
158158
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+
159166 @ moduledoc """
160167 APIs to create and work with colors.
161168
@@ -180,15 +187,15 @@ defmodule Scenic.Color do
180187 For HSL and HSV, h is a float between 0 and 360, while the s, v and l values
181188 are floats between 0 and 100.
182189
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}}` |
192199
193200
194201 ## Named Colors
@@ -204,26 +211,29 @@ defmodule Scenic.Color do
204211
205212 ## Additional Named Colors
206213
207- | Name | Value |
208- |---------------|------------------------|
209- | `:clear` | `{0x80, 0x80, 0x80, 0x00}` |
214+ | Name | Value |
215+ |---------------|----------------------------- |
216+ | `:clear` | `{0x80, 0x80, 0x80, 0x00}` |
210217 | `:transparent` | `{0x80, 0x80, 0x80, 0x00}` |
211218
212219 ## Converting Between Color Formats
213220
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.
216224 """
217225
218- # import IEx
219-
220226 @ g :color_g
221227 @ ga :color_ga
222228 @ rgb :color_rgb
223229 @ rgba :color_rgba
224230 @ hsv :color_hsv
225231 @ hsl :color_hsl
226232
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+
227237 @ type implicit ::
228238 atom
229239 | { name :: atom , a :: integer }
@@ -534,9 +544,9 @@ defmodule Scenic.Color do
534544 l = ( max + min ) / 2
535545
536546 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 ) )
540550 end
541551
542552 { h , s * 100 , l * 100 }
0 commit comments