Image dyeing

From TheManaWorld
Jump to: navigation, search

Simple colors and palettes

A pixel has a simple color if it is not black and if its non-zero RGB components are all equal. Or described in terms of the HSV color model: it has a hue divisible by 60, full saturation, and any volume other than zero.

There are seven simple colors:

  • Red
  • Green
  • Blue
  • Cyan
  • Magenta
  • Yellow
  • White (gray, that is)

For a given color, there are 255 different intensities (from 1 to 255). For RGB, the non-zero value is the intensity. For HSV, the volume is the intensity, scaled to 255.

A palette is a sequence of RGB colors, for example #8c4b41,da9041,ffff41. An intensity of 255 becomes color #ffff41. An intensity of 0 is left unchanged (as black, #000000). Intensities with no explicit color are linearly interpolated between the two closest value. Black is implicitly the first color of the palette. For example:

intensity color
50 #522C26
85 #8c4b41
100 #995741
170 #da9041

Palettes can have from one to 255 colors.

Pixels with complicated colors or without any dedicated palette are left unchanged by the dye process. The system can dye up to 1785 different colors.

Specifying palettes

Whenever an image (usually a png file) is specified in an xml file, a palette can be appended to its name. The image is then automatically dyed on loading. For example, if one changes an image resource from foo.png to foo.png|W:#ffff00, then all the gray pixels of test.png will be replaced by shades of yellow, as described above.

Several palettes (one per simple color) can be appended to an image. For example, green and red pixels are recolored independently for image foo.png|G:palette1;R:palette2.

Palettes can either be specified as the name of a file containing a palette (format not yet defined) or directly defined as a color sequence. A # symbol (often called pound, hash, or sharp) is prefixed to a color sequence, so that the system understands that this is a color, not another file.

Dyeing sprites

When indicating the dyeable colors in an image, some palettes can be left unspecified. For example, the resource name foo.png|G;R:palette1;Y means that green, red, and yellow, pixels of the image have to be dyed. But no palettes are specified for green and yellow pixels.

These palettes will be specified at a higher level. If the foo.png file is part of a sprite description named bar.xml. Then palette specifications can be appended to the file name, e.g. bar.xml|palette2;palette3. These additional palettes are then propagated to any image loaded by bar.xml with unspecified palettes. So the foo.png is finally recolored with the dye G:palette2;R:palette1;Y:palette3.

One-channel example

Here is a simple example taken from actual game data.

The (client)data/monsters.xml file contains the descriptions of all the monsters:

For black scorpions, the definition begins with

 <monster id="1" name="Scorpion">
   <sprite>monsters/scorpion.xml|#4d422d,826242,d8c282,ffffff</sprite>
   <sound event="hit">monsters/scorpion/scorpion-hit1.ogg</sound>
   [...] (sound events)
 </monster>
[...]
 <monster id="2" name="Red Scorpion">
   <sprite>monsters/scorpion.xml|#791d0a,cd5d27,f28d54,ffffff</sprite>
   [...] (sound events)
 </monster>
[...]
 <monster id="7" name="Black Scorpion">
   <sprite>monsters/scorpion.xml|#0d1313,435a5a,879999,ffffff</sprite>
   [...] (sound events)
 </monster>
[...]
 <monster id="82" name="Crotcher Scorpion">
   <sprite>monsters/scorpion.xml|#211b0e,5c3e20,a68d42,ffffff</sprite>
   [...] (sound events)
 </monster>
[...]
 <monster id="55" name="Angry Scorpion">
   <sprite>monsters/scorpion.xml|#800000,ffffff</sprite>
   [...] (sound events)>
 </monster>
<monster id="1009" name="Black scorpion">
    <sprite>monster-scorpion.xml|#0d1313,435a5a,879999,ffffff</sprite>
    <sound event="hit">scorpion-hit1.ogg</sound>
    ...

The (client)data/graphics/sprite/monster-scorpion.xml then describes the animation of any scorpion, whatever its color. It contains this line to make the client refer to the sprite image (monster-scorpion.png):

    <imageset name="base" src="graphics/sprites/monster-scorpion.png|W" width="48" height="45" />

The monster-scorpion.png file is a grayscale image, hence the W color specifier, so that all its pixels are tinted according to the specified palette. If it contained some non-gray pixels, these would not be recolored by the palette specified in the monsters.xml file.

Multi-channel example

Here an example for multi-channel dyeing of an equipment sprite. This example recolors the gray sections of the image to green and the red sections to a gray-blue.

head-devcap.xml:

<imageset name="base" src="graphics/sprites/head-devcap.png|W;R" width="28" height="19" />

items.xml:

<sprite>head-devcap.xml|#22ff22,ffffff;#9999ff</sprite>

Note: Multi-channel dyeing is broken in version 0.0.24 but has been fixed in later versions

Designing graphics to be dyeable

The easy way to make an existing graphic recolorable is to select the parts you want to be recolored and run it through a convert-to-greyscale filter and voilà, you have a recolorable 'W' channel. For optimum results you should then adjust the levels of the grey area so that the darkest color is RGB 127, 127, 127 and the brightest color RGB 255, 255, 255. That way you can very simply define the darkest and the brightest color of the color ramp in items.xml.

When you want to use another dye channel than 'W' you can use "color channels" afterwards and remove one or two of the red, green and blue channel completely.

Implementation remarks

Palettes have to be specified as part of the image and sprite resource names, so that they can be properly cached. If they were not, there would be collisions: All the recolored sprites would have the same color, the first one to be put in the cache for a given sprite definition. Since they are part of the resource names, they may as well be part of the filenames written in .xml files.

Ideas

The ability to specify multiple channels for each palette would be nice. It'd be useful in certain cases (like this robe) where multiple channels are getting the same palette. One way to indicate it would be to have a comma-separated list of channels before the palette string (example: W,Y:#22ff22,ffffff). — Jaxad0127 19:46, 14 July 2008 (CEST)


Test dyes without starting client

The dye engine can also be used standalone as console program. The tool can be found in the mana repository: https://github.com/mana/mana

(clone git://github.com/mana/mana.git).
cd tools/dyecmd
cmake .
make

You will find the binary at src/dyecmd. Example use:

# dyecmd <source_image> <target_image> <dye_string>
dyecmd "armor-legs-shorts.png" "armor-legs-shorts2.png" "W:#222255,6666ff",,