GithubHelp home page GithubHelp logo

jolby / colors Goto Github PK

View Code? Open in Web Editor NEW
46.0 5.0 8.0 208 KB

Color and colorspace calculation, manipulation and conversion in Clojure.

Home Page: http://github.com/jolby/colors

Clojure 92.51% HTML 7.40% Emacs Lisp 0.08%

colors's Introduction

About

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:

Install

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 ]]

Example Usage

Create Color Objects

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})

Convert colors

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)

Manipulate colors

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>"

Color Palettes

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)

Documentation

API Documentation for colors is located at: Colors API

Thanks

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.

colors's People

Contributors

ageneau avatar austinjones avatar brunchboy avatar folcon avatar jimmyhmiller avatar jolby avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

colors's Issues

rainbow-hsl doesnt appear to work.

Hi, just installed and tried rainbow-hsl. fails on an assert in inclusive-seq that the arg should be less than 1.

I was struggling to follow the inclusive-seq fn, so, in the meantime wrote my own:

(defn rainbow-hsl [numcolors & opts]
  (let [opts (merge {:s 50.0 :l 70.0 :start 0 :step (/ 360 numcolors)}
                    (when opts (apply assoc {} opts)))]
    (->>
      (range 1 (inc numcolors))
      (map #(mod (+ (:start opts) (* % (:step opts))) 360))
      (map #(rgb/create-color :h (float %) :s (:s opts) :l (:l opts)))
      (map rgb/rgb-hexstr))))```

Hex strings don't work in clojurescript

Hi

In clojurescript, rgb-hexstr and the other functions that could return a hexstr don't work:

(color/rgb-hexstr (color/create-color "blue"))
;;=> "#%06X"

This is due to this line:

(format "#%08X" (rgba-int color)))

Google closure's format is limited and doesn't format hex values
(see: https://stackoverflow.com/questions/24635974/clojurescript-format-string-with-goog-string-format-doesnt-substitute)

It would need to be done manually with something like this:

(defn hex-color [[r g b]]
  (str "#"
    (.padStart (.toString r 16) 2 "0")
    (.padStart (.toString g 16) 2 "0")
    (.padStart (.toString b 16) 2 "0")))

Let me know if you would like a PR.
Thanks!

Edited to add .padStart to hex-color.

Alpha value when using HSLA

Hi there, I think the alpha value when using HSLA should not be on a scale from 0..255 (a la RGB) but either 0..1 or 0..100. Using a scale of 0..255 seems quite arbitrary when using the HSL notation. I personally would prefer 0..1 as it more closely resembles the CSS notation.

Should mix and the color-bin-ops be documented?

I ended up rolling my own poor mix function last night because I did not see anything like that in the API reference, but just noticed after submitting my pull request for the RGB creation fix that there had been another pull request fixing a mix function, so I dug into the source, and found it and color-add, color-mult, et. al. Those look really useful! Could they be added to the API reference?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.