1
0
Fork
You've already forked color
0
CLJ/CLJS library for color conversion & manipulation
  • Shell 100%
Find a file
2025年03月22日 14:12:35 +01:00
assets/presets assets: migrate SVG files 2025年03月22日 14:09:46 +01:00
src docs: update SVG links 2025年03月22日 14:12:35 +01:00
test update get-float-buffer impls to actually return FloatBuffer (in Clojure), add tests 2016年04月03日 12:59:48 +01:00
.gitignore initial import 2014年09月09日 13:51:45 +01:00
LICENSE initial import 2014年09月09日 13:51:45 +01:00
README.org docs: update SVG links 2025年03月22日 14:12:35 +01:00
tangle-all.sh bump version to 1.1.0-SNAPSHOT, switch to thi.ng/math protocol impls, 2016年03月06日 00:56:33 +00:00
tangle.sh initial import 2014年09月09日 13:51:45 +01:00

thi.ng/color

https://raw.githubusercontent.com/thi-ng/color/refs/heads/master/assets/presets/rainbow1.svg

About the project

Overview

Cross-platform CLJ/CLJS library for color conversion & manipulation. The library supports any-to-any conversions and color manipulations for the following color spaces:

  • RGBA (floats, packed int24/int32)
  • HSVA
  • HSLA
  • CMYKA
  • HCYA
  • HCVA
  • CIE1931A
  • CSS(A) (hex 3/6-digit & long forms)

The library also provides a protocol wrapper for other, user supplied color spaces to proxy all operations via intermediate RGBA conversions. Users will only need to provide two functions to convert from/to RGBA.

All color types are completely protocol based with optimized implementations for each. The current set of color operations includes:

  • channel accessors for all supported color spaces
  • hue rotation
  • brightness/saturation/alpha adjustments
  • analog/complementary/inverse colors
  • hue matching
  • blending
  • distance calculations in RGB/HSV

Furthermore the library includes namespaces to generate procedural gradients and features 100+ CSS named color & gradient presets as well as D3's & Cynthia Brewer's categorical color schemes originally intented for geo mapping and other data visualization applications.

Leiningen coordinates

Stable

[thi.ng/color "1.5.1"]

Note: This library relies on the new conditional reader syntax of recent Clojure & Clojurescript versions and therefore is **not compatible with Clojure versions < 1.7.0

Status

STABLE & under regular development, 170+ tests

Example usage

 (require '[thi.ng.color.core :as col])
 (col/rgba 1 0 0 0.5)
 ;; #thi.ng.color.core.RGBA [1.0 0.0 0.0 0.5]
 (col/hsva 0.5 1 1)
 ;; #thi.ng.color.core.HSVA [0.5 1.0 1.0 1.0]
 (col/as-cmyka (col/hsva 0.5 1 1))
 ;; #thi.ng.color.core.CMYKA [1.0 0.0 0.0 0.0 1.0]
 ;; use deref to obtain the wrapped color values
 @(col/as-css (col/hsla 1/6 1 0.5 0.25))
 ;; "hsla(60,100%,50%,0.25)"
 ;; parse CSS colors into any target space
 @(col/as-rgba (col/css "hsla(60,100%,50%,0.25)"))
 ;; [0.9999999999999998 1.0 0.0 0.25]
 (= 0xffff00 @(col/as-int24 (col/css "hsla(60,100%,50%,0.25)")))
 ;; compact CSS colors
 @(-> "hsla(60,100%,50%,0.25)" col/css col/as-int24 col/as-css)
 ;; "#ffff00"
 ;; create controlled color variations (e.g. to create hover state colors)
 @(-> (col/css "#f04") (col/analog 0.0 -0.2 0.4) col/as-int24 col/as-css)
 "#ff3369"
 @(-> (col/rgba 1 0 0.25 0.8) (col/random-analog 0.05 0.1 0.2) col/as-css)
 ;; "rgba(255,8,63,0.8)"
 ;; All color types implement the IIinterpolate protocol defined in thi.ng.math.core (1.1.0+ only)
 ;; For previous versions use (col/blend ...) (same API as m/mix)
 (require '[thi.ng.math.core :as m])
 ;; blend with any color space (result is in color space of first color)
 (m/mix (col/hsva 0 1 1) (col/rgba 0 1 0 0.5) 0.5)
 ;; #thi.ng.color.core.HSVA [0.16666666666666666 1.0 1.0 0.75]
 ;; sort colors by proximity to a target color
 (->> (repeatedly 10 col/random-rgb)
 (sort-by #(col/dist-hsv col/RED %)) ;; use HSV distance
 (map (comp deref col/as-css col/as-int24)))
 ;; ("#d8756c" "#ca6709" "#f67e9c" "#bf984e" "#6e400d" "#d602af" "#c52fd2" "#93759d" "#3710d1" "#06a1c3")

Project definition

Dependencies

Runtime

Clojure
[org.clojure/clojure "1.11.1"]
ClojureScript
[org.clojure/clojurescript "1.11.121"]
thi.ng/math
[thi.ng/math "0.3.2"]
thi.ng/dstruct
[thi.ng/dstruct "0.2.2"]
thi.ng/typedarrays
[thi.ng/typedarrays "0.1.7"]
thi.ng/strf
[thi.ng/strf "0.2.4"]
thi.ng/xerror
[thi.ng/xerror "0.1.0"]

Development

Criterium
[criterium "0.4.6"]
clojurescript.test
[com.cemerick/clojurescript.test "0.3.3"]
Cljsbuild
[lein-cljsbuild "1.1.8"]

Leiningen coordinates

 [thi.ng/color <<version>>]

Building this project

This project is written in a literate programming format and requires Emacs & Org-mode to generate usable source code. Assuming both tools are installed, the easiest way to generate a working project is via command line (make sure emacs is on your path or else edit its path in tangle.sh):

 git clone https://github.com/thi.ng/color.git
 cd color
 ./tangle.sh src/*.org test/*.org

Tangling is the process of extracting & combining source blocks from .org files into an actual working project/source tree. Once tangling is complete, you can cd into the generated project directory (babel) and then use lein as usual.

Testing

The project.clj file defines an alias to trigger a complete build & tests for both CLJ & CLJS versions.

 cd babel
 lein cleantest

To build the Clojurescript version simply run lein cljsbuild test from the same directory. A small HTML harness for the resulting JS file is also located in that folder (babel/index.html), allowing for further experimentation in the browser.

Working with the REPL

Editing code blocks or files in Org-mode, then re-loading & testing changes is quite trivial. Simply launch a REPL (via lein or Emacs) as usual. Everytime you've made changes to an .org file, re-tangle it from Emacs (C-c C-v t) or tangle.sh, then reload the namespace in the REPL via (require 'thi.ng.color... :reload) or similar.

Accessing library version during runtime

The autogenerated namespace thi.ng.color.version contains a single symbol version holding the version string defined above:

 (use '[thi.ng.color.version])
 (prn version)
 ; "<<version>>"

Release history

Version Released Description Tagged GH URL
1.5.1 2022年10月23日 update deps 1.5.1
1.5.0 2022年04月23日 update deps 1.5.0
1.2.0 2016年04月05日 update dstruct dep, refactor IBuffer impls, add tests 1.2.0
1.1.2 2016年03月28日 add gradient presets 1.1.2
1.1.1 2016年03月19日 fix dependencies 1.1.1
1.1.0 2016年03月19日 defrecord impl, protocol updates, optimizations, bugfixes 1.1.0
1.0.1 2015年12月25日 bugfix yuva & cie1931 rgba conversions 1.0.1
1.0.0 2015年10月02日 complete rewrite using protocols 1.0.0
0.3.1 2015年06月21日 add YCbCr & YUV conversion, update deps 0.3.1
0.3.0 2015年06月15日 gradient ns, HSL/HCY/HCV color space support, bugfixes 0.3.0
0.2.0 2015年05月26日 no more CLJX, CIE1931 color space support, doc updates 0.2.0
0.1.3 2015年04月22日 bugfixes, conversion updates 0.1.3
0.1.2 2015年03月15日 bugfixes 0.1.2
0.1.1 2015年02月22日 updated presets, added conversions 0.1.1
0.1.0 2015年01月20日 1st public release 0.1.0

Contributors

Name Role Website
Karsten Schmidt initiator & principal developer https://thi.ng

License

This project is open source and licensed under the Apache Software License 2.0.