Re: layer colorification

Posted by dscho on
URL: http://imagej.273.s1.nabble.com/layer-colorification-tp3694373p3694374.html

Hi,

On Fri, 28 Nov 2008, Peter Oslanec wrote:

> I have this picture (
> http://www.materialing.whian.net/files/upload/example.png)  with layers
> (black and white). I would like to change colour of each layer: for example
> layer 1 could be colour X, layer 2 could be X+1 and so on. But I need to
> automate this process (maybe write some macro?). I have no programming
> skills and only limited free time,so I need somebody who will be so kind and
> guide me into writing macros (if there is no other way how to do it). Thanks
> in advance.

Try this (you need to create a new macro with Plugins>New>Macro and paste
this text):

-- snip --
w = getWidth();                                 // get dimensions
h = getHeight();

previousColor = -1;                             // color the previous line had

newColor = -1;                                  // new color of current layer

for (y = 0; y < h; y++) {                       // iterate over all lines

        if (getPixel(0, y) != previousColor) {  // if the color has changed,
                previousColor = getPixel(0, y); // remember the color and
                newColor = newColor + 1;        // increment the new color
        }

        for (x = 0; x < w; x++)                 // assign new color to line
                setPixel(x, y, newColor);
}

setMinAndMax(0, newColor);                      // adjust contrast
-- snap --

Hth,
Dscho