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

CopyValues Script, Hex Code not accurate

Explorer ,
Aug 30, 2024 Aug 30, 2024

Copy link to clipboard

Copied

Hi again Geeks, I'm having an issue with this script, that copy the CMYK values and does it fine but I tried to get the Hex Code in other text layer and it put an Hex Code but not accurately. See Picture attached 

 

Hex Codes don't match, I just attached the script:

 

Screenshot 2024-08-30 at 10.57.04 AM.png

 

 

//#target photoshop

// Definir las coordenadas para las tres ubicaciones y sus capas Hex correspondientes
var coordinates = [
{ x: 174, y: 485, layerName: "PRIMARY", hexLayerName: "Hex1" }, // PRIMARY -> Hex1
{ x: 171, y: 722, layerName: "SECONDARY", hexLayerName: "Hex2" }, // SECONDARY -> Hex2
{ x: 168, y: 968, layerName: "TERTIARY", hexLayerName: "Hex3" } // TERTIARY -> Hex3
];

// Iterar sobre las coordenadas para actualizar las capas de texto correspondientes
for (var i = 0; i < coordinates.length; i++) {
var coord = coordinates[i];
 
// Añadir un color sampler en la coordenada especificada
var colorSampler = app.activeDocument.colorSamplers.add([coord.x, coord.y]);
 
// Obtener el color en formato CMYK
var cmykColor = colorSampler.color.cmyk;
 
// Formatear los valores CMYK para mostrar
var theColor =
Math.round(cmykColor.cyan) + "," +
Math.round(cmykColor.magenta) + "," +
Math.round(cmykColor.yellow) + "," +
Math.round(cmykColor.black);
 
// Convertir CMYK a Hex directamente
var hexColor = cmykToHex(cmykColor);
 
// Eliminar el color sampler para no dejar marcadores en el documento
colorSampler.remove();
 
// Buscar la capa de texto correspondiente para CMYK
try {
var textLayer = app.activeDocument.artLayers.getByName(coord.layerName);
 
// Verificar si la capa es de tipo texto
if (textLayer.kind == LayerKind.TEXT) {
// Pegar los valores CMYK en la capa de texto
textLayer.textItem.contents = theColor;
}
} catch (e) {
alert("No se encontró la capa de texto con el nombre: " + coord.layerName);
}
 
// Buscar la capa de texto correspondiente para Hex y asignar el valor
try {
var hexLayer = app.activeDocument.artLayers.getByName(coord.hexLayerName);
 
if (hexLayer.kind == LayerKind.TEXT) {
hexLayer.textItem.contents = hexColor;
}
} catch (e) {
alert("No se encontró la capa de texto con el nombre: " + coord.hexLayerName);
}
}

// Función para convertir CMYK directamente a Hex
function cmykToHex(cmyk) {
var cyan = cmyk.cyan / 100;
var magenta = cmyk.magenta / 100;
var yellow = cmyk.yellow / 100;
var black = cmyk.black / 100;
 
// Convertir CMYK a RGB
var red = Math.round(255 * (1 - cyan) * (1 - black));
var green = Math.round(255 * (1 - magenta) * (1 - black));
var blue = Math.round(255 * (1 - yellow) * (1 - black));
 
// Convertir RGB a Hex
return "#" + ((1 << 24) + (red << 16) + (green << 8) + blue).toString(16).slice(1).toUpperCase();
}
TOPICS
Actions and scripting , macOS , Windows

Views

137

Translate

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
Adobe
LEGEND ,
Aug 30, 2024 Aug 30, 2024

Copy link to clipboard

Copied

CMYK to hex probably wont be that useful. There are multiple ways to have, say, black (rich black has the other ink colors as well.) I'd probably make a temp copy, convert to RGB, and sample that temp file for the values.

Votes

Translate

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 ,
Aug 30, 2024 Aug 30, 2024

Copy link to clipboard

Copied

I would argue that the Color Picker Hex display is incorrect in this representation of the colour values, as the document is CMYK. Notice how there are no radio buttons next to the CMYK fields. 

Hex is a subset of RGB.

 

CMYK (via ICC PCS) > RGB > Hex

 

Votes

Translate

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 ,
Aug 30, 2024 Aug 30, 2024

Copy link to clipboard

Copied

I just found a solution having other Script getting the HexCode without converting from CMYK. Just Straight from rgb as you tell. So I combined both scripts and use it as a buttom on Action Panel.

Votes

Translate

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 ,
Aug 31, 2024 Aug 31, 2024

Copy link to clipboard

Copied

LATEST

Not just straight from RGB, but straight from sRGB.  That's how hex is normally used, by convention.

 

If hex numbers refer to, say, Adobe RGB, you'd get different hex codes for the same colors. This is color space specific. Which, BTW, also applies to CMYK.

Votes

Translate

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