🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: adobe.photoshop.scripting
Article #287

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

#287
From: xbytor
Date: Mon, 23 Feb 2009 16:15
162 lines
4363 bytes
> 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[i];
       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[i];
     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
Technical Details
Message-ID: <1012954EA864C5906F53A40320C26E59@in.webcrossing.la2eafNXanI>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!newshub.sdsu.edu!webcrossing
References: <59b806b8.-1@webcrossing.la2eafNXanI>

Show Full Headers
Complete Article Headers
X-Google-Thread: f5839,8878efe6e0d3c58e
X-Google-Attributes: gidf5839,domainid0,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: xbytor@adobeforums.com
Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!newshub.sdsu.edu!webcrossing
Newsgroups: adobe.photoshop.scripting
Subject: Re: How to load color table in a indexed mode file??
Message-ID: <1012954EA864C5906F53A40320C26E59@in.webcrossing.la2eafNXanI>
Date: Mon, 23 Feb 2009 08:15:15 -0800
References: <59b806b8.-1@webcrossing.la2eafNXanI>
Lines: 167
User-Agent: Thunderbird 2.0.0.19 (Macintosh/20081209)
MIME-Version: 1.0
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit