Skip to main content
Roger Breton
Legend
March 18, 2024
Answered

Script to convert CIE Lab to CIE Lch?

  • March 18, 2024
  • 2 replies
  • 725 views

Looking for a way to work around InDesign limited color management capability.

Suppose I have some object selected on the page, the script would have to be able to extract the CIE Lab values out of the selected object, convert to Chroma and Hue angle, and paste the values in a new text frame. Doable? Easily?

 

This topic has been closed for replies.
Correct answer Roger Breton

Well, here's a simple script that displays an alert with the CIE Chroma and CIE Hue angle information.
InDesign HSB is useless because it is based on RGB. 
I am not by any means a JavaScript specialist.


The code is quite simple:

// Roger Breton / March 18 2024

// Get the active document
var doc = app.activeDocument;

// Check if there's a selection
if (doc.selection.length > 0) {
    // Get the first selected item
    var selectedItem = doc.selection[0];

    // Check if the selected item has a fill color
    if (selectedItem.fillColor !== undefined) {
        // Retrieve the fill color
        var fillColor = selectedItem.fillColor;
        var fillModel = selectedItem.fillColor.model.toString(); // SPOT
        var fillColorSpace = selectedItem.fillColor.space.toString(); // LAB
   

        var l_value = selectedItem.fillColor.colorValue[0];
        var a_value = selectedItem.fillColor.colorValue[1];
        var b_value = selectedItem.fillColor.colorValue[2];

        var l_float = parseFloat(l_value);
        var a_float = parseFloat(a_value);
        var b_float = parseFloat(b_value);

       
        // Calculate CIE Chroma (C)
        var chroma = Math.sqrt(Math.pow(a_value, 2) + Math.pow(b_value, 2));

        // Calculate Hue Angle (H) in radians
        var hue_radians = Math.atan2(b_value, a_value);

        // Convert Hue Angle to degrees
        var hue_degrees = (hue_radians * 180) / Math.PI;
        if (hue_degrees < 0) {
            hue_degrees += 360; // Make it positive
        }

        // Print the fill color details
        alert("Fill Color:\n\n" +
              "Color Model: " + fillModel + "\n" +
              "Color Space: " + fillColorSpace + "\n" +
              "L : " + l_value.toFixed(2) + "\n" +
              "A : " + a_value.toFixed(2) + "\n" +
              "B : " + b_value.toFixed(2) + "\n" +
              "Chroma : " + chroma.toFixed(2) + "\n" +
              "Hue Angle : " + hue_degrees.toFixed(2) + " degrees");
    } else {
        alert("The selected item does not have a fill color.");
    }
} else {
    alert("No item is currently selected.");
}

2 replies

rob day
Community Expert
Community Expert
March 18, 2024

Hi @Roger Breton A conversion to HSB would be the only option. InDesign’s HSB is not device independent, it’s color managed via the assigned RGB profile, so its apperance would change depending on the RGB profile assignment.

James Gifford—NitroPress
Legend
March 18, 2024

I have no solution here but if there is one, there are a couple of color folks who will provide it.

 

I'm just curious how extracting/calculating this fairly esoteric color value... and then just pasting in text values, is of use. For some manual entry at a later point? Seems as if any color value calculator could do that...

Roger Breton
Roger BretonAuthorCorrect answer
Legend
March 19, 2024

Well, here's a simple script that displays an alert with the CIE Chroma and CIE Hue angle information.
InDesign HSB is useless because it is based on RGB. 
I am not by any means a JavaScript specialist.


The code is quite simple:

// Roger Breton / March 18 2024

// Get the active document
var doc = app.activeDocument;

// Check if there's a selection
if (doc.selection.length > 0) {
    // Get the first selected item
    var selectedItem = doc.selection[0];

    // Check if the selected item has a fill color
    if (selectedItem.fillColor !== undefined) {
        // Retrieve the fill color
        var fillColor = selectedItem.fillColor;
        var fillModel = selectedItem.fillColor.model.toString(); // SPOT
        var fillColorSpace = selectedItem.fillColor.space.toString(); // LAB
   

        var l_value = selectedItem.fillColor.colorValue[0];
        var a_value = selectedItem.fillColor.colorValue[1];
        var b_value = selectedItem.fillColor.colorValue[2];

        var l_float = parseFloat(l_value);
        var a_float = parseFloat(a_value);
        var b_float = parseFloat(b_value);

       
        // Calculate CIE Chroma (C)
        var chroma = Math.sqrt(Math.pow(a_value, 2) + Math.pow(b_value, 2));

        // Calculate Hue Angle (H) in radians
        var hue_radians = Math.atan2(b_value, a_value);

        // Convert Hue Angle to degrees
        var hue_degrees = (hue_radians * 180) / Math.PI;
        if (hue_degrees < 0) {
            hue_degrees += 360; // Make it positive
        }

        // Print the fill color details
        alert("Fill Color:\n\n" +
              "Color Model: " + fillModel + "\n" +
              "Color Space: " + fillColorSpace + "\n" +
              "L : " + l_value.toFixed(2) + "\n" +
              "A : " + a_value.toFixed(2) + "\n" +
              "B : " + b_value.toFixed(2) + "\n" +
              "Chroma : " + chroma.toFixed(2) + "\n" +
              "Hue Angle : " + hue_degrees.toFixed(2) + " degrees");
    } else {
        alert("The selected item does not have a fill color.");
    }
} else {
    alert("No item is currently selected.");
}