From 531a1923873cd428e90c7c5814050623c2a9cedd Mon Sep 17 00:00:00 2001 From: zehcnas34 Date: Mon, 10 Apr 2023 21:47:44 -0700 Subject: [PATCH 1/6] Support alpha channel in `as-hex` --- src/garden/color.cljc | 47 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/src/garden/color.cljc b/src/garden/color.cljc index 4949ead..d6ee93a 100644 --- a/src/garden/color.cljc +++ b/src/garden/color.cljc @@ -92,6 +92,12 @@ (and (map? color) (every? color #{:red :green :blue}))) +(defn rgba? + "Return true if color is an RGBA color." + [color] + (and (map? color) + (every? color #{:red :green :blue :alpha}))) + (defn hsl? "Return true if color is an HSL color." [color] @@ -140,6 +146,14 @@ (string/replace " " "0")))] (apply str "#" (map hex-part [r g b])))) +(defn rgba->hex + "Convert an RGB color map to a hexadecimal color." + [{r :red g :green b :blue a :alpha}] + (letfn [(hex-part [v] + (-> (util/format "%2s" (util/int->string v 16)) + (string/replace " " "0")))] + (apply str "#" (map hex-part [r g b a])))) + (defn trim-one [x] (if (< 1 x) 1 x)) @@ -186,6 +200,24 @@ (hue->rgb m1 m2 (- h (/ 1.0 3)))])] (rgb [r g b])))) +(defn hsla->rgba + "Convert an HSLA color map to an RGBA color map." + [{:keys [hue saturation lightness alpha] :as color}] + (if (rgb? color) + color + (let [h (/ hue 360.0) + s (/ saturation 100.0) + l (/ lightness 100.0) + m2 (if (<= l 0.5) + (* l (inc s)) + (- (+ l s) (* l s))) + m1 (- (* 2 l) m2) + [r g b] (map #(int (+ 0.5 (* % 0xff))) + [(hue->rgb m1 m2 (+ h (/ 1.0 3))) + (hue->rgb m1 m2 h) + (hue->rgb m1 m2 (- h (/ 1.0 3)))])] + (rgba [r g b alpha])))) + (defn- hue->rgb [m1 m2 h] (let [h (cond @@ -203,6 +235,11 @@ [color] (-> color hsl->rgb rgb->hex)) +(defn hsla->hex + "Convert an HSLA color map to a hexadecimal string." + [color] + (-> color hsla->rgba rgb->hex)) + (defn hex->hsl "Convert a hexadecimal color to an HSL color." [color] @@ -223,10 +260,12 @@ "Convert a color to a hexadecimal string." [x] (cond - (hex? x) x - (rgb? x) (rgb->hex x) - (hsl? x) (hsl->hex x) - :else (throw (ex-info (str "Can't convert " x " to a color.") {})))) + (hex? x) x + (rgb? x) (rgb->hex x) + (rgba? x) (rgba->hex x) + (hsl? x) (hsl->hex x) + (hsla? x) (hsla->hex x) + :else (throw (ex-info (str "Can't convert " x " to a color.") {})))) (defn as-rgb "Convert a color to a RGB." From e46932fd4489015a24d6395dc40c3115d5b92a8d Mon Sep 17 00:00:00 2001 From: zehcnas34 Date: Mon, 10 Apr 2023 22:11:11 -0700 Subject: [PATCH 2/6] Add `rgba->hex` test --- src/garden/color.cljc | 3 ++- test/garden/color_test.cljc | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/garden/color.cljc b/src/garden/color.cljc index d6ee93a..de189a5 100644 --- a/src/garden/color.cljc +++ b/src/garden/color.cljc @@ -152,7 +152,8 @@ (letfn [(hex-part [v] (-> (util/format "%2s" (util/int->string v 16)) (string/replace " " "0")))] - (apply str "#" (map hex-part [r g b a])))) + (str (apply str "#" (map hex-part [r g b]) ) + (hex-part (int (* 256 a)))))) (defn trim-one [x] (if (< 1 x) 1 x)) diff --git a/test/garden/color_test.cljc b/test/garden/color_test.cljc index c944b53..8b56417 100644 --- a/test/garden/color_test.cljc +++ b/test/garden/color_test.cljc @@ -13,6 +13,13 @@ (def hex-blue "#0000ff") (def hex-white "#ffffff") +(def hexa-black "#00000080") +(def hexa-red "#ff000080") +(def hexa-green "#00ff0080") +(def hexa-blue "#0000ff80") +(def hexa-white "#ffffff80") + + (def rgb-black (color/rgb 0 0 0)) (def rgb-red (color/rgb 255 0 0)) (def rgb-green (color/rgb 0 255 0)) @@ -20,6 +27,13 @@ (def rgb-white (color/rgb 255 255 255)) (def rgb-orange (color/rgb 255 133 27)) +(def rgba-black (color/rgba 0 0 0 0.5)) +(def rgba-red (color/rgba 255 0 0 0.5)) +(def rgba-green (color/rgba 0 255 0 0.5)) +(def rgba-blue (color/rgba 0 0 255 0.5)) +(def rgba-white (color/rgba 255 255 255 0.5)) +(def rgba-orange (color/rgba 255 133 27 0.5)) + (def hsl-black (color/hsl 0 0 0)) (def hsl-red (color/hsl 0 100 50)) (def hsl-green (color/hsl 120 100 50)) @@ -43,6 +57,13 @@ (color/rgb->hex rgb-green) hex-green (color/rgb->hex rgb-blue) hex-blue)) + (testing "rgba->hex" + (are [x y] (= x y) + (color/rgba->hex rgba-black) hexa-black + (color/rgba->hex rgba-red) hexa-red + (color/rgba->hex rgba-green) hexa-green + (color/rgba->hex rgba-blue) hexa-blue)) + (testing "hsl->rgb" (are [x y] (= x y) (color/hsl->rgb hsl-black) rgb-black From d287cfcb1097825dfd2a8f058bbfb393184d5e8e Mon Sep 17 00:00:00 2001 From: zehcnas34 Date: Mon, 10 Apr 2023 22:14:19 -0700 Subject: [PATCH 3/6] Add `hsla->rgba` test --- test/garden/color_test.cljc | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/garden/color_test.cljc b/test/garden/color_test.cljc index 8b56417..a6c249c 100644 --- a/test/garden/color_test.cljc +++ b/test/garden/color_test.cljc @@ -41,6 +41,13 @@ (def hsl-white (color/hsl 0 0 100)) (def hsl-orange (color/hsl 530/19 100 940/17)) +(def hsla-black (color/hsla 0 0 0 0.5)) +(def hsla-red (color/hsla 0 100 50 0.5)) +(def hsla-green (color/hsla 120 100 50 0.5)) +(def hsla-blue (color/hsla 240 100 50 0.5)) +(def hsla-white (color/hsla 0 0 100 0.5)) +(def hsla-orange (color/hsla 530/19 100 940/17 0.5)) + (deftest color-conversion-test (testing "hex->rgb" (are [x y] (= x y) @@ -72,6 +79,14 @@ (color/hsl->rgb hsl-blue) rgb-blue (color/hsl->rgb hsl-white) rgb-white)) + (testing "hsla->rgba" + (are [x y] (= x y) + (color/hsla->rgba hsla-black) rgba-black + (color/hsla->rgba hsla-red) rgba-red + (color/hsla->rgba hsla-green) rgba-green + (color/hsla->rgba hsla-blue) rgba-blue + (color/hsla->rgba hsla-white) rgba-white)) + (testing "rgb->hsl" (are [x y] (= x y) (color/rgb->hsl rgb-black) hsl-black From 81d17125689603cc65428174ee377c8ff87a5ecc Mon Sep 17 00:00:00 2001 From: zehcnas34 Date: Mon, 10 Apr 2023 22:18:56 -0700 Subject: [PATCH 4/6] add `as-hex` test --- src/garden/color.cljc | 4 ++-- test/garden/color_test.cljc | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/garden/color.cljc b/src/garden/color.cljc index de189a5..a6a45de 100644 --- a/src/garden/color.cljc +++ b/src/garden/color.cljc @@ -261,11 +261,11 @@ "Convert a color to a hexadecimal string." [x] (cond + (rgba? x) (rgba->hex x) + (hsla? x) (hsla->hex x) (hex? x) x (rgb? x) (rgb->hex x) - (rgba? x) (rgba->hex x) (hsl? x) (hsl->hex x) - (hsla? x) (hsla->hex x) :else (throw (ex-info (str "Can't convert " x " to a color.") {})))) (defn as-rgb diff --git a/test/garden/color_test.cljc b/test/garden/color_test.cljc index a6c249c..e701fa6 100644 --- a/test/garden/color_test.cljc +++ b/test/garden/color_test.cljc @@ -94,7 +94,15 @@ (color/rgb->hsl rgb-green) hsl-green (color/rgb->hsl rgb-blue) hsl-blue (color/rgb->hsl rgb-white) hsl-white - (color/rgb->hsl rgb-orange) hsl-orange))) + (color/rgb->hsl rgb-orange) hsl-orange)) + + (testing "as-hex" + (are [x y] (= x y) + (color/as-hex rgba-black) hexa-black + (color/as-hex rgba-red) hexa-red + (color/as-hex rgba-green) hexa-green + (color/as-hex rgba-blue) hexa-blue + (color/as-hex rgba-white) hexa-white))) (deftest color-math-test (testing "color+" From 72e08082023fbb465e0ff732b8e1063da8b84987 Mon Sep 17 00:00:00 2001 From: zehcnas34 Date: Tue, 11 Apr 2023 09:38:59 -0700 Subject: [PATCH 5/6] Cover case when `alpha` is one in `rgba->hex` --- src/garden/color.cljc | 4 +++- test/garden/color_test.cljc | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/garden/color.cljc b/src/garden/color.cljc index a6a45de..a5ebe09 100644 --- a/src/garden/color.cljc +++ b/src/garden/color.cljc @@ -153,7 +153,9 @@ (-> (util/format "%2s" (util/int->string v 16)) (string/replace " " "0")))] (str (apply str "#" (map hex-part [r g b]) ) - (hex-part (int (* 256 a)))))) + (if (= 1 a) + "ff" + (hex-part (int (* 256 a))))))) (defn trim-one [x] (if (< 1 x) 1 x)) diff --git a/test/garden/color_test.cljc b/test/garden/color_test.cljc index e701fa6..b4c86ee 100644 --- a/test/garden/color_test.cljc +++ b/test/garden/color_test.cljc @@ -13,6 +13,7 @@ (def hex-blue "#0000ff") (def hex-white "#ffffff") +(def hexa-opaque-black "#000000ff") (def hexa-black "#00000080") (def hexa-red "#ff000080") (def hexa-green "#00ff0080") @@ -27,6 +28,7 @@ (def rgb-white (color/rgb 255 255 255)) (def rgb-orange (color/rgb 255 133 27)) +(def rgba-opaque-black (color/rgba 0 0 0 1)) (def rgba-black (color/rgba 0 0 0 0.5)) (def rgba-red (color/rgba 255 0 0 0.5)) (def rgba-green (color/rgba 0 255 0 0.5)) @@ -66,6 +68,7 @@ (testing "rgba->hex" (are [x y] (= x y) + (color/rgba->hex rgba-opaque-black) hexa-opaque-black (color/rgba->hex rgba-black) hexa-black (color/rgba->hex rgba-red) hexa-red (color/rgba->hex rgba-green) hexa-green From 5f143ae1c96f273754271950570ec16ae8d926b6 Mon Sep 17 00:00:00 2001 From: zehcnas34 Date: Tue, 11 Apr 2023 09:46:27 -0700 Subject: [PATCH 6/6] Add opaque hsla test --- test/garden/color_test.cljc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/garden/color_test.cljc b/test/garden/color_test.cljc index b4c86ee..4533f8b 100644 --- a/test/garden/color_test.cljc +++ b/test/garden/color_test.cljc @@ -43,6 +43,7 @@ (def hsl-white (color/hsl 0 0 100)) (def hsl-orange (color/hsl 530/19 100 940/17)) +(def hsla-opaque-black (color/hsla 0 0 0 1)) (def hsla-black (color/hsla 0 0 0 0.5)) (def hsla-red (color/hsla 0 100 50 0.5)) (def hsla-green (color/hsla 120 100 50 0.5)) @@ -84,6 +85,7 @@ (testing "hsla->rgba" (are [x y] (= x y) + (color/hsla->rgba hsla-opaque-black) rgba-opaque-black (color/hsla->rgba hsla-black) rgba-black (color/hsla->rgba hsla-red) rgba-red (color/hsla->rgba hsla-green) rgba-green