Colors provides color manipulation routines to Clojure/Clojurescript. Note that while this project is being used by some folks, it isn’t in very active development.
Another active clojure project working in the same space is:
https://github.com/thi-ng/color
Further references:
- HSL and HSV: http://en.wikipedia.org/wiki/Luminance-Hue-Saturation
- RGB color space: http://en.wikipedia.org/wiki/RGB_color_space
- SASS color module: http://github.com/nex3/haml/blob/master/lib/sass/script/color.rb
- R colorspace package: http://cran.r-project.org/web/packages/colorspace/index.html
- ColorBrewer http://colorbrewer.org/
- R ColorBrewer palettes: http://sekhon.berkeley.edu/library/RColorBrewer/html/ColorBrewer.html
deps.clj deps.edn
{:deps
...
com.evocomputing/colors {:mvn/version "1.0.7"}
... other deps
}
Or deps.edn using a particular git commit SHA
{:deps
...
com.evocomputing/colors {:git/url "https://github.com/jolby/colors.git"
:sha "3f31b34e429bdde1d777776abea221a177f1a7a0"}
... other deps
}
lein project.clj
:dependencies [[com.evocomputing/colors "1.0.7"]
...other deps ]]
The first point of entry is to create a color object. This package takes a wide range of representations to be passed to the create-color multimethod
(require '[com.evocomputing.colors :refer [create-color]])
;;Symbolic: Either a string or keyword or symbol that matches an entry
;;in the symbolic color pallete. Currently, this is defaults to the
;;html4 colors map and x11 colors map, but the end user of this library
;;can set any named palette they want.
(create-color "blue")
(create-color :blue)
;;Hexstring: A hex string representation of an RGB(A) color
(create-color "0xFFCCAA")
(create-color "#FFCCAA")
(create-color "Ox80FFFF00") ;; alpha = 128
;; Integer: An integer representation of an RGB(A) color
(create-color 0xFFCCAA) ;; integer in hexidecimal format
(create-color 16764074) ;; same integer in decimal format
;;Sequence or array of RGB(A) integers
(create-color [255 0 0])
(create-color [255 0 0 128]) ;;alpha = 128
;;Map of either RGB (A) kw/values or HSL(A) kw/values
;;Allowable RGB keys: :r :red :g :green :b :blue
;;Allowable HSL keys: :h :hue :s :saturation :l :lightness
(create-color {:r 255 :g 0 :blue 0})
(create-color {:r 255 :g 0 :blue 0 :a 128})
(create-color {:h 120.0 :s 100.0 :l 50.0})
(create-color {:h 120.0 :s 100.0 :l 50.0 :a 128})
You can easily convert color objects into java.awt.Color objects, or a packed 32 bit integer representation, or its component R, G, B, A or H, S, L, A parts.
(require '[com.evocomputing.colors :as c])
(def red-color (c/create-color :red))
;;Convert to java.awt.Color
(c/awt-color red-color)
;;get the rgba integer representation
(c/rgba-int red-color)
;;get the hexstring representation
(c/rgb-hexstr red-color)
;;get a vector of the constituent rgba components
(:rgba red-color)
;;likewise for the HSL constituent components
(:hsl red-color)
You can easily manipulate and adjust colors with simple operations
(require '[com.evocomputing.colors :as c])
(def blue-color (c/create-color :blue))
;; => "#<color: blue R: 0, G: 0, B: 255, H: 240.00, S: 100.00, L:#50.00, A: 255>"
;;Create new color 1/3 way around color wheel
;;to get new primary red color
(c/adjust-hue blue-color 120)
;; => "#<color: red R: 255, G: 0, B: 0, H: 0.00, S: 100.00, L: 50.00, A: 255>"
;;lighten by 20%
(c/lighten blue-color 20)
;; => "#<color: 0xff6666ff R: 102, G: 102, B: 255, H: 240.00, S: 100.00, L: 70.00, A: 255>"
;;darken by 20%
(c/darken blue-color 20)
;; => "#<color: 0xff000099 R: 0, G: 0, B: 153, H: 240.00, S: 100.00,#L: 30.00, A: 255>"
You can use the com.evocomputing.colors.palettes package to easily create semantic color palettes. The functions in com.evocomputing.colors.palettes.core follow the functions in the R colorspaces package. http://cran.r-project.org/web/packages/colorspace/index.html
(require '[com.evocomputing.colors.palettes.core
:refer [rainbow-hsl diverge-hsl heat-hsl]])
(require '[com.evocomputing.colors.palettes.color-brewer :as cb])
;;Create a rainbow qualitative palette of 10 colors
;;each with different hues given a single value of each
;;saturation and lightness
(rainbow-hsl 10)
;;Create a diverging palette of 10 colors, composed of a set of colors
;;diverging from a neutral center (grey or white, without color) to two
;;different extreme colors (blue and red by default).
(diverge-hsl 10)
;;Create heat palette in HSL space. By default, it goes from a red to
;;a yellow hue, while simultaneously going to lighter colors (i.e.,
;;increasing lightness) and reducing the amount of color (i.e.,
;;decreasing saturation).
(heat-hsl 10)
;;Create an 8 item ColorBrewer sequential palette "YlOrRd" (Yellow,
;;Orange, Red)
(cb/get-color-brewer-palette "YlOrRd" 8)
API Documentation for colors is located at: Colors API
I all but abandoned this project after 2010 or so. I’d like to thank everyone who have furthered the work on this project by submitting patches. James Elliott deserves a special shout out for all the work he’s done. Check out his afterglow project to see a really cool project that uses colors to great effect. Also many thanks to Sylvain Ageneau for the CLJS port.