diff --git a/src/main/clojure/mikera/image/colours.clj b/src/main/clojure/mikera/image/colours.clj index 13bb1a6..3ce92f0 100644 --- a/src/main/clojure/mikera/image/colours.clj +++ b/src/main/clojure/mikera/image/colours.clj @@ -25,14 +25,19 @@ (long-colour (Colours/getARGBClamped (double a) (double r) (double g) (double b))))) (defn components-argb - "Return the ARGB components of a colour value, in a 4-element vector of double values" + "Return the ARGB components of a colour value, in a 4-element vector of long values" ([^long argb] - (TODO))) + [(bit-shift-right (bit-and argb 0xFF000000) 24) + (bit-shift-right (bit-and argb 0x00FF0000) 16) + (bit-shift-right (bit-and argb 0x0000FF00) 8) + (bit-and argb 0x000000FF)])) (defn components-rgb - "Return the RGB components of a colour value, in a 3-element vector of double values" + "Return the RGB components of a colour value, in a 3-element vector of long values" ([^long argb] - (TODO))) + [(bit-shift-right (bit-and argb 0x00FF0000) 16) + (bit-shift-right (bit-and argb 0x0000FF00) 8) + (bit-and argb 0x000000FF)])) (defn rand-colour "Returns a random RGB colour value with 100% alpha" @@ -49,4 +54,4 @@ orange pink red white yellow)) (doseq [colour JAVA-COLOURS] - (eval `(def ~colour (long-colour (.getRGB (. Color ~colour)))))) \ No newline at end of file + (eval `(def ~colour (long-colour (.getRGB (. Color ~colour)))))) diff --git a/src/test/clojure/mikera/image/test_colours.clj b/src/test/clojure/mikera/image/test_colours.clj index aa92ef7..a3e2610 100644 --- a/src/test/clojure/mikera/image/test_colours.clj +++ b/src/test/clojure/mikera/image/test_colours.clj @@ -5,6 +5,12 @@ (deftest test-rgb (is (== 0xFF000000 (rgb 0 0 0)))) +(deftest test-components-argb + (is (= [0xDE 0xAD 0xBE 0xEF] (components-argb 0xDEADBEEF)))) + +(deftest test-components-rgb + (is (= [0xFE 0xBA 0xBE] (components-rgb 0xCAFEBABE)))) + (deftest test-rand-colour (let [rc (rand-colour)] (is (== 0xFF000000 (bit-and 0xFF000000 rc)))))