dojo/_base/Color.js

  • Provides:

    • dojo
  • dojo.Color.named

    • type
      Object
    • summary
  • dojo.Color

    • type
      Function
    • parameters:
      • color: (typeof Array|String|Object)
    • source: [view]
        if(color){ this.setColor(color); }
    • summary
      Takes a named string, hex string, array of rgb or rgba values,
      an object with r, g, b, and a properties, or another `dojo.Color` object
      and creates a new Color instance to work from.
    • example
      Work with a Color instance:
      
       var c = new dojo.Color();
       c.setColor([0,0,0]); // black
       var hex = c.toHex(); // #000000
    • example
      Work with a node's color:
      
       var color = dojo.style("someNode", "backgroundColor");
       var n = new dojo.Color(color);
       // adjust the color some
       n.r *= .5;
       console.log(n.toString()); // rgb(128, 255, 255);
  • dojo.blendColors

    • type
      Function
    • parameters:
      • start: (typeof dojo.Color)
      • end: (typeof dojo.Color)
      • weight: (typeof Number)
      • obj: (typeof dojo.Color)
    • source: [view]
        var t = obj || new d.Color();
        d.forEach(["r", "g", "b", "a"], function(x){
         t[x] = start[x] + (end[x] - start[x]) * weight;
         if(x != "a"){ t[x] = Math.round(t[x]); }
        });
        return t.sanitize(); // dojo.Color
    • summary
      Blend colors end and start with weight from 0 to 1, 0.5 being a 50/50 blend,
      can reuse a previously allocated dojo.Color object for the result
    • returns
      dojo.Color
  • dojo.colorFromRgb

    • type
      Function
    • parameters:
      • color: (typeof String)
      • obj: (typeof dojo.Color)
    • source: [view]
        var m = color.toLowerCase().match(/^rgba?\(([\s\.,0-9]+)\)/);
        return m && dojo.colorFromArray(m[1].split(/\s*,\s*/), obj); // dojo.Color
    • summary
      Returns a `dojo.Color` instance from a string of the form
      "rgb(...)" or "rgba(...)". Optionally accepts a `dojo.Color`
      object to update with the parsed value and return instead of
      creating a new object.
    • return_summary
      A dojo.Color object. If obj is passed, it will be the return value.
    • returns
      dojo.Color
  • dojo.colorFromHex

    • type
      Function
    • parameters:
      • color: (typeof String)
      • obj: (typeof dojo.Color)
    • source: [view]
        var t = obj || new d.Color(),
         bits = (color.length == 4) ? 4 : 8,
         mask = (1 << bits) - 1;
        color = Number("0x" + color.substr(1));
        if(isNaN(color)){
         return null; // dojo.Color
        }
        d.forEach(["b", "g", "r"], function(x){
         var c = color & mask;
         color >>= bits;
         t[x] = bits == 4 ? 17 * c : c;
        });
        t.a = 1;
        return t; // dojo.Color
    • summary
      Converts a hex string with a '#' prefix to a color object.
      Supports 12-bit #rgb shorthand. Optionally accepts a
      `dojo.Color` object to update with the parsed value.
    • return_summary
      A dojo.Color object. If obj is passed, it will be the return value.
    • returns
      dojo.Color
    • example
      
       var thing = dojo.colorFromHex("#ededed"); // grey, longhand
    • example
      
       var thing = dojo.colorFromHex("#000"); // black, shorthand
  • dojo.colorFromArray

    • type
      Function
    • parameters:
      • a: (typeof Array)
      • obj: (typeof dojo.Color)
    • source: [view]
        var t = obj || new d.Color();
        t._set(Number(a[0]), Number(a[1]), Number(a[2]), Number(a[3]));
        if(isNaN(t.a)){ t.a = 1; }
        return t.sanitize(); // dojo.Color
    • summary
      Builds a `dojo.Color` from a 3 or 4 element array, mapping each
      element in sequence to the rgb(a) values of the color.
    • return_summary
      A dojo.Color object. If obj is passed, it will be the return value.
    • returns
      dojo.Color
    • example
      
       var myColor = dojo.colorFromArray([237,237,237,0.5]); // grey, 50% alpha
  • dojo.colorFromString

    • type
      Function
    • parameters:
      • str: (typeof String)
      • obj: (typeof dojo.Color)
    • source: [view]
        var a = d.Color.named[str];
        return a && d.colorFromArray(a, obj) || d.colorFromRgb(str, obj) || d.colorFromHex(str, obj);
    • summary
      Parses `str` for a color value. Accepts hex, rgb, and rgba
      style color values.
    • description
      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)"
    • return_summary
      A dojo.Color object. If obj is passed, it will be the return value.
  • dojo.Color.named.black

    • summary
  • dojo.Color.named.silver

    • summary
  • dojo.Color.named.gray

    • summary
  • dojo.Color.named.white

    • summary
  • dojo.Color.named.maroon

    • summary
  • dojo.Color.named.red

    • summary
  • dojo.Color.named.purple

    • summary
  • dojo.Color.named.fuchsia

    • summary
  • dojo.Color.named.green

    • summary
  • dojo.Color.named.lime

    • summary
  • dojo.Color.named.olive

    • summary
  • dojo.Color.named.yellow

    • summary
  • dojo.Color.named.navy

    • summary
  • dojo.Color.named.blue

    • summary
  • dojo.Color.named.teal

    • summary
  • dojo.Color.named.aqua

    • summary
  • dojo.Color.named.transparent

    • summary
  • dojo.Color.r

    • summary
  • dojo.Color._set

    • type
      Function
    • parameters:
      • r: (typeof )
      • g: (typeof )
      • b: (typeof )
      • a: (typeof )
    • source: [view]
         var t = this; t.r = r; t.g = g; t.b = b; t.a = a;
    • summary
  • dojo.Color.setColor

    • type
      Function
    • parameters:
      • color: (typeof Array|String|Object)
    • source: [view]
         if(d.isString(color)){
          d.colorFromString(color, this);
         }else if(d.isArray(color)){
          d.colorFromArray(color, this);
         }else{
          this._set(color.r, color.g, color.b, color.a);
          if(!(color instanceof d.Color)){ this.sanitize(); }
         }
         return this; // dojo.Color
    • summary
      Takes a named string, hex string, array of rgb or rgba values,
      an object with r, g, b, and a properties, or another `dojo.Color` object
      and sets this color instance to that value.
    • returns
      dojo.Color
    • example
      
      	var c = new dojo.Color(); // no color
      	c.setColor("#ededed"); // greyish
  • dojo.Color.sanitize

    • type
      Function
    • source: [view]
         return this; // dojo.Color
    • summary
      Ensures the object has correct attributes
    • description
      the default implementation does nothing, include dojo.colors to
      augment it with real checks
    • returns
      dojo.Color
  • dojo.Color.toRgb

    • type
      Function
    • source: [view]
         var t = this;
         return [t.r, t.g, t.b]; // Array
    • summary
      Returns 3 component array of rgb values
    • returns
      Array
    • example
      
      	var c = new dojo.Color("#000000");
       	console.log(c.toRgb()); // [0,0,0]
  • dojo.Color.toRgba

    • type
      Function
    • source: [view]
         var t = this;
         return [t.r, t.g, t.b, t.a]; // Array
    • summary
      Returns a 4 component array of rgba values from the color
      represented by this object.
    • returns
      Array
  • dojo.Color.toHex

    • type
      Function
    • source: [view]
         var arr = d.map(["r", "g", "b"], function(x){
          var s = this[x].toString(16);
          return s.length < 2 ? "0" + s : s;
         }, this);
         return "#" + arr.join(""); // String
    • summary
      Returns a CSS color string in hexadecimal representation
    • returns
      String
    • example
      
       	console.log(new dojo.Color([0,0,0]).toHex()); // #000000
  • dojo.Color.toCss

    • type
      Function
    • parameters:
      • includeAlpha: (typeof Boolean)
    • source: [view]
         var t = this, rgb = t.r + ", " + t.g + ", " + t.b;
         return (includeAlpha ? "rgba(" + rgb + ", " + t.a : "rgb(" + rgb) + ")"; // String
    • summary
      Returns a css color string in rgb(a) representation
    • returns
      String
    • example
      
      	var c = new dojo.Color("#FFF").toCss();
      	console.log(c); // rgb('255','255','255')
  • dojo.Color.toString

    • type
      Function
    • source: [view]
         return this.toCss(true); // String
    • summary
      Returns a visual representation of the color
    • returns
      String
  • dojo

    • type
      Object
    • summary