diff options
author | Eduardo Julian | 2022-12-12 03:47:35 -0400 |
---|---|---|
committer | Eduardo Julian | 2022-12-12 03:47:35 -0400 |
commit | fe9a58dfcd5732ef0c5e5c4b7e85370cdc0db45a (patch) | |
tree | 5ad844ea2bdf33a67cceaad437efaf82cf773a02 /stdlib/source/library/lux/data | |
parent | eef4422b1f16be2b8c651461f2c006dc4c11f314 (diff) |
Added trade session (OHLCV) abstraction.
Diffstat (limited to 'stdlib/source/library/lux/data')
-rw-r--r-- | stdlib/source/library/lux/data/color.lux | 79 | ||||
-rw-r--r-- | stdlib/source/library/lux/data/color/cmyk.lux | 6 | ||||
-rw-r--r-- | stdlib/source/library/lux/data/color/hsb.lux | 6 | ||||
-rw-r--r-- | stdlib/source/library/lux/data/color/hsl.lux | 278 | ||||
-rw-r--r-- | stdlib/source/library/lux/data/color/rgb.lux | 182 | ||||
-rw-r--r-- | stdlib/source/library/lux/data/color/terminal.lux | 6 |
6 files changed, 256 insertions, 301 deletions
diff --git a/stdlib/source/library/lux/data/color.lux b/stdlib/source/library/lux/data/color.lux index 2bfa5e5af..b65249c33 100644 --- a/stdlib/source/library/lux/data/color.lux +++ b/stdlib/source/library/lux/data/color.lux @@ -47,90 +47,19 @@ ... else it)) -(with_template [<op> <name>] - [(def .public (<name> ratio it) - (-> Frac Color Color) - (let [it (hsl.of_rgb (rgb it))] - (|> (hsl.hsl (hsl.hue it) - (|> it - hsl.saturation - (f.* (|> +1.0 (<op> (..ratio ratio)))) - (f.min +1.0)) - (hsl.luminance it)) - hsl.rgb - of_rgb)))] - - [f.+ saturated] - [f.- un_saturated] - ) - -(def .public (gray_scale color) - (-> Color Color) - (let [color (hsl.of_rgb (rgb color))] - (|> (hsl.hsl +0.0 - +0.0 - (hsl.luminance color)) - hsl.rgb - of_rgb))) - -(with_template [<name> <1> <2>] - [(`` (def .public (<name> color) - (-> Color [Color Color Color]) - (let [hsl (hsl.of_rgb (rgb color)) - hue (hsl.hue hsl) - saturation (hsl.saturation hsl) - luminance (hsl.luminance hsl)] - [color - (|> (hsl.hsl (|> hue (f.+ <1>) ..ratio) - saturation - luminance) - hsl.rgb - of_rgb) - (|> (hsl.hsl (|> hue (f.+ <2>) ..ratio) - saturation - luminance) - hsl.rgb - of_rgb)])))] - - [triad (|> +1.0 (f./ +3.0)) (|> +2.0 (f./ +3.0))] - [clash (|> +1.0 (f./ +4.0)) (|> +3.0 (f./ +4.0))] - [split_complement (|> +1.0 (f./ +5.0)) (|> +3.0 (f./ +5.0))] - ) - -(with_template [<name> <1> <2> <3>] - [(`` (def .public (<name> color) - (-> Color [Color Color Color Color]) - (let [it (hsl.of_rgb (..rgb color)) - hue (hsl.hue it) - saturation (hsl.saturation it) - luminance (hsl.luminance it) - of_hue (is (-> hsl.Value - Color) - (function (_ hue) - (|> (hsl.hsl hue saturation luminance) - hsl.rgb - ..of_rgb)))] - [color - (|> hue (f.+ <1>) ..ratio of_hue) - (|> hue (f.+ <2>) ..ratio of_hue) - (|> hue (f.+ <3>) ..ratio of_hue)])))] - - [square (|> +1.0 (f./ +4.0)) (|> +2.0 (f./ +4.0)) (|> +3.0 (f./ +4.0))] - [tetradic (|> +2.0 (f./ +12.0)) (|> +6.0 (f./ +12.0)) (|> +8.0 (f./ +12.0))] - ) - (type .public Spread Frac) +... https://en.wikipedia.org/wiki/Color_scheme (type .public Palette (-> Spread Nat Color (List Color))) (def .public (analogous spread variations it) Palette (let [it (hsl.of_rgb (..rgb it)) - hue (hsl.hue it) - saturation (hsl.saturation it) - luminance (hsl.luminance it) + hue (the hsl.#hue it) + saturation (the hsl.#saturation it) + luminance (the hsl.#luminance it) spread (..ratio spread)] (list#each (function (_ idx) (|> (hsl.hsl (|> idx ++ .int int.frac (f.* spread) (f.+ hue) ..ratio) diff --git a/stdlib/source/library/lux/data/color/cmyk.lux b/stdlib/source/library/lux/data/color/cmyk.lux index ae47d9604..bd0a9d298 100644 --- a/stdlib/source/library/lux/data/color/cmyk.lux +++ b/stdlib/source/library/lux/data/color/cmyk.lux @@ -78,9 +78,9 @@ (def .public (cmyk it) (-> RGB CMYK) - (let [red (..down (rgb.red it)) - green (..down (rgb.green it)) - blue (..down (rgb.blue it)) + (let [red (..down (the rgb.#red it)) + green (..down (the rgb.#green it)) + blue (..down (the rgb.#blue it)) key (opposite (all f.max red green blue)) f (if (f.< ..most key) (f./ (opposite key) diff --git a/stdlib/source/library/lux/data/color/hsb.lux b/stdlib/source/library/lux/data/color/hsb.lux index 5421c2dc3..ede1ffd08 100644 --- a/stdlib/source/library/lux/data/color/hsb.lux +++ b/stdlib/source/library/lux/data/color/hsb.lux @@ -97,9 +97,9 @@ (def .public (of_rgb it) (-> RGB HSB) - (let [red (..down (rgb.red it)) - green (..down (rgb.green it)) - blue (..down (rgb.blue it)) + (let [red (..down (the rgb.#red it)) + green (..down (the rgb.#green it)) + blue (..down (the rgb.#blue it)) max (all f.max red green blue) min (all f.min red green blue) diff --git a/stdlib/source/library/lux/data/color/hsl.lux b/stdlib/source/library/lux/data/color/hsl.lux index ce57f5210..835864b26 100644 --- a/stdlib/source/library/lux/data/color/hsl.lux +++ b/stdlib/source/library/lux/data/color/hsl.lux @@ -10,10 +10,7 @@ [math [number ["i" int] - ["f" frac]]] - [meta - [type - ["[0]" nominal]]]]] + ["f" frac]]]]] [// ["[0]" rgb (.only RGB)]]) @@ -56,117 +53,170 @@ (|>> (f.max ..least) (f.min ..most))) -(nominal.def .public HSL +(type .public HSL (Record [#hue Value #saturation Value - #luminance Value]) - - (def .public equivalence - (Equivalence HSL) - (implementation - (def (= left right) - (`` (and (,, (with_template [<slot>] - [(f.= (the <slot> (nominal.representation left)) - (the <slot> (nominal.representation right)))] - - [#hue] - [#saturation] - [#luminance] - ))))))) - - (with_template [<name> <slot>] - [(def .public <name> - (-> HSL - Value) - (|>> nominal.representation - (the <slot>)))] - - [hue #hue] - [saturation #saturation] - [luminance #luminance] - ) - - (def .public (hsl hue saturation luminance) - (-> Frac Frac Frac - HSL) - (nominal.abstraction - [#hue (..value hue) - #saturation (..value saturation) - #luminance (..value luminance)])) - - (def .public (of_rgb it) - (-> RGB - HSL) - (let [red (..down (rgb.red it)) - green (..down (rgb.green it)) - blue (..down (rgb.blue it)) - - max (all f.max red green blue) - min (all f.min red green blue) - luminance (|> (f.+ max min) (f./ +2.0))] - (nominal.abstraction - (if (f.= max min) - ... Achromatic - [#hue ..least - #saturation ..least - #luminance luminance] - ... Chromatic - (let [diff (|> max (f.- min)) - saturation (|> diff - (f./ (if (f.> +0.5 luminance) - (|> +2.0 (f.- max) (f.- min)) - (|> max (f.+ min))))) - hue' (cond (f.= red max) - (|> green (f.- blue) (f./ diff) - (f.+ (if (f.< blue green) +6.0 +0.0))) - - (f.= green max) - (|> blue (f.- red) (f./ diff) - (f.+ +2.0)) - - ... (f.= blue max) - (|> red (f.- green) (f./ diff) - (f.+ +4.0)))] - [#hue (|> hue' (f./ +6.0)) - #saturation saturation - #luminance luminance]))))) - - (def (hue_rgb p q t) - (-> Frac Frac Frac - Nat) - (let [t (cond (f.< +0.0 t) (f.+ +1.0 t) - (f.> +1.0 t) (f.- +1.0 t) - ... else - t) - f2/3 (f./ +3.0 +2.0)] - (..up (cond (f.< (f./ +6.0 +1.0) t) - (|> q (f.- p) (f.* +6.0) (f.* t) (f.+ p)) - - (f.< (f./ +2.0 +1.0) t) - q - - (f.< f2/3 t) - (|> q (f.- p) (f.* (|> f2/3 (f.- t))) (f.* +6.0) (f.+ p)) - - ... else - p)))) - - (def .public (rgb it) - (-> HSL - RGB) - (let [[hue saturation luminance] (nominal.representation it)] - (if (f.= ..least saturation) - ... Achromatic - (let [intensity (..up luminance)] - (rgb.rgb intensity intensity intensity)) - ... Chromatic - (let [q (if (f.< +0.5 luminance) - (|> saturation (f.+ +1.0) (f.* luminance)) - (|> luminance (f.+ saturation) (f.- (f.* saturation luminance)))) - p (|> luminance (f.* +2.0) (f.- q)) - third (|> +1.0 (f./ +3.0))] - (rgb.rgb (|> hue (f.+ third) (hue_rgb p q)) - (|> hue (hue_rgb p q)) - (|> hue (f.- third) (hue_rgb p q))))))) + #luminance Value])) + +(def .public equivalence + (Equivalence HSL) + (implementation + (def (= left right) + (`` (and (,, (with_template [<slot>] + [(f.= (the <slot> left) + (the <slot> right))] + + [#hue] + [#saturation] + [#luminance] + ))))))) + +(def .public (hsl hue saturation luminance) + (-> Frac Frac Frac + HSL) + [#hue (..value hue) + #saturation (..value saturation) + #luminance (..value luminance)]) + +(def .public (of_rgb it) + (-> RGB + HSL) + (let [red (..down (the rgb.#red it)) + green (..down (the rgb.#green it)) + blue (..down (the rgb.#blue it)) + + max (all f.max red green blue) + min (all f.min red green blue) + luminance (|> (f.+ max min) (f./ +2.0))] + (if (f.= max min) + ... Achromatic + [#hue ..least + #saturation ..least + #luminance luminance] + ... Chromatic + (let [diff (|> max (f.- min)) + saturation (|> diff + (f./ (if (f.> +0.5 luminance) + (|> +2.0 (f.- max) (f.- min)) + (|> max (f.+ min))))) + hue' (cond (f.= red max) + (|> green (f.- blue) (f./ diff) + (f.+ (if (f.< blue green) +6.0 +0.0))) + + (f.= green max) + (|> blue (f.- red) (f./ diff) + (f.+ +2.0)) + + ... (f.= blue max) + (|> red (f.- green) (f./ diff) + (f.+ +4.0)))] + [#hue (|> hue' (f./ +6.0)) + #saturation saturation + #luminance luminance])))) + +(def (hue_rgb p q t) + (-> Frac Frac Frac + Nat) + (let [t (cond (f.< +0.0 t) (f.+ +1.0 t) + (f.> +1.0 t) (f.- +1.0 t) + ... else + t) + f2/3 (f./ +3.0 +2.0)] + (..up (cond (f.< (f./ +6.0 +1.0) t) + (|> q (f.- p) (f.* +6.0) (f.* t) (f.+ p)) + + (f.< (f./ +2.0 +1.0) t) + q + + (f.< f2/3 t) + (|> q (f.- p) (f.* (|> f2/3 (f.- t))) (f.* +6.0) (f.+ p)) + + ... else + p)))) + +(def .public (rgb (open "/[0]")) + (-> HSL + RGB) + (if (f.= ..least /#saturation) + ... Achromatic + (let [intensity (..up /#luminance)] + (rgb.rgb intensity intensity intensity)) + ... Chromatic + (let [q (if (f.< +0.5 /#luminance) + (|> /#saturation (f.+ +1.0) (f.* /#luminance)) + (|> /#luminance (f.+ /#saturation) (f.- (f.* /#saturation /#luminance)))) + p (|> /#luminance (f.* +2.0) (f.- q)) + third (|> +1.0 (f./ +3.0))] + (rgb.rgb (|> /#hue (f.+ third) (hue_rgb p q)) + (|> /#hue (hue_rgb p q)) + (|> /#hue (f.- third) (hue_rgb p q)))))) + +(def (ratio it) + (-> Frac + Frac) + (cond (f.> +1.0 it) + (f.% +1.0 it) + + (f.< +0.0 it) + (|> it (f.% +1.0) (f.+ +1.0)) + + ... else + it)) + +(with_template [<op> <name>] + [(def .public (<name> ratio (open "/[0]")) + (-> Frac HSL + HSL) + (..hsl /#hue + (|> /#saturation + (f.* (|> +1.0 (<op> (..ratio ratio)))) + (f.min +1.0)) + /#luminance))] + + [f.+ saturated] + [f.- un_saturated] + ) + +(def .public gray_scale + (-> HSL + HSL) + (|>> (the #luminance) + (..hsl +0.0 + +0.0))) + +(with_template [<name> <1> <2>] + [(`` (def .public (<name> it) + (-> HSL + [HSL HSL HSL]) + (let [(open "/[0]") it] + [it + (..hsl (|> /#hue (f.+ <1>) ..ratio) + /#saturation + /#luminance) + (..hsl (|> /#hue (f.+ <2>) ..ratio) + /#saturation + /#luminance)])))] + + [triad (|> +1.0 (f./ +3.0)) (|> +2.0 (f./ +3.0))] + [clash (|> +1.0 (f./ +4.0)) (|> +3.0 (f./ +4.0))] + [split_complement (|> +1.0 (f./ +5.0)) (|> +3.0 (f./ +5.0))] + ) + +(with_template [<name> <1> <2> <3>] + [(`` (def .public (<name> it) + (-> HSL + [HSL HSL HSL HSL]) + (let [(open "/[0]") it + of_hue (is (-> Value + HSL) + (function (_ hue) + (..hsl hue /#saturation /#luminance)))] + [it + (|> /#hue (f.+ <1>) ..ratio of_hue) + (|> /#hue (f.+ <2>) ..ratio of_hue) + (|> /#hue (f.+ <3>) ..ratio of_hue)])))] + + [square (|> +1.0 (f./ +4.0)) (|> +2.0 (f./ +4.0)) (|> +3.0 (f./ +4.0))] + [tetradic (|> +2.0 (f./ +12.0)) (|> +6.0 (f./ +12.0)) (|> +8.0 (f./ +12.0))] ) diff --git a/stdlib/source/library/lux/data/color/rgb.lux b/stdlib/source/library/lux/data/color/rgb.lux index deb97365f..4dff2eb5d 100644 --- a/stdlib/source/library/lux/data/color/rgb.lux +++ b/stdlib/source/library/lux/data/color/rgb.lux @@ -5,7 +5,7 @@ [monad (.only do)] [monoid (.only Monoid)] [equivalence (.only Equivalence)] - ["[0]" hash (.only Hash)]] + [hash (.only Hash)]] [control [function [predicate (.only Predicate)]]] @@ -16,10 +16,7 @@ ["n" nat] ["i" int] ["f" frac] - ["[0]" i64]]] - [meta - [type - ["[0]" nominal]]]]]) + ["[0]" i64]]]]]) (def .public limit Nat @@ -48,104 +45,83 @@ (|>> (n.max ..least) (n.min ..most))) -(nominal.def .public RGB +(type .public RGB (Record [#red Value #green Value - #blue Value]) - - (def .public (rgb red green blue) - (-> Nat Nat Nat - RGB) - (nominal.abstraction - [#red (value red) - #green (value green) - #blue (value blue)])) - - (with_template [<name> <slot>] - [(def .public <name> - (-> RGB - Value) - (|>> nominal.representation - (the <slot>)))] - - [red #red] - [green #green] - [blue #blue] - ) - - (def .public hash - (Hash RGB) - (of hash.functor each - (|>> nominal.representation) - (all product.hash - n.hash - n.hash - n.hash - ))) - - (def .public equivalence - (Equivalence RGB) - (of ..hash equivalence)) - - (def (opposite_intensity value) - (-> Nat - Nat) - (|> ..most - (n.- value))) - - (def .public (complement it) - (-> RGB - RGB) - (nominal.abstraction - (`` [(,, (with_template [<slot>] - [<slot> (|> it - nominal.representation - (the <slot>) - opposite_intensity)] - - [#red] - [#green] - [#blue] - ))]))) - - (def .public black - RGB - (nominal.abstraction - [#red ..least - #green ..least - #blue ..least])) - - (def .public white - RGB - (nominal.abstraction - [#red ..most - #green ..most - #blue ..most])) - - (with_template [<monoid> <identity> <composite> <left> <right>] - [(def .public <monoid> - (Monoid RGB) - (implementation - (def identity - <identity>) - - (def (composite left right) - (let [left (<left> left) - right (<right> right)] - (nominal.abstraction - (`` [(,, (with_template [<slot>] - [<slot> (<composite> (the <slot> (nominal.representation left)) - (the <slot> (nominal.representation right)))] - - [#red] - [#green] - [#blue] - ))]))))))] - - [addition ..black n.max |> |>] - [subtraction ..white n.min ..complement |>] - ) + #blue Value])) + +(def .public (rgb red green blue) + (-> Nat Nat Nat + RGB) + [#red (value red) + #green (value green) + #blue (value blue)]) + +(def .public hash + (Hash RGB) + (all product.hash + n.hash + n.hash + n.hash + )) + +(def .public equivalence + (Equivalence RGB) + (of ..hash equivalence)) + +(def (opposite_intensity value) + (-> Nat + Nat) + (|> ..most + (n.- value))) + +(def .public (complement it) + (-> RGB + RGB) + (`` [(,, (with_template [<slot>] + [<slot> (|> it + (the <slot>) + opposite_intensity)] + + [#red] + [#green] + [#blue] + ))])) + +(def .public black + RGB + [#red ..least + #green ..least + #blue ..least]) + +(def .public white + RGB + [#red ..most + #green ..most + #blue ..most]) + +(with_template [<monoid> <identity> <composite> <left> <right>] + [(def .public <monoid> + (Monoid RGB) + (implementation + (def identity + <identity>) + + (def (composite left right) + (let [left (<left> left) + right (<right> right)] + (`` [(,, (with_template [<slot>] + [<slot> (<composite> (the <slot> left) + (the <slot> right))] + + [#red] + [#green] + [#blue] + ))])))))] + + [addition ..black n.max |> |>] + [subtraction ..white n.min ..complement |>] ) (def (ratio it) @@ -172,9 +148,9 @@ (f.+ (|> end .int i.frac (f.* dE))) f.int .nat)))] - (..rgb (interpolated' (..red end) (..red start)) - (interpolated' (..green end) (..green start)) - (interpolated' (..blue end) (..blue start))))) + (..rgb (interpolated' (the #red end) (the #red start)) + (interpolated' (the #green end) (the #green start)) + (interpolated' (the #blue end) (the #blue start))))) (with_template [<name> <target>] [(def .public <name> diff --git a/stdlib/source/library/lux/data/color/terminal.lux b/stdlib/source/library/lux/data/color/terminal.lux index 64cf6bb64..06c23c6b3 100644 --- a/stdlib/source/library/lux/data/color/terminal.lux +++ b/stdlib/source/library/lux/data/color/terminal.lux @@ -90,9 +90,9 @@ (let [it (//.rgb it)] (|> [(%.format ..command <command> - ";" (%.nat (rgb.red it)) - ";" (%.nat (rgb.green it)) - ";" (%.nat (rgb.blue it)) + ";" (%.nat (the rgb.#red it)) + ";" (%.nat (the rgb.#green it)) + ";" (%.nat (the rgb.#blue it)) "m") <reset>] (nominal.abstraction Command))))] |