0

Converting a value to a colour is well known, I do understand the following two approaches (very well described in changing rgb color values to represent a value)

  1. Value as shades of grey
  2. Value as brightness of a base colour (e.g. brightness of blue)

But what is the best algorithm when I want to use the full colour range ("all colours"). When I use "greys" with 8bit RGB values, I actually do have a representation of 256 shades (white to black). But if I use the whole range, I could use more shades. Something like this. Also this would be easier to recognize.

Basically I need the algorithm in Javascript, but I guess all code such as C#, Java, pseudo code would do as well. The legend at the bottom shows the encoding, and I am looking for the algorithm for this.

So having a range of values(e.g. 1-1000), I could represent 1 as white and 1000 as black, but I could also represent 1 as yellow and 1000 as blue. But is there a standard algorithm for this? Looking at the example here, it is shown that they use colour intervals. I do not only want to use greys or change the brightness, but use all colours.

This is a visual demonstration (Flash required). Given values a represented in a color scheme, my goal is to calculate the colours.

I do have a linear colour range, e.g. from 1-30000

-- Update --

Here I found that here is something called a LabSpace:

Lab space is a way of representing colours where points that are close to each other are those that look similar to each other to humans.

So what I would need is an algorithm to represent the linear values in this lab space.

Community
  • 1
  • 1
Horst Walter
  • 13,663
  • 32
  • 126
  • 228
  • I do not get the downvote. I do understand you think the tagging is wrong, OK, point taken, but is the question useless, badly explained or somehow stupid? The base question is event tagged with Java only, I did tag it this way cause C#, Java and Javascript are similar in syntax and I do need the algorithm in JS – Horst Walter Aug 24 '11 at 21:19
  • I think the downvote is because we can't understand what you're asking. What do you want to use the colors for? What do you mean by "the whole range?" Right now your question is too vague to be meaningfully answered. – templatetypedef Aug 24 '11 at 21:21
  • It is misleading. As the tag has been removed, I have removed my down-vote. – Hovercraft Full Of Eels Aug 24 '11 at 21:21
  • Nope, the downvote was because it has "nothing to do with Java", that is what the comment says. I will update the question to improve it. – Horst Walter Aug 24 '11 at 21:23

4 Answers4

1

It depends on the purposes of your datasets.
For example, you can assign a color to each range of values (0-100 - red, 100-200 - green, 200-300 - blue) by changing the brightness within the range.

umbr
  • 440
  • 2
  • 4
  • This is a good example and by this comment I might be able to further explain my use case. I do have a linear range, 1-30000. Your approach is clear, but is it intuitive that the colour from 100 (dark red) would change to light green for 101. I am looking for an algorithm / standard scheme giving me the next logical colour. – Horst Walter Aug 24 '11 at 21:48
  • For such range is will good solution by using full color palette with limited brightness range. – umbr Aug 24 '11 at 22:27
  • Thanks for your help, I appreciate your support. – Horst Walter Aug 25 '11 at 11:15
1

Horst,

The example you gave does not create gradients. Instead, they use N preset colors from an array and pick the next color as umbr points out. Something like this:

a = { "#ffffff", "#ff00ff", "#ff0000", "#888888", ... };
c = a[pos / 1000];

were pos is your value from 1 to 30,000 and c is the color you want to use. (you'd need to better define the index than pos / 1000 for this to work right in all situations.)

If you want a gradient effect, you can just use the simple math shown on the other answer you pointed out, although if you want to do that with any number of points, it has to be done with triangles. You'll have a lot of work to determine the triangles and properly define every point.

In JavaScript, it will be dog slow. (with OpenGL it would be instantaneous and you would not even have to compute the gradients, and that would be "faster than realtime.")

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
1

There are two basic ways to specify colors. One is a pre-defined list of colors (a palette) and then your color value is an index into this list. This is how old 8-bit color systems worked, and how GIF images still work. There are lists of web-safe colors, eg http://en.wikipedia.org/wiki/Web_colors, that typically fit into an 8-bit value. Often similar colors are adjacent, but sometimes not. A palette has the advantage of requiring a small amount of data per pixel, but the disadvantage that you're limited in the number of different colors that can be on the screen at the same time.

The other basic way is to specify the coordinates of a color. One way is RGB, with a separate value for each primary color. Another is Hue/Saturation/Luminance. CMYK (Cyan, Magenta, Yellow and sometimes blacK) is used for print. This is what's typically referred to as true color and when you use a phrase like "all colors" it sounds like you're looking for a solution like this. For gradients and such HSL might be a perfect fit for you. For example, a gradient from a color to grey simply reduces the saturation value. If all you want are "pure" colors, then fix the saturation and luminance values and vary the hue. Nearly all drawing systems require RGB, but the conversion from HSL to RGB is straight forward. http://en.wikipedia.org/wiki/HSL_and_HSV

If you can't spare the full 24 bits per color (8 bits per color, 32-bit color is the same but adds a transparency channel) you can use 15 or 16 bit color. It's the same thing, but instead of 8 bits per color you get 5 each (15 bit) or 5-6-5 (16 bit, green gets the extra bit because our eyes are more sensitive to shades of green). That fits into a short integer.

Adam
  • 16,808
  • 7
  • 52
  • 98
1

What you need is a transfer function.
given a float number, a transfer function can generate a color.

see this:

http://http.developer.nvidia.com/GPUGems/gpugems_ch39.html

and this:

http://graphicsrunner.blogspot.com/2009/01/volume-rendering-102-transfer-functions.html

the second article says that the isovalue is between [0,255]. But it doesn't have to be in that range.

Normally, we scale any float number to the [0,1] range, and apply transfer function to get the color value.

Bill Yan
  • 3,369
  • 4
  • 27
  • 42