Showcases the Adobe.ASE.load.js
file which can load a Photoshop ASE color palette file from the server and convert it to
both a standard JS object hierarchy (which includes the group and color names) and a flattened array which can be fed
directly to d3.scale.ordinal().range(...)
for quick & easy as a color palette for d3.js visualizations.
Using this JavaScript software, you or your designer can concoct any color palette in tools such as Adobe Photoshop, place the exported Adobe ASE file on any server as a static file and you’re Up & Go!
ZERO server-side / developer / tool effort required to convert artist-created color palettes before those are usable in your web projects, particularly when you’re using D3.js to visualize your data.
Adobe Photoshop > Swatches > Save Swatches for Exchange > upload to server (static content) > use in any d3 visualization
Certainly. Get your derrière over to the Adobe Kuler web site to peruse a zillion color palettes created by the community.
And if that’s not enough, there’s a lot of color tools out there which produce ASE files for you to download: anyone who exports color templates for use in Photoshop is producing ASE files!
The code uses the excellent jDataview.js from Christopher Chedeau.
Using this is very simple:
make sure you’ve loaded both the jDataview.js
and Adobe.ASE.load.js
scripts
fetch the ASE file from your web store (server) using the API call: adobe.ase.load(file, callback)
where callback
can be any
function you specify. The callback function will receive up to 3 arguments: (palette, flat_rgb_set, error_msg).
palette
: the JavaScript object structure which contains all the info, including color and group names, which was contained in the ASE file.
See the callback.palette.arg.format.md
file for the object hierarchy/format specification.
flat_rgb_set
: a flat array containing one HTML color string in #RRGGBB
format for each color in the palette
error_msg
: optional 3rd argument which will contain an error message when something went horribly wrong. In case of errors, both palette
and flat_rgb_set
are set to null
.
<!DOCTYPE html>
<html>
<head>
<script src="//mbostock.github.com/d3/d3.v2.js"></script>
<script src="jdataview.js"></script>
<script src="Adobe.ASE.load.js"></script>
<title>Show ASE color palettes from Adobe Photoshop / Adobe Kuler</title>
<style>
</style>
</head>
<body>
<img style="position:absolute;top:0;right:0;border:0;" width="149" height="149" src="../forkme.png" alt="Fork me on GitHub">
<div>
<label>Pick a color palette:
<select id="pallete_picker">
<option value="-" > D3 internal: category10</option>
<option value="dragon.ase" > Dragon NS</option>
<option value="HKS.Z.process.ase" > Adobe Photoshop: HKS Z (process)</option>
<option value="adobe.kuler---5.Star.ase" > Adobe Kuler: 5 Star</option>
<option value="adobe.kuler---a.mix.of.misty.blue.with.purple.ase" > Adobe Kuler: a mix of misty blue with purple</option>
<option value="adobe.kuler---A.Touch.of.Mink.ase" > Adobe Kuler: A Touch of Mink</option>
<option value="adobe.kuler---AEC.New.3.ase" > Adobe Kuler: AEC New 3</option>
<option value="adobe.kuler---Ex.Removal.Connection.ase" > Adobe Kuler: Ex Removal Connection</option>
<option value="adobe.kuler---I.want.Color.Wheels.for.my.Car.ase" > Adobe Kuler: I want Color Wheels for my Car</option>
<option value="adobe.kuler---Streetlamp.ase" > Adobe Kuler: Streetlamp</option>
<option value="adobe.kuler---The.Tough.Go.Shopping.ase" > Adobe Kuler: The Tough Go Shopping</option>
<option value="LAB.colorspace.test.ase" > (ugly) LAB colorspace test swatch set</option>
</select>
</label>
</div>
<svg>
</svg>
<script type="text/javascript" >
var h=450
,w=900
,margin=20
,color = d3.scale.category10()
,blocks = d3.range(10 * 20);
var svg = d3.select("svg")
.attr("height",h)
.attr("width",w);
svg.selectAll("rect")
.data(blocks)
.enter()
.append("rect")
.attr("x", function(d) { return (d % 20) * 44 + Math.floor((d % 20) / 5) * 4 + 3; })
.attr("y", function(d) { return Math.floor(d / 20) * 44 + 3; })
.attr("width", 40)
.attr("height", 40)
.style("stroke", "black")
.style("fill", function(d) { return color(d); });
var picker = d3.select("#pallete_picker")
.on("change", function() {
console.log("click", arguments);
var file = picker.node().value;
if (file === "-")
{
color = d3.scale.category10();
svg.selectAll("rect")
.style("fill", function(d) { return color(d); });
}
else
{
adobe.ase.load(file, function(palette, flat_rgb_set, error_msg) {
console.log("ASE: ", palette, flat_rgb_set, error_msg);
color = d3.scale.ordinal().range(flat_rgb_set);
svg.selectAll("rect")
.style("fill", function(d) { return color(d); });
});
}
});
</script>
</body>
</html>
/*
Copyright (c) 2012, Ger Hobbelt (ger@hobbelt.com)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* The name Ger Hobbelt may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL MICHAEL BOSTOCK BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
(function(){
/*
This software assumes that jDataview.js has been loaded alongside.
jDataview.js is (c) Copyright Christopher Chedeau and is available at
https://github.com/vjeux/jDataView
and
http://blog.vjeux.com/2011/javascript/jdataview-read-binary-file.html
Info sources consulted while preparing/building this software:
Adobe ASE (Color Swatches) format:
http://www.selapa.net/swatches/colors/fileformats.php
http://iamacamera.org/default.aspx?id=109
CMYK / LAB to RGB:
http://www.easyrgb.com/index.php?X=MATH&H=12#text12
http://www.codeproject.com/Articles/4488/XCmyk-CMYK-to-RGB-Calculator-with-source-code
Loading binary files in JavaScript:
https://developer.mozilla.org/en/using_xmlhttprequest#Receiving_binary_data
http://blog.vjeux.com/2011/javascript/jdataview-read-binary-file.html
http://blog.vjeux.com/2010/javascript/javascript-binary-reader.html
http://codereview.stackexchange.com/questions/3569/pack-and-unpack-bytes-to-strings
http://stackoverflow.com/questions/1240408/reading-bytes-from-a-javascript-string
*/
adobe =
{
ase: {version: "0.0.1"} // semver
};
adobe.ase.load = function(url, callback)
{
d3.xhr(url, 'text/plain; charset=x-user-defined', function(req)
{
if (req)
{
console.log(req);
var version_major, version_minor, count, i, view, palette = {}, flattened = [];
try
{
view = new jDataView(req.response, 0, undefined, false); // big-endian format
}
catch(e)
{
view = null;
}
if (!view ||
"ASEF" !== view.getString(4) ||
(version_major = view.getInt16()) < 1 ||
(version_minor = view.getInt16()) < 0 ||
(count = view.getInt32()) < 1)
{
callback(null, null, "illegal file format, not a ASE color palette file");
}
function rgb2str(rgb)
{
var r, g, b;
r = rgb[0].toString(16);
if (r.length < 2)
r = "0" + r;
g = rgb[1].toString(16);
if (g.length < 2)
g = "0" + g;
b = rgb[2].toString(16);
if (b.length < 2)
b = "0" + b;
return "#" + r + g + b;
}
function parse_utf16_Cstring(view)
{
var slen = view.getUint16();
var c, name = "", i = slen;
// ignore NUL sentinel at the end of the string
while (--i > 0) {
c = view.getUint16();
name += String.fromCharCode(c);
}
view.getUint16();
return name;
}
function parse_block(view, palette)
{
// parse block:
var i, id, len, slen, model, type, c, m, k, l, a, r, g, b, x, y, z, name, p;
while (--count >= 0)
{
id = view.getUint16();
switch (id)
{
default:
// illegal block; damaged ASE file?
callback(null, null, "unknown block type " + id.toString(16) + ": broken ASE color palette file");
return -1;
case 0xc001: // group start
len = view.getUint32();
name = parse_utf16_Cstring(view);
if (!palette.groups)
palette.groups = [];
palette.groups.push(p = {
name: name
});
if (parse_block(view, p))
return -1;
continue;
case 0xc002: // group end
view.getUint32(); // skip 0 length
return 0;
case 0x0001: // color
len = view.getUint32();
name = parse_utf16_Cstring(view);
model = view.getString(4);
if (!palette.colors)
palette.colors = [];
palette.colors.push(p = {
name: name,
model: model
});
switch (model)
{
case "CMYK":
c = view.getFloat32();
m = view.getFloat32();
y = view.getFloat32();
k = view.getFloat32();
p.cmyk = [c, m, y, k];
if (k >= 1)
{
//Black
r = g = b = 0;
}
else
{
//CMYK and CMY values from 0 to 1
c = c * (1 - k) + k;
m = m * (1 - k) + k;
y = y * (1 - k) + k;
//CMY values from 0 to 1
//RGB results from 0 to 255
r = (1 - c);
g = (1 - m);
b = (1 - y);
r = Math.min(255, Math.max(0, Math.round(r * 255)));
g = Math.min(255, Math.max(0, Math.round(g * 255)));
b = Math.min(255, Math.max(0, Math.round(b * 255)));
}
flattened.push(rgb2str(p.html_rgb = [r, g, b]));
break;
case "RGB ":
r = view.getFloat32();
g = view.getFloat32();
b = view.getFloat32();
p.rgb = [r, g, b]; // also keep the raw RGB
r = Math.min(255, Math.max(0, Math.round(r * 255)));
g = Math.min(255, Math.max(0, Math.round(g * 255)));
b = Math.min(255, Math.max(0, Math.round(b * 255)));
flattened.push(rgb2str(p.html_rgb = [r, g, b]));
break;
case "LAB ":
l = view.getFloat32();
a = view.getFloat32();
b = view.getFloat32();
p.lab = [l, a, b];
// Photoshop CS5.5 saves these as perunage (0..1), value, value. So we need to adjust L before commencing:
l *= 100;
// CIE-L*ab -> XYZ
y = (l + 16) / 116;
x = a / 500 + y;
z = y - b / 200;
if (Math.pow(y, 3) > 0.008856)
y = Math.pow(y, 3);
else
y = (y - 16 / 116) / 7.787;
if (Math.pow(x, 3) > 0.008856)
x = Math.pow(x, 3);
else
x = (x - 16 / 116) / 7.787;
if (Math.pow(z, 3) > 0.008856)
z = Math.pow(z, 3);
else
z = (z - 16 / 116) / 7.787;
x = 95.047 * x; //ref_X = 95.047 Observer= 2°, Illuminant= D65
y = 100.000 * y; //ref_Y = 100.000
z = 108.883 * z; //ref_Z = 108.883
// XYZ -> RGB
x = x / 100; //X from 0 to 95.047 (Observer = 2°, Illuminant = D65)
y = y / 100; //Y from 0 to 100.000
z = z / 100; //Z from 0 to 108.883
r = x * 3.2406 + y * -1.5372 + z * -0.4986;
g = x * -0.9689 + y * 1.8758 + z * 0.0415;
b = x * 0.0557 + y * -0.2040 + z * 1.0570;
if (r > 0.0031308)
r = 1.055 * Math.pow(r, 1 / 2.4) - 0.055;
else
r = 12.92 * r;
if (g > 0.0031308)
g = 1.055 * Math.pow(g, 1 / 2.4) - 0.055;
else
g = 12.92 * g;
if (b > 0.0031308)
b = 1.055 * Math.pow(b, 1 / 2.4) - 0.055;
else
b = 12.92 * b;
r = Math.min(255, Math.max(0, Math.round(r * 255)));
g = Math.min(255, Math.max(0, Math.round(g * 255)));
b = Math.min(255, Math.max(0, Math.round(b * 255)));
flattened.push(rgb2str(p.html_rgb = [r, g, b]));
break;
case "GRAY":
g = view.getFloat32();
p.gray = g;
g = Math.min(255, Math.max(0, Math.round(g * 255)));
flattened.push(rgb2str(p.html_rgb = [g, g, g]));
break;
default:
callback(null, null, "unknown color model " + model + ": broken ASE color palette file");
return -1;
}
type = view.getUint16();
p.color_type = type; // (0 => Global, 1 => Spot, 2 => Normal)
continue;
}
}
return 0;
}
if (!parse_block(view, palette))
callback(palette, flattened);
} else {
callback(null, null, "I/O error: could not load ASE palette file");
}
});
};
})();
ASEF V * H K S 0 1 Z CMYK =M@ ?3: * H K S 0 2 Z CMYK<�� ?� * H K S 0 3 Z CMYK ?� * H K S 0 4 Z CMYK >� ?� * H K S 0 5 Z CMYK >L� ?� * H K S 0 6 Z CMYK >�� ?� * H K S 0 7 Z CMYK >�t ?� * H K S 0 8 Z CMYK ?� ?� * H K S 1 0 Z CMYK ?&l ?� * H K S 1 1 Z CMYK >�t >�t * H K S 1 2 Z CMYK ?@ ?fl * H K S 1 3 Z CMYK ?fl ?� * H K S 1 4 Z CMYK ?Y� ?s4 * H K S 1 5 Z CMYK ?s4 ?3: * H K S 1 6 Z CMYK=� ?� ?L� * H K S 1 7 Z CMYK=M@ ?� >�4 =M@ * H K S 1 8 Z CMYK>� ?� ?� * H K S 2 1 Z CMYK ?� >L� * H K S 2 2 Z CMYK ?fl ?L� * H K S 2 3 Z CMYK ?s4 ?&l * H K S 2 4 Z CMYK ?� >� * H K S 2 5 Z CMYK ?� >L� * H K S 2 6 Z CMYK=M@ ?� =M@ * H K S 2 7 Z CMYK=M@ ?� * H K S 2 8 Z CMYK=� ?s4 >� * H K S 2 9 Z CMYK>�4 ?s4 * H K S 3 1 Z CMYK>� ?s4 * H K S 3 2 Z CMYK=� ?� * H K S 3 3 Z CMYK>�� ?s4 * H K S 3 4 Z CMYK? ?� * H K S 3 5 Z CMYK?s4 ?Y� * H K S 3 6 Z CMYK?3: ?s4 * H K S 3 7 Z CMYK?3: ?� * H K S 3 8 Z CMYK?� ?&l =� >�4 * H K S 3 9 Z CMYK?� >�4 * H K S 4 0 Z CMYK?� =� =M@ * H K S 4 1 Z CMYK?s4 ?� =M@ >�� * H K S 4 2 Z CMYK?� ?&l * H K S 4 3 Z CMYK?� ?� * H K S 4 4 Z CMYK?� >�� * H K S 4 5 Z CMYK?� ?fl * H K S 4 6 Z CMYK?s4 =� >� * H K S 4 7 Z CMYK?� =� * H K S 4 8 Z CMYK?� =M@ * H K S 4 9 Z CMYK?� >� =M@ * H K S 5 0 Z CMYK?3: =M@ * H K S 5 1 Z CMYK?� >� * H K S 5 2 Z CMYK?� >�t * H K S 5 3 Z CMYK?@ >�� * H K S 5 4 Z CMYK?Y� ?3: * H K S 5 5 Z CMYK?� ?&l * H K S 5 6 Z CMYK?� =� ?� >L� * H K S 5 7 Z CMYK?� ?Y� * H K S 5 8 Z CMYK?fl =M@ ?� >�� * H K S 5 9 Z CMYK?� >�4 >� * H K S 6 0 Z CMYK?&l ?� * H K S 6 1 Z CMYK?3: ?s4 >�� * H K S 6 2 Z CMYK>� >� ?Y� >L� * H K S 6 3 Z CMYK?� ?Y� =� * H K S 6 4 Z CMYK?@ ?s4 * H K S 6 5 Z CMYK?� ?@ * H K S 6 6 Z CMYK>�� ?L� * H K S 6 7 Z CMYK>�� ?Y� * H K S 6 8 Z CMYK=� ?fl * H K S 6 9 Z CMYK=M@ ?� * H K S 7 1 Z CMYK >� ?s4 * H K S 7 2 Z CMYK >�4 ?� * H K S 7 3 Z CMYK >�4 ?� >� * H K S 7 4 Z CMYK >�� ?� >�4 * H K S 7 5 Z CMYK>� >� ?� >� * H K S 7 6 Z CMYK ? ?� ?3: * H K S 7 7 Z CMYK ?� ?� ?� * H K S 7 8 Z CMYK ?&l ?s4 >�4 * H K S 8 1 Z CMYK ?3: ?� =� * H K S 8 2 Z CMYK=� ?@ ?fl =M@ * H K S 8 3 Z CMYK>� ?s4 ?� >� * H K S 8 4 Z CMYK>�4 ?fl ?fl >� * H K S 8 8 Z CMYK>�� ?� * H K S 8 9 Z CMYK =� >L� >�� * H K S 9 0 Z CMYK >�� >�� ?s4 * H K S 9 1 Z CMYK>� =M@ >L� * H K S 9 2 Z CMYK>L� =M@ ? * H K S 9 3 Z CMYK>�4 =� ?3: * H K S 9 5 Z CMYK=� =M@ =� >�� * H K S 9 6 Z CMYK=M@ =M@ ? * H K S 9 7 Z CMYK>� >L� ?s4
ASEF * H K S 9 7 Z CMYK>� >L� ?s4 L A B 1 LAB ?a� �<