Index/8Bit Color from Array

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

ezgoodnight

Index/8Bit Color from Array

Post by ezgoodnight »

I've been able to get script listener to give me some code for transforming a document into an indexed graphic based on predefined colors. What I'd like to do is be able to do something like this, but with a loop and pulling values from an existing array that contains Hex color recipes.

How would you go about doing this? I might could bash out an ugly solution using the function hexToRgb:

Code: Select allfunction hexToRgb(hex) {
   hex1 = hex.substring(0,2);
   hex2 = hex.substring(2,4);
   hex3 = hex.substring(4,6);
   
   var rgb = {r:0, g:0, b:0};
   
   rgb.r =parseInt(hex1, 16);
   rgb.g =parseInt(hex2, 16);
   rgb.b =parseInt(hex3, 16);
   return rgb;
}

And from the script listener:

Code: Select allvar idCnvM = charIDToTypeID( "CnvM" );
    var desc4 = new ActionDescriptor();
    var idT = charIDToTypeID( "T   " );
        var desc5 = new ActionDescriptor();
        var idCstP = charIDToTypeID( "CstP" );
            var list1 = new ActionList();
                var desc6 = new ActionDescriptor();
                var idRd = charIDToTypeID( "Rd  " );
                desc6.putDouble( idRd, 255.000000 );
                var idGrn = charIDToTypeID( "Grn " );
                desc6.putDouble( idGrn, 255.000000 );
                var idBl = charIDToTypeID( "Bl  " );
                desc6.putDouble( idBl, 255.000000 );
            var idRGBC = charIDToTypeID( "RGBC" );
            list1.putObject( idRGBC, desc6 );
                var desc7 = new ActionDescriptor();
                var idRd = charIDToTypeID( "Rd  " );
                desc7.putDouble( idRd, 222.000000 );
                var idGrn = charIDToTypeID( "Grn " );
                desc7.putDouble( idGrn, 41.000000 );
                var idBl = charIDToTypeID( "Bl  " );
                desc7.putDouble( idBl, 30.000000 );
            var idRGBC = charIDToTypeID( "RGBC" );
            list1.putObject( idRGBC, desc7 );
                var desc8 = new ActionDescriptor();
                var idRd = charIDToTypeID( "Rd  " );
                desc8.putDouble( idRd, 255.000000 );
                var idGrn = charIDToTypeID( "Grn " );
                desc8.putDouble( idGrn, 183.000000 );
                var idBl = charIDToTypeID( "Bl  " );
                desc8.putDouble( idBl, 16.000000 );
            var idRGBC = charIDToTypeID( "RGBC" );
            list1.putObject( idRGBC, desc8 );
                var desc9 = new ActionDescriptor();
                var idRd = charIDToTypeID( "Rd  " );
                desc9.putDouble( idRd, 106.000000 );
                var idGrn = charIDToTypeID( "Grn " );
                desc9.putDouble( idGrn, 211.000000 );
                var idBl = charIDToTypeID( "Bl  " );
                desc9.putDouble( idBl, 230.000000 );
            var idRGBC = charIDToTypeID( "RGBC" );
            list1.putObject( idRGBC, desc9 );
        desc5.putList( idCstP, list1 );
        var idDthr = charIDToTypeID( "Dthr" );
        var idDthr = charIDToTypeID( "Dthr" );
        var idDfsn = charIDToTypeID( "Dfsn" );
        desc5.putEnumerated( idDthr, idDthr, idDfsn );
        var idDthA = charIDToTypeID( "DthA" );
        desc5.putInteger( idDthA, 1 );
    var idIndC = charIDToTypeID( "IndC" );
    desc4.putObject( idT, idIndC, desc5 );
executeAction( idCnvM, desc4, DialogModes.NO );


My problem solving is usually more simple than the way this is written, so any help towards a solution that is more clear to a beginner like me would be appreciated.
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: Index/8Bit Color from Array

Post by Kukurykus »

I like the effect you got. That's interesting as I only tried with posterization where we can have 8 colours the less while in your function there is 4.
There's lack of .toSource() at the end of the returned result. Try this:

Code: Select all

function hexToRgb(hex) {
hex1 = hex.substring(0,2);
hex2 = hex.substring(2,4);
hex3 = hex.substring(4,6);

var rgb = {r:0, g:0, b:0};

rgb.r =parseInt(hex1, 16);
rgb.g =parseInt(hex2, 16);
rgb.b =parseInt(hex3, 16);
return rgb.toSource();
}

hexToRgb('0080FF')

and then see how easy you can obtain all colour converted data using following example:

Code: Select all

r = new SolidColor
r.rgb.red = 111
r.rgb.green = 222
r.rgb.blue = 123

alert(r.toSource())
you're now able to read what you ever want with RegEx from that 'little' colour info!



At the end I post my own function, but you must reverse it somehow as it's RGB to HEX:

Code: Select all

function COL(r, g, b) {
for(i = 0, C = [], RGB = new SolidColor; i < (rgb = ['red', 'green', 'blue']).length; i++) {
C.push(eval('RGB.rgb.' + rgb[i] + ' = ' + arguments[i]))
}
function S(v) {return h[v].toString()}
for(c = 0; c < C.length; c++) {
h = [], i = -1; while (i < 17) {i++, h.push(i < 10 ? i : String.fromCharCode(i + 87))}
for(q = 0; q < l = h.length; q++) {
for(w = 0; w < l; w++) if (+("0x" + (Q = S(q)) + (W = S(w))) == ~~(C[c])) C[c] = Q + W
}
}
alert(C.join('').toUpperCase())
}

COL(0, 128, 255)

EDIT:

Yeee I did it! Rewrote that ezgoodnight tried to do, so converted hex to RGB my way and then transformed totally ScriptListener RAW code, what I did first time in my adventure in Photoshop scripting world :D You may compare new look to that primitive Action Manager code :) However I know there are probably many other people who did that much time before me and not just once :/ but maybe not better 8-)

Code: Select all

function RGB() {return rgb = {r: 1, g: 2, b: 3}}; RGB()
arr = ['FFFFFF', 'DE291E', 'FFB710', '6AD3E6'], d = 0
function cTT(v) {return charIDToTypeID(v)}

function hexToRgb(hex) {
for(i in rgb) rgb[i] = parseInt(hex.substring((s = rgb[i] * 2) - 2, s), 16)
return m = rgb.toSource().match(RegExp((r = '\\w:(\\d{1,3}), ') + r + r.slice(0, -2)))
}

dsc1 = new ActionDescriptor(), dsc2 = new ActionDescriptor(), lst1 = new ActionList()

function M(v) {
eval('N = dsc' + (3 + d) + ' = new ActionDescriptor()')
for(i = 0; i < (a = ["Rd ", "Grn ", "Bl "]).length; i++) N.putDouble(cTT(a[i]), m[i + 1])
lst1.putObject(cTT("RGBC"), N)
}
while (d < arr.length) M(hexToRgb(arr[d])), d++, RGB()

dsc2.putList(cTT("CstP"), lst1), dsc2.putEnumerated(D = cTT("Dthr"), D, cTT("Dfsn")), dsc2.putInteger(cTT("DthA"), 1)
dsc1.putObject(cTT( "T " ), cTT("IndC"), dsc2), executeAction(cTT("CnvM"), dsc1, DialogModes.NO)
Attachments
RGB to HEX.zip
(481 Bytes) Downloaded 318 times
Indexed Color Mode.zip
(807 Bytes) Downloaded 343 times