Colors provides color manipulation routines to Clojure.
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
Colors is now up on clojars, so you can add it as a dependency to your lein project like normal:
:dependencies [[com.evocomputing/colors "1.0.0-SNAPSHOT"] ...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
(use 'com.evocomputing.colors) ;;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.
(use 'com.evocomputing.colors) (def red-color (create-color :red)) ;;Convert to java.awt.Color (awt-color red-color) ;;get the rgba integer representation (rgba-int red-color) ;;get the hexstring representation (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
(use 'com.evocomputing.colors) (def blue-color (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 (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% (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% (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
(use 'com.evocomputing.colors) (use 'com.evocomputing.colors.palettes.core) (use 'com.evocomputing.colors.palettes.color-brewer) ;;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) (get-color-brewer-palette "YlOrRd" 8)
API Documentation for colors is located at: Colors API