Skip to main content
Alexander Rott
Known Participant
February 21, 2025
Answered

Using javascript to get Lab color information

  • February 21, 2025
  • 1 reply
  • 573 views

Is it at all possible to have a script pull Lab color information from any swatch or object in Illustrator? I'm specifically trying to write a script to compare Lab color information between two different colors. I've run across the renderSwatchLegend script, but I can't seem to get it to display in a UI instead of rendering to an object. I'm not much of a coder, mostly am trying to use chatGPT to make this. Any help/direction would be appreciated.

Correct answer m1b

Hi @Alexander Rott, here is an example of deriving the Lab values from RGB, CMYK, or Grayscale colors. You should be able to incorporate into your own script. This will be helpful: app.convertSampleColor(). Also here is another answer that uses this method.

- Mark

 

 

/**
 * @file Show Lab Color Breakdown Demo.js
 *
 * Display the Lab color breakdown of the selected item's fillColor.
 *
 * @author m1b
 * @version 2025-02-22
 * @discussion https://community.adobe.com/t5/illustrator-discussions/using-javascript-to-get-lab-color-information/m-p/15169713
 */
(function () {

    var doc = app.activeDocument,
        item = doc.selection[0];

    if (
        undefined == item
        || !item.hasOwnProperty('fillColor')
        || 'NoColor' === item.fillColor.constructor.name
    )
        return alert('Please select an item with a fill color and try again.');

    // we only what an array of the color channel values
    var breakdown = getColorBreakdown(item.fillColor) || [];

    // pick the colorspace based on the number of channels in the breakdown
    var colorSpace = [
        null,
        ImageColorSpace.GrayScale,
        null,
        ImageColorSpace.RGB,
        ImageColorSpace.CMYK,
    ][breakdown.length];

    // this is what you are looking for!
    var labValues = app.convertSampleColor(colorSpace, breakdown, ImageColorSpace.LAB, ColorConvertPurpose.defaultpurpose);

    alert("L: " + labValues[0] + ", a: " + labValues[1] + ", b: " + labValues[2]);


})();

/**
 * Returns an array of color channel values.
 * @author m1b
 * @version 2022-05-23
 * @param {Swatch|SpotColor|Color} col - the source color.
 * @param {Number} [tintFactor] - a number in range 0..1 (default: 1).
 * @returns {Array<Number>}
 */
function getColorBreakdown(col, tintFactor) {

    tintFactor = tintFactor || 1;

    if (col.hasOwnProperty('color'))
        col = col.color;

    if ('SpotColor' === col.constructor.name)
        col = col.spot.color;

    if ('CMYKColor' === col.constructor.name)
        return [col.cyan * tintFactor, col.magenta * tintFactor, col.yellow * tintFactor, col.black * tintFactor];

    else if ('RGBColor' === col.constructor.name)
        return [col.red * tintFactor, col.green * tintFactor, col.blue * tintFactor];

    else if ('GrayColor' === col.constructor.name)
        return [col.gray * tintFactor];

};

 

Edit 2025-02-22: added code to choose the correct source ImageColorSpace. Oops!

1 reply

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
February 22, 2025

Hi @Alexander Rott, here is an example of deriving the Lab values from RGB, CMYK, or Grayscale colors. You should be able to incorporate into your own script. This will be helpful: app.convertSampleColor(). Also here is another answer that uses this method.

- Mark

 

 

/**
 * @file Show Lab Color Breakdown Demo.js
 *
 * Display the Lab color breakdown of the selected item's fillColor.
 *
 * @author m1b
 * @version 2025-02-22
 * @discussion https://community.adobe.com/t5/illustrator-discussions/using-javascript-to-get-lab-color-information/m-p/15169713
 */
(function () {

    var doc = app.activeDocument,
        item = doc.selection[0];

    if (
        undefined == item
        || !item.hasOwnProperty('fillColor')
        || 'NoColor' === item.fillColor.constructor.name
    )
        return alert('Please select an item with a fill color and try again.');

    // we only what an array of the color channel values
    var breakdown = getColorBreakdown(item.fillColor) || [];

    // pick the colorspace based on the number of channels in the breakdown
    var colorSpace = [
        null,
        ImageColorSpace.GrayScale,
        null,
        ImageColorSpace.RGB,
        ImageColorSpace.CMYK,
    ][breakdown.length];

    // this is what you are looking for!
    var labValues = app.convertSampleColor(colorSpace, breakdown, ImageColorSpace.LAB, ColorConvertPurpose.defaultpurpose);

    alert("L: " + labValues[0] + ", a: " + labValues[1] + ", b: " + labValues[2]);


})();

/**
 * Returns an array of color channel values.
 * @author m1b
 * @version 2022-05-23
 * @param {Swatch|SpotColor|Color} col - the source color.
 * @param {Number} [tintFactor] - a number in range 0..1 (default: 1).
 * @returns {Array<Number>}
 */
function getColorBreakdown(col, tintFactor) {

    tintFactor = tintFactor || 1;

    if (col.hasOwnProperty('color'))
        col = col.color;

    if ('SpotColor' === col.constructor.name)
        col = col.spot.color;

    if ('CMYKColor' === col.constructor.name)
        return [col.cyan * tintFactor, col.magenta * tintFactor, col.yellow * tintFactor, col.black * tintFactor];

    else if ('RGBColor' === col.constructor.name)
        return [col.red * tintFactor, col.green * tintFactor, col.blue * tintFactor];

    else if ('GrayColor' === col.constructor.name)
        return [col.gray * tintFactor];

};

 

Edit 2025-02-22: added code to choose the correct source ImageColorSpace. Oops!

Alexander Rott
Known Participant
February 27, 2025

This is great! thank you

m1b
Community Expert
Community Expert
February 27, 2025

You're welcome!