Managing Color Swatches

Discussion of the xtools toolkit.

Managing Color Swatches

Postby xbytor » Tue Aug 15, 2006 4:15 pm

After this thread (with some helpful input and code from Larry), I've implemented a comprehensive API for working with Swatches.

The external class are SwatchPalette for performing opertaitons on the runtime palette and ColorSwatches for reading and writing swatch files in various formats.

Here's a first cut at the API. It still needs to be tuned and names will probably be tweaked, but the core (including all of the color conversions) appear to be working fine.

SwatchPalette
getColor(name) - get a color by name from the runtime palette. e.g SwatchPalette.getColor("FloralWhite");
saveRuntimeSwatches(file) - save the runtime palette to a .aco file.
addRuntime(color, name) - add a SolidColor to the runtime palette using the given name.
appendSwatchFile(file) - append the swatches in the file to the runtime palette.
loadSwatchFile(file) - replace the rutime palette with the swatches in the file.
deleteFromRuntime(index) - delete the color at the given index. I need to implement a named version.
renameRuntimeSwatch(index, newName) - delete the color at the given index. I need to implement a named version.
resetRuntime - reset the Swatches Palette back to its default values.

ColorSwatches
addColor(color, name) - add a color to a swatches table.
readFromRuntime() - load the runtime palette into a swatche table
readFromACOFile(file) - read in swatches from a .aco file
readFromX11File(file) - read in swatches from an X11-style rgb.txt file
readFromCSVFile(file) - read in swatches from a .csv file
writeToRuntime() - write the swatches table to the runtime palette
writeToACOFile(file) - write the swatches table to a .aco file
writeToTextFile(file) - write the swatches table to a text file
getColorByName(name) - retrieve a color from the table by name

The attached zip file contains the script (with test code), an X11 rgb.txt file, and the rgb.aco file that was generated from the rgb.txt file.

I've already done similar work for layer styles, including the file I/O. I'll probably write up a generic interface for the Palette code now that I've got a better sense on how to use it from JavaScript. The file I/O portions will have to be done on an "as needed" (or an "as funded") basis.

Thanks again to Larry for getting me moving in this direction.

-X
Attachments
ColorChooser.zip
(13.85 KiB) Downloaded 1747 times
xbytor
Site Admin
 
Posts: 2286
Joined: Thu May 19, 2005 12:11 pm
Location: In Limbo

Postby smoothdeity » Fri May 15, 2009 7:22 pm

I downloaded the attached file above and didn't see the API from the post anywhere. I also downloaded xtools and checked for it in there as well. Is it possible that the wrong file got attached, or am I just missing something?
smoothdeity
 
Posts: 2
Joined: Fri May 15, 2009 1:32 am

Postby xbytor » Fri May 15, 2009 7:36 pm

I just missing something?


Code: Select all
The external class are SwatchPalette for performing opertaitons on the runtime palette and ColorSwatches for reading and writing swatch files in various formats.


Looking in the xtools/xlib does reveal a file called 'ColorSwatches.jsx' which contains both of the required classes.
xbytor
Site Admin
 
Posts: 2286
Joined: Thu May 19, 2005 12:11 pm
Location: In Limbo

Postby smoothdeity » Fri May 15, 2009 8:17 pm

D'oh- I missed them in there. Can't thank you enough- this will be really useful...
smoothdeity
 
Posts: 2
Joined: Fri May 15, 2009 1:32 am

Re: Managing Color Swatches

Postby tjhb » Wed Feb 22, 2012 10:55 pm

I am trying hard to learn from this excellent example. Classes, function prototypes and so on are a bit over my head.

I had been trying to get access to the Swatches palette on my own, before finding this thread (and therefore ColorSwatches.jsx, which had been under my nose for some time!).

Now, thanks to the example in the SwatchPalette class, I can get a list of swatch names. I hadn't thought of going via the Preset Manager--thanks. I wonder whether the reason why you go via the Preset Manager is because getting names from the Swatches palette directly (via charID "Clrs") proved impossible. I couldn't see how to do it--but then I'm generally basically guessing as to what properties, lists, and values I should try to address and extract.

What I'd really like to do is, similarly, to get an array of RGBColor or SolidColor objects from the currently loaded swatches. As I understand it, you've managed to achieve this by making a .colors property internal to the custom ColorSwatches class. The array is initialized by first exporting the current swatches to an .aco file, then reading it back in. Is that, again, because you could not find any way of getting an array/list of colours directly out of the palette?

Is it really impossible? (What an oversight on Adobe's part if so!) I've tried everything I can imagine, but I'm really floundering. Any pointers would be very welcome.

Tim Baigent
tjhb
 
Posts: 2
Joined: Wed Feb 22, 2012 10:14 pm
Location: Wellington

Re: Managing Color Swatches

Postby xbytor » Thu Feb 23, 2012 1:00 am

The array is initialized by first exporting the current swatches to an .aco file, then reading it back in. Is that, again, because you could not find any way of getting an array/list of colours directly out of the palette?


At the time I implemented it, this was the only way of getting the colors. I do not think things have changed since then.
xbytor
Site Admin
 
Posts: 2286
Joined: Thu May 19, 2005 12:11 pm
Location: In Limbo

Re: Managing Color Swatches

Postby Mike Hale » Thu Feb 23, 2012 2:59 am

I do not think things have changed since then.


They have not. All you can get directly using Action Manager alone is the list of swatch names. Here is how to do that
Code: Select all
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var desc = executeActionGet(ref);// get the app descriptor
var presetsList = desc.getList(stringIDToTypeID('presetManager'));// the presets list
var swatchDesc = presetsList.getObjectValue(1);// swatches is the second key
var nameList = swatchDesc.getList(charIDToTypeID("Nm  "));// there is only one key in the swatch descriptor so get the list
var nameOfFirstSwatch = nameList.getString(0);// all the name list keys are strings data types so get the first swatch name
alert( nameOfFirstSwatch );


But X's code does the same thing and is easier to use. It let you do more with swatches than just get the names. And it works with other preset types.
Mike Hale
Site Admin
 
Posts: 4337
Joined: Fri Sep 30, 2005 10:52 pm
Location: USA

Re: Managing Color Swatches

Postby tjhb » Thu Feb 23, 2012 5:42 am

Thanks very much, both of you. It's great to know I'm not going mad, and can stop trying to do something impossible.

There are many oddities here, aren't there? It is as if Photoshop was never designed with a long future in mind.

Here's an example found this afternoon, doing the same sort of thing with layer styles. (Again I have been learning everything I can from the xtools, especially the module Styles.js.)

When it comes to applying preset styles to layers, you can normally do so directly, having just activated and referenced the target layer, and supplied the source style name (following Script Listerner output). But that only works if the target layer is of kind NORMAL, SOLIDFILL, PATTERNFILL or GRADIENTFILL. For any other layer type, you can't do it directly, neither in the GUI (which I'd never realised) nor by scripting. In the GUI nothing happens; in scripting a "general Photoshop error" is raised.

The reason this seems odd is that, for any of these layers, you can define new layer effects manually by using the fx button in the Layers panel. There, all ArtLayers behave in the same way.

Furthermore, you can always copy or move a set of effects from one ArtLayer to another ArtLayer, even for a type to which layer styles can't be applied. And since that operation can be scripted, it will be my workaround--via a dummy NORMAL layer which has first had the desired style preset applied to it.

I'm a new poster here but for years have been very grateful for your extremely thorough work. Thanks for answering my question so well too.
tjhb
 
Posts: 2
Joined: Wed Feb 22, 2012 10:14 pm
Location: Wellington


Return to xtools

Who is online

Users browsing this forum: No registered users and 0 guests