In my previous article, I described how to read an archaic CorelDRAW! 3.0 palette file. This continuation covers how to write files in this format.
Just like reading the file, writing is also a simple enough process.
The first task is to work out the longest swatch name, with a minimum length of 32. This will allow us to write the text aligned so it's still easily readable or writable with a standard text editor.
After that, it is a matter of looping our colours and for each
colour write a line describing it to a
After writing each line, I reset the
Length property of the
StringBuilder instance to zero so I can reuse the same
instance, avoiding new object allocations.
Newer versions of .NET include a
Clearmethod, but it does exactly the same thing under the hood.
Although I encountered the
SUB character in my previous
digging into these palettes, given it was the exception rather
than the norm I've chosen not to write an end of file character
in this sample application.
Remembering that the names need to be quoted, we write
characters before and after the actual swatch name. Then, to
ensure that subsequent values are aligned, we write some padding
spaces. We could do this by creating a new string with a space
character repeated the requisite count, but as usual I'm going
try and sidestep the allocation.
Once again, this article is overdue and so I haven't done any profiling. When I get back on track I will probably revisit these potentially micro-optimisation routines I keep creating and see if there's a concrete reason for doing them or if I'm just writing code for no reason.
As with writing the swatch name, I don't want to create a string version of the source value, and then another string for the padding as these are allocations that aren't required.
Instead, with a range of
0-100 that means I'm only dealing
values that will be 1, 2 or 3 characters long so I decided to
create a helper to write the padded value without creating
interim strings (the
100 below doesn't count as it is
In the first version of this program, I was left aligning the strings, when they should actually be right aligned. The alignment doesn't actually seem to matter - CorelDRAW! 3 opened a left aligned palette quite happily, but better to be consistent.
Although I covered RGB to CMYK conversion in a previous article, the version used in this sample is slightly different to account for the fact that CorelDRAW 3 palettes use the range 0-100.
As previously noted, converting from RGB to CMYK and then back to RGB can introduce subtle errors which means the original RGB colour may not match the converted version.
The sample application from part 1 has been updated to include the ability to write CorelDRAW! 3.0 palettes in addition to reading them.
- 2018-08-05 - First published
- 2020-11-22 - Updated formatting
Like what you're reading? Perhaps you like to buy us a coffee?
- Working with CorelDRAW Palettes part 2, writing .pal files
- Working with CorelDRAW Palettes part 1, reading .pal files