Skip to main content
Known Participant
March 8, 2024
Answered

Get Photoshop text color

  • March 8, 2024
  • 1 reply
  • 699 views

hello

 

Using scripting, it's possible to get the current Photoshop text color and add it to the swatch color menu?

 

 

thanks!

This topic has been closed for replies.
Correct answer Stephen Marsh

Oh, it works! 
I have to learn more Javascript.

Thank you so much~! 🙂


@wellearn 

 

See how this works for you...

 

/* 
Text Layer Color to Swatch.jsx
v1.0 - 10th March 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/get-photoshop-text-color/td-p/14475583
*/

#target photoshop

// Store the current foreground colour
var savedForegroundColor = app.foregroundColor;

/*
Based on code from the late Michael Hale
https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-get-color-info-of-text-in-text-layer/m-p/3342131
*/
var ref = new ActionReference();
ref.putEnumerated(stringIDToTypeID("layer"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var desc = executeActionGet(ref);
var list = desc.getObjectValue(charIDToTypeID("Txt "));
var tsr = list.getList(charIDToTypeID("Txtt"));
var info = [];

for (var i = 0; i < tsr.count; i++) {
     var tsr0 = tsr.getObjectValue(i);
     var textStyle = tsr0.getObjectValue(charIDToTypeID("TxtS"));
     var color = textStyle.getObjectValue(charIDToTypeID('Clr '));
     var textColor = new SolidColor();
     textColor.rgb.red = color.getDouble(charIDToTypeID('Rd  '));
     textColor.rgb.green = color.getDouble(charIDToTypeID('Grn '));
     textColor.rgb.blue = color.getDouble(charIDToTypeID('Bl  '));
}

// Set the foreground colour
var solidColorValue = new SolidColor();
solidColorValue.rgb.red = Math.round(textColor.rgb.red);
solidColorValue.rgb.green = Math.round(textColor.rgb.green);
solidColorValue.rgb.blue = Math.round(textColor.rgb.blue);
app.foregroundColor = solidColorValue;

// Set a new swatch
var c2t = function (s) {
    return app.charIDToTypeID(s);
};
var s2t = function (s) {
    return app.stringIDToTypeID(s);
};
var descriptor = new ActionDescriptor();
var descriptor2 = new ActionDescriptor();
var descriptor3 = new ActionDescriptor();
var reference = new ActionReference();
reference.putClass(s2t("colors"));
descriptor.putReference(s2t("null"), reference);
descriptor2.putString(s2t("name"), ~~textColor.rgb.red + "R " + ~~textColor.rgb.green + "G " + ~~textColor.rgb.blue + "B"); // Swatch name
descriptor2.putString(s2t("ID"), uuid()); // Create a faux random ID
descriptor2.putString(c2t("CCid"), "");
descriptor3.putDouble(s2t("red"), textColor.rgb.red);
descriptor3.putDouble(s2t("grain"), textColor.rgb.green);
descriptor3.putDouble(s2t("blue"), textColor.rgb.blue);
descriptor2.putObject(s2t("color"), s2t("RGBColor"), descriptor3);
descriptor.putObject(s2t("using"), s2t("colors"), descriptor2);
//descriptor.putBoolean(s2t("pushToDesignLibraries"), true); // Add to CC library
executeAction(s2t("make"), descriptor, DialogModes.ALL);

// Restore the original foreground colour
app.foregroundColor = savedForegroundColor;

function uuid() {
  /* https://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid */
  var chars = '0123456789abcdef'.split('');

  var uuid = [],
    rnd = Math.random,
    r;
  uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
  uuid[14] = '4'; // version 4

  for (var i = 0; i < 36; i++) {
    if (!uuid[i]) {
      r = 0 | rnd() * 16;

      uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r & 0xf];
    }
  }

  return uuid.join('');
}

 

1 reply

Stephen Marsh
Community Expert
Community Expert
March 8, 2024

I have a long-winded scripting hack, but there is a better way:

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-get-color-info-of-text-in-text-layer/m-p/3342131

 

It is of course presumed that the text has a single colour.

wellearnAuthor
Known Participant
March 9, 2024

Thank you Stephen! 🙂

But I found that there is PS version issue. 
May I ask your help? 

 

//

function getFontInfo() {
var ref = new ActionReference();
ref.putEnumerated(stringIDToTypeID("layer"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var desc = executeActionGet(ref)
var list = desc.getObjectValue(charIDToTypeID("Txt "));
var tsr = list.getList(charIDToTypeID("Txtt"));
var info = new Array;
for (var i = 0; i < tsr.count; i++) {
var tsr0 = tsr.getObjectValue(i);
var from = tsr0.getInteger(charIDToTypeID("From"));
var to = tsr0.getInteger(charIDToTypeID("T "));
var range = [from, to];
var textStyle = tsr0.getObjectValue(charIDToTypeID("TxtS"));
var font = textStyle.getString(charIDToTypeID("FntN"));
var size = textStyle.getDouble(charIDToTypeID("Sz "));
var color = textStyle.getObjectValue(charIDToTypeID('Clr '));
var textColor = new SolidColor;
textColor.rgb.red = color.getDouble(charIDToTypeID('Rd '));
textColor.rgb.green = color.getDouble(charIDToTypeID('Grn '));
textColor.rgb.blue = color.getDouble(charIDToTypeID('Bl '));
info.push([range, font, size, textColor.rgb.hexValue]);
}
return info;
}

var info = getFontInfo();
alert("there are " + info.length + " textRanges\rRange 1 details\rRangeStart " + info[0][0][0] + ", rangeEnd:" + info[0][0][1] + ", Font:" + info[0][1] + ", size:" + info[0][2] + ", colorHexValue:" + info[0][3]);

//

 

I copy this Javascript code but it doesn't worked @_@@

 

Stephen Marsh
Community Expert
Community Expert
March 9, 2024

I was playing around with it to isolate only the colour info and didn't have any issues:

 

alert("Text color: " + getFontColor());

function getFontColor() {
     /* Based on code from the late Michael Hale
     https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-get-color-info-of-text-in-text-layer/m-p/3342131
     */
     var ref = new ActionReference();
     ref.putEnumerated(stringIDToTypeID("layer"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
     var desc = executeActionGet(ref);
     var list = desc.getObjectValue(charIDToTypeID("Txt "));
     var tsr = list.getList(charIDToTypeID("Txtt"));
     var info = [];
     for (var i = 0; i < tsr.count; i++) {
          var tsr0 = tsr.getObjectValue(i);
          var textStyle = tsr0.getObjectValue(charIDToTypeID("TxtS"));
          var color = textStyle.getObjectValue(charIDToTypeID('Clr '));
          var textColor = new SolidColor();
          textColor.rgb.red = color.getDouble(charIDToTypeID('Rd  '));
          textColor.rgb.green = color.getDouble(charIDToTypeID('Grn '));
          textColor.rgb.blue = color.getDouble(charIDToTypeID('Bl  '));
          info.push([textColor.rgb.red.toFixed(), textColor.rgb.green.toFixed(), textColor.rgb.blue.toFixed()]);
     }
     return info;
}

 

I'm still trying to figure out how to split the returned array into separate R, G, B values.