Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Using javascript to get Lab color information

Explorer ,
Feb 21, 2025 Feb 21, 2025

Copy link to clipboard

Copied

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.

TOPICS
Experiment , How-to , Scripting

Views

109
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Feb 21, 2025 Feb 21, 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/us
...

Votes

Translate
Community Expert ,
Feb 21, 2025 Feb 21, 2025

Copy link to clipboard

Copied

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!

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 27, 2025 Feb 27, 2025

Copy link to clipboard

Copied

This is great! thank you

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 27, 2025 Feb 27, 2025

Copy link to clipboard

Copied

LATEST

You're welcome!

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines