Skip to content
fabiantheblind edited this page Nov 19, 2015 · 3 revisions

Colorbrewer is a nice application for creating color schemes. It comes with an .ase (Adobe Swatch Exchange) export. The downside is this export creates cmyk swatches when imported to InDesign. So I wrote up a tiny helper.
Also I wanted to try the Array.prototype.map function. Unfortunately it is not present in ExtendScript so I had to add it as prototype.

// this is what your get if you get when you export from http://colorbrewer2.org
//
// ["rgb(197,27,125)", "rgb(233,163,201)", "rgb(253,224,239)", "rgb(247,247,247)", "rgb(230,245,208)", "rgb(161,215,106)", "rgb(77,146,33)"]
//
// and select the javaScript colors
// lets play with it

/**
 * this is our runner function.
 * Everything is in here
 * @return {None} returns nothing
 */
var run = function() {

  // our colors
  var cols = [
    "rgb(197,27,125)",
    "rgb(233,163,201)",
    "rgb(253,224,239)",
    "rgb(247,247,247)",
    "rgb(230,245,208)",
    "rgb(161,215,106)",
    "rgb(77,146,33)"
  ];

  var d = app.activeDocument; // current document

  // unfortunately the Array map function does not exist in ExtendScript
  // see this
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
  //
  cols.map(function(i) {
    i = i.replace(/rgb\((.*?)\)/g, "$1"); // remove unused stuff
    var vals = i.split(","); // make a new array from it

    var colname = "color " + vals.toString(); // create a name
    var color = null; // will be the color
    try {
      // this is a test if the color alredy exists
      // if so
      color = d.colors.item(colname);
      var tempname = color.name; // <-- This will throw an error
    } catch (e) {
      // the color with that name does not exist.
      // Create it
      color = d.colors.add({
        name: colname,
        model: ColorModel.PROCESS,
        space: ColorSpace.RGB,
        colorValue: [parseInt(vals[0]), parseInt(vals[1]), parseInt(vals[2])]
      }); // add a new color
    } // end catch
  }); // end col.map
}; // end run function

// This is a prototype function
if (!Array.prototype.map) {
  Array.prototype.map = function(fun /*, thisArg */ ) {
    "use strict";

    if (this === void 0 || this === null)
      throw new TypeError();

    var t = Object(this);
    var len = t.length >>> 0;
    if (typeof fun !== "function")
      throw new TypeError();

    var res = new Array(len);
    var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
    for (var i = 0; i < len; i++) {
      // NOTE: Absolute correctness would demand Object.defineProperty
      //       be used.  But this method is fairly new, and failure is
      //       possible only if Object.prototype or Array.prototype
      //       has a property |i| (very unlikely), so use a less-correct
      //       but more portable alternative.
      if (i in t)
        res[i] = fun.call(thisArg, t[i], i, t);
    }

    return res;
  };
}

run(); // <-- run that thing
Clone this wiki locally