View
 

Color Library

Page history last edited by Corey 9 months ago

 

type: utility

release: 2.0

priority: medium

status: in development

documentation: http://oksoclap.com/jquery-color-api

demo: N/A

 

 


 

1 - Description:

 

A common set of functions for color storage and manipulation that should be utilitised by UI widgets wherever required. Also for use independently of the UI core.

 

Color object to parse color strings such as #rrggbb, "red",  "rgb(255,0,0)" and explicit color values as arguments/array/named-object into a color object. Every color object has transform functions to adjust/assign color values as well as transform between one color and another at a given percentage of blend.

 

v2.0 of jquery-color is the new jQuery.Color function object, similar to jQuery itself (creates a new object with prototype methods)

 

v2.1 of jquery-color will implement hsla support. 

 

 


 

2 - Visual Design:

 

N/A

 


 

 

3 - Functional Specifications/Requirements:

 

MILESTONE 1: Basic Functionality / animation

Status: Needs Review

Branch: https://github.com/jquery/jquery-color/

 

The $.Color function object:

 

 

  • Returns a new Color object, similar to $() or $.Event
  • Accepts the color value to create the new Color object with, and assigns $.Color.fn as it's prototype
    • $.Color( { red: red, green: green, blue: blue, alpha: alpha } )
    • $.Color( [ red, green, blue, alpha ] ) 
    • $.Color( "#rrggbbaa" || "#rrggbb" )
    • $.Color( "rgba(255,255,255,1)" || "rgb(255,255,255)" )
    • $.Color( anotherColorObject )  
  • Has static properties available
    • .names
      • Color names to rgb values
      • "transparent"
        • it will return "undefined" for rgb unless you specify colors for these values... the transition funciton will know if the color portion is undefined that you want to scale only the alpha - allowing for something like this:

          $.Color("#abcdef").transition("transparent", 0.5) - animating to the value "transparent" will use #abcdef the whole time...

    • .support 

      • .rgba - The browser supports native rgba() 

 

 

The jQuery.Color.fn / prototype methods

     Getters 

  • .red() - returns the "red" component of the color ( int 0-255)
  • .blue() - returns the "blue" component of the color ( int 0-255)
  • .green() - returns the "green" component of the color ( int 0-255)
  • .alpha() - returns the "alpha" component of the color ( int 0-255) 
  • .rgba() - returns the rgba "tupple" as an array: [ red, green, blue, alpha ] 

 

     Setters

 

  • .red( intOrString ) - sets red to specified value and returns a copy of the color object
  • .blue( intOrString ) - sets blue to specified value and returns a copy of the color object
  • .green( intOrString ) - sets green to specified value and returns a copy of the color object
  • .alpha( intOrString ) - sets alpha to specified value and returns a copy of the color object 
  • .rgba( redOrArrayOrObject, green, blue, alpha ) - assigns all values and returns a copy of the color object 
    • rgba( red, green, blue, alpha )
    • rgba( { red: red, green: green, blue: blue, alpha: alpha } )
    • rgba( [ red, green, blue, alpha ] )

  

     Manipulation

 

  • transition( color, distance ) - the color distance from the objects color to the provided color, between a distance of 0.0 and 1.0
  • blend( opaqueColor ) - applies the objects color on top of the provided color using alpha compositing

 

     Other methods

 

  • toRgbaString() - returns string as css, e.g. "rgba( 255,255,255,1 )"
  • toHexString( includeAlpha ) = returns hex value with pound sign, e.g., "#123456"
    • includeAlpha is true, returns string in #rrggbbaa format
    • includeAlpha is false, returns string in #rrggbb format 

 

Internal Storage/Options

  • Internally, rgba is stored as
    • colorObject._rgba[0] = red
    • colorObject._rgba[1] = green
    • colorObject._rgba[2] = blue
    • colorObject._rgba[3] = alpha

 

CSS Hooks

 

  • Allow setting css values to a Color
    • Calculate "transparent" colors based on nearest background color in browsers which can't support rgba 

 

Animation Support

  • Reproducing the same animation options available from the original jQuery-color with the new library

 

 

 

Milestone 2: HSL Support - Ready for Review

 

The internal color object properties will probably still be stored in RGBA. However if initiated with an HSLA color and hsla operations are made, it will store/use the _hsla data and simply update the _rgba information. Once an RGBA operation is performed on HSLA, however, the _hsla cache is removed and all operations will continue based off of rgb. (Condering most operations will by
RGBA, bothering to update the cache would be a perf hit)
The ._hsla array will follow the same format as ._rbga, [h,s,l,a]. 
Colors with 0 saturation, or 100%/0% lightness will be stored with a hue of 0
HSL prototype functions
     Getters
  • .hue() - returns the "hue" of the color ( int 0-359 )
  • .saturation() - returns the "saturation" of the color( Float 0.0-1.0)
  • .lightness() - returns the "lightness" of the color( Float 0.0-1.0) 
  • .hsla() - returns a HSLA tuple as an array: [hue, saturation, lightness, alpha]

 

     Setters

 

  • .hue( intOrString ) - returns the "hue" of the color ( int 0-359 )
  • .saturation( floatOrString ) - returns the "saturation" of the color( Float 0.0-1.0)
  • .lightness( floatOrString ) - returns the "lightness" of the color( Float 0.0-1.0) 
  • .hsla( hueOrArrayOrObject, saturation, lightness, alpha) - returns a HSLA tuple as an array: [hue, saturation, lightness, alpha] 
    • hsla( hue, saturation, lightness, alpha )
    • hsla( { hue: hue, saturation: saturation, lightness: lightness, alpha: alpha } )
    • hsla( [ hue, saturation, lightness, alpha ] )
    • hsla(325,50%,32%,1)" || "hsl(325,50%,32%)" )
    • hsla( anotherColorObject ) 

 

     Other

 

  • toHslaString() - returns the css string, e.g., "hsla( 330, 75%, 25%, 1)" 

 

 

 


 

4 - Markup & Style

 

N/A

 


 

5 - Latest version of plugin:

 

https://github.com/gnarf37/jquery-color

 


 

6 - Open issues being discussed

 

 

  • Should detect support for hsla/rgba - should these toString functions return what the browser supports? Something like:
    • toRgbaString() - returns what the browser supports, rgb or rgba
    • toRgbaString( false ) - returns if alpha false, that is, rgb(255,255,255)
    • toRgbaString( true ) - returns if alpha true, that is, rgba(255,255,255,1); 

 

 

 

 

Comments (5)

Carsten Klein said

at 2:13 pm on Aug 13, 2009


please also support quadruples as the color value, including the alpha transparency for a given color.

see my comment @ color picker for some more info

Mark Gibson said

at 6:00 am on Aug 27, 2009

The current library already supports this for all of the implement colour spaces (ie RGB, HSV). See the docs for more details: http://www.adaptavist.com/display/free/jQuery+Colour+Library

Carsten Klein said

at 2:14 pm on Aug 13, 2009

please also implement named properties for the set of components of a given color, i.e. .red, .green, .blue, .alpha etc.

Mark Gibson said

at 6:14 am on Aug 27, 2009

The colour tuples have a fixed order, so i'm not sure this is entirely necessary as it would just add extra complication and overhead - ie. having to set the properties and maintain the properties - if getter/setter overrides were supported in all browser this would be easy - but they aren't. Instead we could introduce getter methods, eg: $.Color('#fff').red();
This would have the added advantage of the automatic colour space conversion, so you could extract any component regardless of the colour space.

gero3 said

at 2:33 pm on Nov 20, 2009

i am adding full support for Hsl on my repo on github

will try to add setter and getter methods like html does it

You don't have permission to comment on this page.