Skip to main content
Participant
April 7, 2022
Question

Write foreground color CMYK, RGB, HEX

  • April 7, 2022
  • 6 replies
  • 845 views

I have to write the CMYK, RGB and hex of many colors in the format:
#ffffff
RGB: 0,0,0
CMYK: 0,0,0,0

I don't think it's possible to copy paste this natively in indesign so I'm looking for a script.
I've been trying to find a script that has been doing this for a while now, it doesn't seem complicated to me, but I can't find it and I don't know the javascript to do it myself. Can anybody help me ?

This topic has been closed for replies.

6 replies

rob day
Community Expert
Community Expert
April 15, 2022

This would get the Color Picker conversions for a selected fill color and copy them as text to the clipboard—make sure you have an object with a fill color selected. Just keep in mind that the Color Picker is color managed, so the conversions would depend on your document’s assigned color profiles.

 

Hex values typically are used for HTML coding, so for web hex values your assigned RGB profile needs to be sRGB.

 

 

 

 

 

getColorPickerValues()


function getColorPickerValues(){
    try {
        var fc = app.activeDocument.selection[0].fillColor
    }catch(e) {
        alert("Please select an object with a fill color applied");
        return
    }  

    var sp = fc.properties;
    var h, k, r, str;
    $.writeln(fc.name)

    if (fc.name == "None" || fc.name == "Black" || fc.name == "Registration") {
        alert("Can not get fill color conversions")
        return
    }

    if (fc.space == ColorSpace.CMYK) {
        k = fc.colorValue;
        fc.space = ColorSpace.RGB;
        r = roundColor(fc.colorValue);
        h = convertRGB(fc.colorValue);
        fc.properties = sp;
    } else {
        fc.space = ColorSpace.RGB;
        r = roundColor(fc.colorValue);
        h = convertRGB(r);
        fc.properties = sp;
        fc.space = ColorSpace.CMYK;
        k = roundColor(fc.colorValue);
        fc.properties = sp;
    }
    
    setClipboard("Hex: " + h + "\r" + "RGB: " + r + "\r" + "CMYK: " + k)
}


/**
* Round color values to nearest integer 
* @ param array of color values 
* @Return rounded array 
* 
*/
function roundColor(a){
    var ca = []
    for (var i = 0; i < a.length; i++){
        ca.push(Math.round(a[i]))
    };   
    return ca
}


/**
* Converts 8-bit RGB values to Hex 
* @ param array of RGB 
* @ return hex value 
* 
*/
function convertRGB(a){
    var h1 = toHex(Math.round(a[0]))
    if (h1.length == 1) {
        h1 = "0" + h1
    } 

    var h2 = toHex(Math.round(a[1]))
    if (h2.length == 1) {
        h2 = "0" + h2
    } 

    var h3 = toHex(Math.round(a[2]))
    if (h3.length == 1) {
        h3 = "0" + h3
    } 
    return "#" + h1 + h2 + h3
}

function toHex(d) {
  var r = d % 16;
  if (d - r == 0) {
    return toChar(r);
  }
  return toHex((d - r) / 16) + toChar(r);
}

function toChar(n) {
  const alpha = "0123456789ABCDEF";
  return alpha.charAt(n);
}


/**
* Sets the clipboard to the provided string
*/
function setClipboard(s){
    var tf = app.activeDocument.pages[0].textFrames.add()
	tf.parentStory.texts[0].select();
    tf.parentStory.contents = s;
    tf.parentStory.texts.everyItem().select(SelectionOptions.REPLACE_WITH)
	app.copy()
    tf.remove()
}

 

 

 

 

 

The pasted clipboard text after running the script with the blue circle selected:

 

rob day
Community Expert
Community Expert
April 15, 2022

Hi @ehho.collectif , I don’t think you can get the state of the Color Picker via scripting, but you can get a selection’s fill color properties, and from there get the hex, RGB, and CMYK color managed values. Would you have an object with a fill color selected when you want the foreground color values?

Willi Adelberger
Community Expert
Community Expert
April 15, 2022

Are you aware that these are only numbers and these numbers are linked to each other. Applications which have colormanagement, like InDesign, Illustrator or Photoshop help to translate these values from one color system to antoher and with different color profiles will translate those values differently.

Why do you need a script for those numbers as the CM does a good job to get the closest color from a different color space?

rob day
Community Expert
Community Expert
April 15, 2022

I think @ehho.collectif  wants to get the 3 conversion values for a color as text without having to open the Color Picker and writing them down.

rob day
Community Expert
Community Expert
April 8, 2022

Also, I’m not sure if you are trying to create swatches or colors from a list of hex values, or you are trying to get the color conversions for an existing set of swatches?

 

If you are trying to get color conversions, this might be helpful:

 

https://community.adobe.com/t5/indesign-discussions/branding-color-guide/td-p/10818696

Participant
April 15, 2022

No i don't try to do this i want to find a script ca write for me every forground color info. Actually for doing it i open the color picker panel, I memorise what I can, I close it and repet until everything is write 

rob day
Community Expert
Community Expert
April 8, 2022

I don't think it's possible to copy paste this natively in indesign

 

Hi @ehho.collectif , you can paste a hex code into the Color Picker’s #: field, which will also give you the RGB equivalent of the hex code.

 

Make sure you cursor is in one of the R, G, B fields—clicking OK will set the foreground color to the pasted HEX/RGB value, or click Add RGB Swatch to create a new RGB Swatch in the Swatches panel.

 

If you place your cursor in one of the C,M,Y, or K fields the returned CMYK values are a color managed conversion from your document’s assigned RGB profile to the document’s assigned CMYK profile :

 

Participant
April 15, 2022

thx i know this. On the picture you can see i have to write every color by hand i would like to find better wait to do it! 

I have to open the picker panel and memorise it to write it. I don't know any way to show this info on texte that i could copy past or at least show so don't don't need to open and close the panel to see it and write it.

brian_p_dts
Community Expert
Community Expert
April 8, 2022

Are these swatches in your InDesign document? If not, where are these colors coming from? Where are you writing them to? A text frame on the document? Which one? 

 

As you can see, many questions need to be answered, and thus this is more complicated than it might appear to you.