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!