Print

Print


On Mar 29, 2015, at 9:45 PM, Ernesto Carrella <[log in to unmask]> wrote:

> Wanted to build a double gradient color map (really just a pair of simpleColorMap delegated) but I am stumped by the ColorMap interface.

See end of message: I threw together a similar thing a while back (not tested), which you can use and modify and give me feedback on if you like.


> 1) If everything has to return a color no matter what, why not just have one getColor()? Why do we need a getRGB() + getAlpha() too?

Efficiency.  getColor does allocation and a fair bit of overhead.  For example, FastValuePortrayal2D uses getRGB, then pokes the int values into the raster array of an image, which is then stretched and displayed.  However you don't have to implement things that way; as described in the class docs, you could obviously just write getRGB and getAlpha in terms of getColor.  I think what you're really asking is: why isn't there a concrete abstract implementation of ColorMap, like this:

public abstract class BasicColorMap
    {
    public abstract Color getColor(double level);
    public int getRGB(double level) { return getColor(level).getRGB(); }
    public int getAlpha(double level) { return getColor(level).getAlpha(); }
    public boolean validLevel(double level);
    public double defaultValue();
   }

And the answer is: that's a very good question.


> 2) Why do we need validLevel() and defaultValue()? can't they both be part of getColor()?

I think you're thinking that validLevel and defaultValue are *called* by getColor. But they're not.   There are situations where the external code which is calling ColorMap needs to know if the value it's displaying has been mapped to a color by the user, or if some random default is going to get thrown in there.  In fact, the code at the end of this message is a example.

> 3) In what order are the methods called? does validLevel()+defaultValue() override getColor()? Should getColor() delegate to getRGB() or viceversa?

The validLevel and defaultValue functions are independent of the others.  getColor CAN delegate to getRGB if you like, though it's perhaps more common to do it the other way around.  That is, instead of implementing BasicColorMap like I did above, we could have instead done:

public abstract class BasicColorMap
    {
    public Color getColor(double level) { return new Color(getRGB(level), true); }
    public abstract int getRGB(double level);
    public int getAlpha(double level) { return getAlpha(level) >>> 24 ; }
    public boolean validLevel(double level);
    public double defaultValue();
   }

So anyway, a while back someone else asked about a multi-gamut colormap, so I whipped up the following below.  Not sure if it works, but try it out and let me know what you think.