hello all,
I am generating some color palettes that I would like to export as .ase file to make it easier to import in Adobe softwares.
tried to use the HYPE library, but the palettes are just empty, I asked ChatGPT and it suggested using a library " java-ase ". I can not find it anywhere.
exporting as JSON file and then converting it to .ase is a tedious way for such an “easy” task.
chatGPT gave me the code but I can not find the org.json library neither.
is there any library you can recommend?
thanx
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import org.json.*;
void setup() {
// Define colors
color[] colors = {
color(255, 0, 0), // Red
color(0, 255, 0), // Green
color(0, 0, 255) // Blue
};
// Create JSON array for colors
JSONArray colorsArray = new JSONArray();
for (int i = 0; i < colors.length; i++) {
int r = (int) red(colors[I]);
int g = (int) green(colors[I]);
int b = (int) blue(colors[I]);
JSONObject colorObject = new JSONObject();
colorObject.put("name", "Color" + i);
colorObject.put("r", r);
colorObject.put("g", g);
colorObject.put("b", b);
colorsArray.put(colorObject);
}
// Convert JSON to binary ASE format
byte[] aseBytes = createASEBinary(colorsArray);
// Save ASE file
String filePath = sketchPath("colors.ase");
try (FileOutputStream fos = new FileOutputStream(filePath)) {
fos.write(aseBytes);
println("ASE file saved at: " + filePath);
} catch (IOException e) {
e.printStackTrace();
}
}
byte[] createASEBinary(JSONArray colorsArray) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
// Write ASE header
baos.write("ASEF".getBytes(StandardCharsets.US_ASCII)); // Signature
baos.write(ByteBuffer.allocate(2).putShort((short) 1).array()); // Version major
baos.write(ByteBuffer.allocate(2).putShort((short) 0).array()); // Version minor
baos.write(ByteBuffer.allocate(4).putInt(colorsArray.length()).array()); // Number of blocks
// Write color blocks
for (int i = 0; i < colorsArray.length(); i++) {
JSONObject colorObject = colorsArray.getJSONObject(i);
// Block type (Color entry)
baos.write(ByteBuffer.allocate(2).putShort((short) 1).array());
// Block length
int blockLength = 20 + 4 * 4; // Name length + 4 floats
baos.write(ByteBuffer.allocate(4).putInt(blockLength).array());
// Color name
String colorName = colorObject.getString("name");
baos.write(ByteBuffer.allocate(2).putShort((short) colorName.length()).array());
baos.write(colorName.getBytes(StandardCharsets.US_ASCII));
baos.write(0); // Null terminator
// Color model (RGB)
baos.write("RGB ".getBytes(StandardCharsets.US_ASCII));
// RGB values
baos.write(ByteBuffer.allocate(4).putFloat(colorObject.getFloat("r") / 255).array());
baos.write(ByteBuffer.allocate(4).putFloat(colorObject.getFloat("g") / 255).array());
baos.write(ByteBuffer.allocate(4).putFloat(colorObject.getFloat("b") / 255).array());
// Color type (global)
baos.write(ByteBuffer.allocate(2).putShort((short) 0).array());
}
} catch (IOException e) {
e.printStackTrace();
}
return baos.toByteArray();
}