Code Monkey home page Code Monkey logo

rs-colors's Introduction

A color data type for Common Lisp.


Implemented color spaces:

   * CIE RGB
   * CIE XYZ
   * CIE xyY
   * CIE L*u*v*
   * CIE L*a*b*
   * sRGB
   * Adobe RGB

and

   * generic RGB
   * generic HSV/HSB
   * generic HSL
   * generic CMY
   * generic CMYK


Color space conversion is resolved by CLOS:

     (srgb-color-coordinates
      (make-cie-xyy-color 1/3 1/3 1))
      ==> 1
          0.976911599934101d0
          0.9587075033783911d0

     (change-class (make-rgb-color 1/2 1/2 1) 'hsv-color)
      ==> #<HSV-COLOR (240 1/2 1) {100661BCF3}>

     (change-class (make-hsv-color 240 1/2 1) 'rgb-color)
      ==> #<RGB-COLOR (1/2 1/2 1) {100660DA23}>


Colors can be read and written:

     (with-input-from-string (stream "#4E9A06 junk")
       (let ((color (read-color-html stream)))
         (values color
                 (format nil color-formatter-html color)
                 (format nil color-formatter-css-rgb color)
                 (format nil color-formatter-xcms-cie-xyy color))))
      ==> #<SRGB-COLOR (26/85 154/255 2/85) {1007930543}>
          "#4E9A06"
          "rgb(30.588236%, 60.39216%, 2.3529413%)"
          "CIExyY:0.33741736/0.566726/0.2474434"

You can define your own formats:

     (define-color-printer :fubar (color stream)
       (multiple-value-bind (a b c)
           (abc-color-coordinates color)
         (format stream "<ABC:~A,~A,~A>" a b c)))

     (print-color-fubar (make-srgb-color 199 21 133 :byte-size 8))


There are dictionaries with predefined colors:

     (ql:quickload "html-colors")
     html-color:silver
      ==> #<SRGB-COLOR (64/85 64/85 64/85) {100801A323}>

     (ql:quickload "svg-colors")
     svg-color:tan
      ==> #<SRGB-COLOR (14/17 12/17 28/51) {1007E023C3}>

     (ql:quickload "tango-colors")
     (tango-color:orange :light)
      ==> #<SRGB-COLOR (84/85 35/51 62/255) {1007E26E53}>

     (ql:quickload "x11-colors")
     x11-color:ghost-white
      ==> #<SRGB-COLOR (248/255 248/255 1) {1007E04953}>


Adding a new color space is straight forward:

     (defclass abc-color (color) (a b c))

     (defmethod color-coordinates ((color abc-color))
       (with-slots (a b c) color
         (values a b c)))

     (defun make-abc-color (a b c) ...)

     ;; Color conversion.
     (defun abc-from-cie-xyz (x y z) ...)
     (defun cie-xyz-from-abc (a b c) ...)

     (defgeneric abc-color-coordinates (color)
       (:method ((color abc-color))
         (color-coordinates color))
       ;; Otherwise, go via CIE XYZ.
       (:method ((color color))
         (multiple-value-call #'abc-from-cie-xyz
           (cie-xyz-color-coordinates color))))

     (defmethod cie-xyz-color-coordinates ((color abc-color))
       (multiple-value-call #'cie-xyz-from-abc
         (abc-color-coordinates color)))


Have fun!

rs-colors's People

Contributors

ralph-schleicher avatar sjl avatar

Watchers

 avatar James Cloos avatar  avatar

Forkers

jebcat1982

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.