diff options
Diffstat (limited to 'stdlib/source/library/lux/data/color/hsb.lux')
-rw-r--r-- | stdlib/source/library/lux/data/color/hsb.lux | 65 |
1 files changed, 38 insertions, 27 deletions
diff --git a/stdlib/source/library/lux/data/color/hsb.lux b/stdlib/source/library/lux/data/color/hsb.lux index 9bf403cf2..87d03137f 100644 --- a/stdlib/source/library/lux/data/color/hsb.lux +++ b/stdlib/source/library/lux/data/color/hsb.lux @@ -9,13 +9,19 @@ [control [function [predicate (.only Predicate)]]] + [data + [text + ["%" \\format]]] [math [number ["f" frac] + ["[0]" nat] ["[0]" int]]] [meta [type - ["[0]" nominal]]]]] + ["[0]" nominal]] + ["[0]" macro + ["[1]" local]]]]] [// ["[0]" rgb (.only RGB)]]) @@ -90,7 +96,7 @@ (def down (-> Nat Frac) - (|>> .int int.frac (f./ rgb_factor))) + (|>> nat.frac (f./ rgb_factor))) (def up (-> Frac @@ -111,37 +117,33 @@ 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]))) + (macro.let [hue_of (template (_ <base> <shift> <adjustment>) + [(|> <base> (f.- <shift>) + (f./ chroma) + <adjustment> + (f./ +6.0))])] + (nominal.abstraction + [#hue (cond (f.= +0.0 chroma) + ... Achromatic + +0.0 + ... Chromatic + (f.= brightness red) + (hue_of green blue (f.mod +6.0)) + + (f.= brightness green) + (hue_of blue red (f.+ +2.0)) + + ... (f.= brightness blue) + (hue_of red green (f.+ +4.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)) @@ -156,4 +158,13 @@ (rgb.rgb (..up red) (..up green) (..up blue)))) + + (def .public (format it) + (%.Format HSB) + (let [it (nominal.representation it)] + (%.format "hsb(" + (%.nat (f.nat (f.degree (the #hue it)))) + " " (%.nat (f.nat (f.percentage (the #saturation it)))) "%" + " " (%.nat (f.nat (f.percentage (the #brightness it)))) "%" + ")"))) ) |