Skip to main content
Participating Frequently
February 23, 2009
Question

How to load color table in a indexed mode file??

  • February 23, 2009
  • 5 replies
  • 9054 views
How to load color table in a indexed mode file??
Actually, if i opened a indexed file and want to edit color table by loading another color table from desktop (or any other location), any way to do this through java scripting??
This topic has been closed for replies.

5 replies

Participating Frequently
March 16, 2009
Hi xbytor,

just wondering if you get a chance to look into this.

rgs
Robin
Participating Frequently
February 27, 2009
Thanxs again xbytor,
but it's still not working for me,
it shows an error "file is undefined" in the below code line:
file.open("r");

It would be a really great help if you define where should i change/apply path of the color table in the script or what changes should be done before i run it on my mac.

Rgs
Robin
Known Participant
March 16, 2009
> it shows an error "file is undefined" in the below code line:
> file.open("r");

Without looking at the code, the variable 'file' was either not assigned a valid
value or was not initialized at all. Or, it was misspelled, which I doubt. Put a
breakpoint on that line of code in ESTK and run the script. When the script
stops at that line, look at the 'Data Browser' panel to see if there are any
'File' objects in scope. If there are, 'file' should probably have been assigned
to one of them.
Participant
August 2, 2009

Thanks for the post of the script in advance.

There were a few things not directly working like the function charIDToTypeID() had to be properly used. The file mamber had to be prefixed by a "self.", too, to be correctly referenced for some reason.

It's utterly annoying that the photoshop API doesn't support palettes itself. We have to use palettes a lot in our company and this is a major drawback of Photoshop.

Again, thanks for the post.

Regards

Participating Frequently
February 24, 2009
hi xbytor,

thanks for your response, but I didn't quiet get it.

When i run it, following error occurs:

"ColorTable does not have a constructor".

following is the script i have writted:

------

var clrTbl = new ColorTable();
clrTbl.apply("/Users/robin/Desktop/AC5.act");

//============================= ColorTable ====================================

ColorTable = function() {
var self = this;

self.file = null;
self.colors = [];
};
ColorTable.RcsId = "$Id: ColorTable.jsx,v 1.3 2009/02/23 16:11:49 anonymous Exp $";

ColorTable.prototype.typename = "ColorTable";

//
// toString always returns 256 colors
//
ColorTable.prototype.toString = function() {
var self = this;
var str = '';

if (!self.colors || self.colors.length == 0) {
return undefined;
}

for (var i = 0; i < 256; i++) {
if (i >= self.colors.length) {
str += "0,0,0\n";
} else {
var sc = self.colors;
var rgb = sc.rgb;
str += rgb.red + ',' + rgb.green + ',' + rgb.blue + '\n';
}
};

return str;
};

ColorTable.prototype.read = function(str) {
var self = this;

self.colors = [];

for (var i = 0; i < 256; i++) {
var sc = new SolidColor();
var rgb = sc.rgb;

rgb.red = str.readByte();
rgb.green = str.readByte();
rgb.blue = str.readByte();
self.colors.push(sc);

if (str.eof()) {
break;
}
}
};

ColorTable.prototype.readFromFile = function(fptr) {
var self = this;

if (fptr.constructor == String) {
fptr = File(fptr);
}

self.file = fptr;

file.open("r");
file.encoding = 'BINARY';
var s = file.read();
file.close();

var str = {
str: s,
ptr: 0
}

str.readByte = function() {
var self = this;
if (self.ptr >= self.str.length) {
return -1;
}
return self.str.charCodeAt(self.ptr++);
}

str.eof = function() {
this.ptr = this.ptr.length;
}

self.read(str);
};

ColorTable.prototype.apply = function(fptr) {
var self = this;

self.readFromFile(fptr);
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putProperty(cTID('Clr '), cTID('ClrT'));
desc.putReference(cTID('null'), ref);

var list = new ActionList();

for (var i = 0; i < colors.length; i++) {
var color = colors;
var cdesc = new ActionDescriptor();
cdesc.putDouble(cTID('Rd '), color.rgb.red);
cdesc.putDouble(cTID('Grn '), color.rgb.green);
cdesc.putDouble(cTID('Bl '), color.rgb.blue);
list.putObject(cTID('RGBC'), cdesc);
}

desc.putList(cTID('T '), list);
return executeAction(cTID('setd'), desc, DialogModes.NO);
};

/*
Sample Usage

var clrTbl = new ColorTable();
clrTbl.apply("~/someTable.act");

//include "xlib/stdlib.js"
//include "xlib/Stream.js"

var clrTbl = new ColorTable();
clrTbl.readFromFile("~/someTable.act");
var str = clrTbl.toString();
alert(str);

*/

"ColorTable.jsx";
// EOF

-------

Also, please tell where do I locate these files on mac: "//include "xlib/stdlib.js"
//include "xlib/Stream.js"".
Known Participant
February 24, 2009
Robin_Massey@adobeforums.com wrote:
> When i run it, following error occurs:
>
> "ColorTable does not have a constructor".

Try moving this bit of code from the beginning of the file to the end.

> var clrTbl = new ColorTable();
> clrTbl.apply("/Users/robin/Desktop/AC5.act");
>


>
> Also, please tell where do I locate these files on mac: "//include "xlib/stdlib.js"
> //include "xlib/Stream.js"".

You don't need them. I removed the dependencies on these two file from the
script. Those lines of code do nothing now.

They are a part of my xtools package that can be found at
ps-scripts.sourceforge.net. There is a massive amount of code out there. That's
where the original implementation of the ColorTable script can be found.

-X
Participating Frequently
February 24, 2009
hi xbytor,

thanks for your response, but I didn't quiet get it.

When i run it, following error occurs:

"ColorTable does not have a constructor".

following is the script i have writted:

------

var clrTbl = new ColorTable();
clrTbl.apply("/Users/robin/Desktop/AC5.act");

//============================= ColorTable ====================================

ColorTable = function() {
var self = this;

self.file = null;
self.colors = [];
};
ColorTable.RcsId = "$Id: ColorTable.jsx,v 1.3 2009/02/23 16:11:49 anonymous Exp $";

ColorTable.prototype.typename = "ColorTable";

//
// toString always returns 256 colors
//
ColorTable.prototype.toString = function() {
var self = this;
var str = '';

if (!self.colors || self.colors.length == 0) {
return undefined;
}

for (var i = 0; i < 256; i++) {
if (i >= self.colors.length) {
str += "0,0,0\n";
} else {
var sc = self.colors;
var rgb = sc.rgb;
str += rgb.red + ',' + rgb.green + ',' + rgb.blue + '\n';
}
};

return str;
};

ColorTable.prototype.read = function(str) {
var self = this;

self.colors = [];

for (var i = 0; i < 256; i++) {
var sc = new SolidColor();
var rgb = sc.rgb;

rgb.red = str.readByte();
rgb.green = str.readByte();
rgb.blue = str.readByte();
self.colors.push(sc);

if (str.eof()) {
break;
}
}
};

ColorTable.prototype.readFromFile = function(fptr) {
var self = this;

if (fptr.constructor == String) {
fptr = File(fptr);
}

self.file = fptr;

file.open("r");
file.encoding = 'BINARY';
var s = file.read();
file.close();

var str = {
str: s,
ptr: 0
}

str.readByte = function() {
var self = this;
if (self.ptr >= self.str.length) {
return -1;
}
return self.str.charCodeAt(self.ptr++);
}

str.eof = function() {
this.ptr = this.ptr.length;
}

self.read(str);
};

ColorTable.prototype.apply = function(fptr) {
var self = this;

self.readFromFile(fptr);
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putProperty(cTID('Clr '), cTID('ClrT'));
desc.putReference(cTID('null'), ref);

var list = new ActionList();

for (var i = 0; i < colors.length; i++) {
var color = colors;
var cdesc = new ActionDescriptor();
cdesc.putDouble(cTID('Rd '), color.rgb.red);
cdesc.putDouble(cTID('Grn '), color.rgb.green);
cdesc.putDouble(cTID('Bl '), color.rgb.blue);
list.putObject(cTID('RGBC'), cdesc);
}

desc.putList(cTID('T '), list);
return executeAction(cTID('setd'), desc, DialogModes.NO);
};

/*
Sample Usage

var clrTbl = new ColorTable();
clrTbl.apply("~/someTable.act");

//include "xlib/stdlib.js"
//include "xlib/Stream.js"

var clrTbl = new ColorTable();
clrTbl.readFromFile("~/someTable.act");
var str = clrTbl.toString();
alert(str);

*/

"ColorTable.jsx";
// EOF

-------

Also, please tell where do I locate these files on mac: "//include "xlib/stdlib.js"
//include "xlib/Stream.js"".
Known Participant
February 23, 2009
> How to load color table in a indexed mode file??

I just converted some code that I have to work without the rest of my libs. It's
untested, but the conversion was easy as was adding the 'apply' method.

It's fairly simple to use:
var clrTbl = new ColorTable();
clrTbl.apply("~/someTable.act");

It has a builtin assumption that the color table is always exactly 256 colors in
size, which is probably fine.

-X


//
// ColorTable
// This class reads and writes color table files from disk.
// This assumes that the index color table is (at most) 256 colors long.
// If the file contains less, toString will pad with 0,0,0 entries.
//
// $Id: ColorTable.jsx,v 1.3 2009/02/23 16:11:49 anonymous Exp $
// Copyright: (c)2007, xbytor
// License: http://creativecommons.org/licenses/LGPL/2.1
// Contact: xbytor@gmail.com
//
//

//============================= ColorTable ====================================

ColorTable = function() {
var self = this;

self.file = null;
self.colors = [];
};
ColorTable.RcsId = "$Id: ColorTable.jsx,v 1.3 2009/02/23 16:11:49 anonymous Exp $";

ColorTable.prototype.typename = "ColorTable";

//
// toString always returns 256 colors
//
ColorTable.prototype.toString = function() {
var self = this;
var str = '';

if (!self.colors || self.colors.length == 0) {
return undefined;
}

for (var i = 0; i < 256; i++) {
if (i >= self.colors.length) {
str += "0,0,0\n";
} else {
var sc = self.colors;
var rgb = sc.rgb;
str += rgb.red + ',' + rgb.green + ',' + rgb.blue + '\n';
}
};

return str;
};

ColorTable.prototype.read = function(str) {
var self = this;

self.colors = [];

for (var i = 0; i < 256; i++) {
var sc = new SolidColor();
var rgb = sc.rgb;

rgb.red = str.readByte();
rgb.green = str.readByte();
rgb.blue = str.readByte();
self.colors.push(sc);

if (str.eof()) {
break;
}
}
};

ColorTable.prototype.readFromFile = function(fptr) {
var self = this;

if (fptr.constructor == String) {
fptr = File(fptr);
}

self.file = fptr;

file.open("r");
file.encoding = 'BINARY';
var s = file.read();
file.close();

var str = {
str: s,
ptr: 0
}

str.readByte = function() {
var self = this;
if (self.ptr >= self.str.length) {
return -1;
}
return self.str.charCodeAt(self.ptr++);
}

str.eof = function() {
this.ptr = this.ptr.length;
}

self.read(str);
};

ColorTable.prototype.apply = function(fptr) {
var self = this;

self.readFromFile(fptr);
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putProperty(cTID('Clr '), cTID('ClrT'));
desc.putReference(cTID('null'), ref);

var list = new ActionList();

for (var i = 0; i < colors.length; i++) {
var color = colors;
var cdesc = new ActionDescriptor();
cdesc.putDouble(cTID('Rd '), color.rgb.red);
cdesc.putDouble(cTID('Grn '), color.rgb.green);
cdesc.putDouble(cTID('Bl '), color.rgb.blue);
list.putObject(cTID('RGBC'), cdesc);
}

desc.putList(cTID('T '), list);
return executeAction(cTID('setd'), desc, DialogModes.NO);
};


/*
Sample Usage

var clrTbl = new ColorTable();
clrTbl.apply("~/someTable.act");

//include "xlib/stdlib.js"
//include "xlib/Stream.js"

var clrTbl = new ColorTable();
clrTbl.readFromFile("~/someTable.act");
var str = clrTbl.toString();
alert(str);

*/


"ColorTable.jsx";
// EOF