dojo/_base/Color (version 1.10)

Summary

Takes a named string, hex string, array of rgb or rgba values, an object with r, g, b, and a properties, or another Color object and creates a new Color instance to work from.

Usage

Color(color);
Parameter Type Description
color Array | String | Object

See the dojo/_base/Color reference documentation for more information.

Examples

Example 1

Work with a Color instance:

require(["dojo/_base/color"], function(Color){
    var c = new Color();
    c.setColor([0,0,0]); // black
    var hex = c.toHex(); // #000000
});

Example 2

Work with a node's color:

  require(["dojo/_base/color", "dojo/dom-style"], function(Color, domStyle){
      var color = domStyle("someNode", "backgroundColor");
      var n = new Color(color);
      // adjust the color some
      n.r *= .5;
      console.log(n.toString()); // rgb(128, 255, 255);
  });

Property Summary

  • a
  • b
  • g
  • namedDictionary list of all CSS named colors, by name.
  • r

Method Summary

  • _set(r,g,b,a)
  • blendColors(start,end,weight,obj) Blend colors end and start with weight from 0 to 1, 0.5 being a 50/50 blend, can reuse a previously allocated Color object for the result
  • fromArray(a,obj) Builds a Color from a 3 or 4 element array, mapping each element in sequence to the rgb(a) values of the color.
  • fromHex(color,obj) Converts a hex string with a '#' prefix to a color object.
  • fromRgb(color,obj) get rgb(a) array from css-style color declarations
  • fromString(str,obj) Parses str for a color value.
  • makeGrey(g,a) creates a greyscale color with an optional alpha
  • sanitize() makes sure that the object has correct attributes
  • setColor(color) Takes a named string, hex string, array of rgb or rgba values, an object with r, g, b, and a properties, or another Color object and sets this color instance to that value.
  • toCmy() Convert this Color to a CMY definition.
  • toCmyk() Convert this Color to a CMYK definition.
  • toCss(includeAlpha) Returns a css color string in rgb(a) representation
  • toHex() Returns a CSS color string in hexadecimal representation
  • toHsl() Convert this Color to an HSL definition.
  • toHsv() Convert this Color to an HSV definition.
  • toRgb() Returns 3 component array of rgb values
  • toRgba() Returns a 4 component array of rgba values from the color represented by this object.
  • toString() Returns a visual representation of the color

Properties

a
Defined by: dojo/_base/Color
b
Defined by: dojo/_base/Color
g
Defined by: dojo/_base/Color
named
Defined by: dojo/_base/Color

Dictionary list of all CSS named colors, by name. Values are 3-item arrays with corresponding RG and B values.

r
Defined by: dojo/_base/Color

Methods

_set(r,g,b,a)
Defined by dojo/_base/Color
Parameter Type Description
r undefined
g undefined
b undefined
a undefined
blendColors(start,end,weight,obj)
Defined by dojo/_base/Color

Blend colors end and start with weight from 0 to 1, 0.5 being a 50/50 blend, can reuse a previously allocated Color object for the result

Parameter Type Description
start dojo/_base/Color
end dojo/_base/Color
weight Number
obj dojo/_base/Color
Optional
Returns:undefined
fromArray(a,obj)
Defined by dojo/_base/Color

Builds a Color from a 3 or 4 element array, mapping each element in sequence to the rgb(a) values of the color.

Parameter Type Description
a Array
obj dojo/_base/Color
Optional
Returns:any | undefined

A Color object. If obj is passed, it will be the return value.

Examples

Example 1

require(["dojo/_base/color"], function(Color){
    var myColor = new Color().fromArray([237,237,237,0.5]); // grey, 50% alpha
});
fromHex(color,obj)
Defined by dojo/_base/Color

Converts a hex string with a '#' prefix to a color object. Supports 12-bit #rgb shorthand. Optionally accepts a Color object to update with the parsed value.

Parameter Type Description
color String
obj dojo/_base/Color
Optional
Returns:any

A Color object. If obj is passed, it will be the return value.

Examples

Example 1

require(["dojo/_base/color"], function(Color){
    var thing = new Color().fromHex("#ededed"); // grey, longhand
    var thing2 = new Color().fromHex("#000"); // black, shorthand
});
fromRgb(color,obj)
Defined by dojo/colors

get rgb(a) array from css-style color declarations

this function can handle all 4 CSS3 Color Module formats: rgb, rgba, hsl, hsla, including rgb(a) with percentage values.

Parameter Type Description
color String
obj dojo/_base/Color
Optional
Returns:null
fromString(str,obj)
Defined by dojo/_base/Color

Parses str for a color value. Accepts hex, rgb, and rgba style color values.

Acceptable input values for str may include arrays of any form accepted by dojo.colorFromArray, hex strings such as "#aaaaaa", or rgb or rgba strings such as "rgb(133, 200, 16)" or "rgba(10, 10, 10, 50)"

Parameter Type Description
str String
obj dojo/_base/Color
Optional
Returns:any

A Color object. If obj is passed, it will be the return value.

makeGrey(g,a)
Defined by dojo/colors

creates a greyscale color with an optional alpha

Parameter Type Description
g Number
a Number
Optional
sanitize()
Defined by dojo/colors

makes sure that the object has correct attributes

setColor(color)
Defined by dojo/_base/Color

Takes a named string, hex string, array of rgb or rgba values, an object with r, g, b, and a properties, or another Color object and sets this color instance to that value.

Parameter Type Description
color Array | String | Object
Returns:function

Takes a named string, hex string, array of rgb or rgba values, an object with r, g, b, and a properties, or another Color object and sets this color instance to that value.

Examples

Example 1

require(["dojo/_base/color"], function(Color){
    var c = new Color(); // no color
    c.setColor("#ededed"); // greyish
});
toCmy()
Defined by dojox/color/_base

Convert this Color to a CMY definition.

Returns:object
toCmyk()
Defined by dojox/color/_base

Convert this Color to a CMYK definition.

Returns:object
toCss(includeAlpha)
Defined by dojo/_base/Color

Returns a css color string in rgb(a) representation

Parameter Type Description
includeAlpha Boolean
Optional
Returns:string

Examples

Example 1

require(["dojo/_base/color"], function(Color){
    var c = new Color("#FFF").toCss();
    console.log(c); // rgb('255','255','255')
});
toHex()
Defined by dojo/_base/Color

Returns a CSS color string in hexadecimal representation

Returns:string

Examples

Example 1

require(["dojo/_base/color"], function(Color){
    console.log(new Color([0,0,0]).toHex()); // #000000
});
toHsl()
Defined by dojox/color/_base

Convert this Color to an HSL definition.

Returns:object
toHsv()
Defined by dojox/color/_base

Convert this Color to an HSV definition.

Returns:object
toRgb()
Defined by dojo/_base/Color

Returns 3 component array of rgb values

Returns:Array

Examples

Example 1

require(["dojo/_base/color"], function(Color){
    var c = new Color("#000000");
    console.log(c.toRgb()); // [0,0,0]
});
toRgba()
Defined by dojo/_base/Color

Returns a 4 component array of rgba values from the color represented by this object.

Returns:Array
toString()
Defined by dojo/_base/Color

Returns a visual representation of the color

Returns:undefined
Error in the documentation? Can’t find what you are looking for? Let us know!