Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add components-a?rgb with tests #2

Merged
merged 1 commit into from Jul 31, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/main/clojure/mikera/image/colours.clj
Expand Up @@ -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"
Expand All @@ -49,4 +54,4 @@
orange pink red white yellow))

(doseq [colour JAVA-COLOURS]
(eval `(def ~colour (long-colour (.getRGB (. Color ~colour))))))
(eval `(def ~colour (long-colour (.getRGB (. Color ~colour))))))
6 changes: 6 additions & 0 deletions src/test/clojure/mikera/image/test_colours.clj
Expand Up @@ -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)))))
Expand Down