A small Clojure/ClojureScript library for creating colour swatches
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
doc
example
resources
src/inkspot
test/inkspot
.gitignore
.travis.yml
LICENSE.md
README.md
project.clj

README.md

Inkspot

Build Status Dependencies Status Downloads Clojars Project Maintenance

A small Clojure/ClojureScript library for creating colour swatches and converting between colorspaces.

Pre-requisites

You will need Leiningen 2.4.2 or above installed.

Building

To build and install the library locally, run:

$ lein test
$ lein install

Including in your project

There is version hosted at Clojars. For leiningen include a dependency:

[rm-hull/inkspot "0.2.1"]

For maven-based projects, add the following to your pom.xml:

<dependency>
  <groupId>rm-hull</groupId>
  <artifactId>inkspot</artifactId>
  <version>0.2.1</version>
</dependency>

Basic Usage

See the API Documentation.

A cross-platform IColor protocol has been overlaid on several common color representations. For example, in Clojure:

(color/coerce 0xFF0000)
=> #<Color java.awt.Color[r=255,g=0,b=0]>

(color/coerce "#00FF00")
=> #<Color java.awt.Color[r=0,g=255,b=0]>

(color/coerce "rgb(0,0,255)")
=> #<Color java.awt.Color[r=0,g=0,b=255]>

(color/coerce java.awt.Color/MAGENTA)
=> #<Color java.awt.Color[r=255,g=0,b=255]>

(color/coerce [255 255 0])
=> #<Color java.awt.Color[r=255,g=255,b=0]>

Specifying the color by keyword is also possible, with unknown colors returning nil as might be expected. The full list of supported color names can be found in the lindsay and X11 files. Notes: (1) The lindsay list is checked first, and if the color name is not present, it is then checked for in the X11 list. (2) the coerce lookup is case insensitive.

(color/coerce :bisque)
=> #<Color java.awt.Color[r=255,g=228,b=196]>

(color/coerce :BISQUE)
=> #<Color java.awt.Color[r=255,g=228,b=196]>

(color/coerce :none-existent-color)
=> nil

For ClojureScript, the same calls would instead yield a string representation, thus:

(color/coerce :bisque)
=> "rgba(255,228,196,1.0)"

Built-in color swatches

There are a number of built-in swatches which can be used,

Distinct Color swatches

Function Color Palette
color-chart/web-safe-colors Web-safe
color-chart.lindsay/swatch Lindsay
color-chart.x11/swatch X11

Interpolated Color swatches

Function Color Palette
color-chart/spectrum Spectrum
color-chart/rainbow Rainbow
color-chart/hue Hue
color-chart/gradient :orange :blue 216 gradient1
color-chart/gradient :red :snow 216 gradient2
color-chart/heatmap 216 heatmap
color-chart/cube-helix 216 cube-helix

These palettes were generated with the following example:

(ns inkspot.examples
  (require [clojure.java.io :as io]
           [inkspot.color :as color]
           [inkspot.color-chart :as cc]
           [inkspot.palette :as palette]
           [inkspot.color-chart.lindsay :as lindsay]
           [inkspot.color-chart.x11 :as x11])
  (import [javax.imageio ImageIO]))

;; Distinct Color Swatches
(doseq [[k v] {:web-safe-colors (map color/coerce cc/web-safe-colors)
               :lindsay         (map color/coerce (vals lindsay/swatch))
               :x11             (map color/coerce (vals x11/swatch))}
        :let [f (io/file (str "example/palette/" (name k) ".png"))]]
  (ImageIO/write (palette/draw v :g2d-target palette/bitmap) "png" f))

;; Interpolated Color Swatches
(doseq [[k v] {:spectrum   (cc/spectrum 216)
               :rainbow    (cc/rainbow 216)
               :gradient1  (cc/gradient :orange :blue 216)
               :gradient2  (cc/gradient :red :snow 216)
               :heatmap    (cc/heatmap 216)
               :cube-helix (cc/cube-helix 216)}
        :let [f (io/file (str "example/palette/" (name k) ".png"))]]
  (ImageIO/write (palette/draw v :g2d-target palette/bitmap
                                 :cell-width 2 :cell-height 50
                                 :cells-per-row 216 :border 0) "png" f))

uiGradients

The gradients.json from uiGradients is loaded in (via a macro in clojurescript), and interpolated color swatches can be generated by specifying the color name. For example:

; Names can be specified as the named strings or kebab-case
(cc/ui-gradient :sea-blizz 240)
=> (#<Color java.awt.Color[r=28,g=216,b=210]> #<Color java.awt.Color[r=28,g=216,b=209]> ...

(cc/ui-gradient "Sea Blizz" 240)
=> (#<Color java.awt.Color[r=28,g=216,b=210]> #<Color java.awt.Color[r=28,g=216,b=209]> ...
Name Color Palette
:a-lost-memory a-lost-memory
:almost almost
:amethyst amethyst
:aqua-marine aqua-marine
:aqualicious aqualicious
:army army
:ash ash
:aubergine aubergine
:autumn autumn
:behongo behongo
:bloody-mary bloody-mary
:blurry-beach blurry-beach
:bora-bora bora-bora
:bourbon bourbon
:calm-darya calm-darya
:candy candy
:cheer-up-emo-kid cheer-up-emo-kid
:cherry cherry
:cherryblossoms cherryblossoms
:clouds clouds
:dance-to-forget dance-to-forget
:day-tripper day-tripper
:dirty-fog dirty-fog
:dracula dracula
:earthly earthly
:electric-violet electric-violet
:emerald-water emerald-water
:facebook-messenger facebook-messenger
:forever-lost forever-lost
:frozen frozen
:horizon horizon
:influenza influenza
:jonquil jonquil
:juicy-orange juicy-orange
:kashmir kashmir
:kyoto kyoto
:lemon-twist lemon-twist
:man-of-steel man-of-steel
:mango-pulp mango-pulp
:mantle mantle
:miaka miaka
:midnight-city midnight-city
:mirage mirage
:misty-meadow misty-meadow
:mojito mojito
:moonrise moonrise
:moor moor
:moss moss
:mystic mystic
:namn namn
:neon-life neon-life
:opa opa
:parklife parklife
:peach peach
:petrichor petrichor
:pinky pinky
:pinot-noir pinot-noir
:purple-paradise purple-paradise
:red-mist red-mist
:reef reef
:rose-water rose-water
:sea-blizz sea-blizz
:sea-weed sea-weed
:shadow-night shadow-night
:shore shore
:shrimpy shrimpy
:shroom-haze shroom-haze
:sirius-tamed sirius-tamed
:soundcloud soundcloud
:starfall starfall
:steel-gray steel-gray
:stellar stellar
:sunrise sunrise
:teal-love teal-love
:the-strain the-strain
:titanium titanium
:vasily vasily
:venice-blue venice-blue
:virgin virgin
:winter winter

The JSON file is packaged into the inkspot jar, but the uiGradients project is referenced as a git submodule and the intention is to keep it mostly up-to-date as new inkspot releases are published.

Mixing Colors

TODO

Cycling Colors

TODO

Linear mapping across numeric ranges

A color swatch can be mapped to a specific range of numbers (integer or floating point), where for each given input in the range, the nearest color is selected. For example,

(use 'inkspot.color-chart)

(def colors
  (color-mapper
    (spectrum 48)
    100 250))

(colors 100) ; lower bound is inclusive
=> #<Color java.awt.Color[r=224,g=0,b=0]>

(colors 249.99)
=> #<Color java.awt.Color[r=110,g=0,b=180]>

(colors 250) ; upper bound is exclusive
=> nil

(colors 43) ; outside range
=> nil

Converting IColor instances to other colorspaces and back

The inkspot.converter namespace supports the following conversions to/from IColor instances. In all cases the non-RGB colorspace results are always returned as vector of 3 elements.

  • HSV (Hue, Saturation, Value)
  • HSL (Hue, Saturation, Luminosity)
  • Y'UV (Luma, Chrominance as used in PAL video standards)
(use 'inkspot.converter)

(rgb->hsv :yellow)
=> [60.0 1.0 1.0]

(hsv->rgb [60.0 1.0 1.0])
=> #<Color java.awt.Color[r=255,g=255,b=0]>

TODO

  • Web-safe colors
  • Spectral colors
  • IColor protocol
  • Color mapper function - given a numerical range and a color swatch, maps numerical input to the range of colors
  • Color averaging/mixing
  • Create PNG & SVG swatch palette representations (& add custome height, width, border options)
  • Logarithmic color mapper function
  • Import LUT maps
  • Gradient interpolation: Use HSV values rather than RGB interpolation? not necessary
  • RGB, HSV, HSL, YUV, YIQ colorspace conversions
  • X11 Color names
  • Monochrome/triadic/tetradic schemes
  • CubeHelix schemes
  • Embed uiGradients

Known Bugs

  • CLJS files not generating properly

References

Attribution

This repo incorporates gradients.json from uiGradients.

License

The MIT License (MIT)

Copyright (c) 2016 Richard Hull

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.