Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate a color which sits between 2 colors?

I need to visualize a list of numbers, this list is generated every x minutes. I currently use a single line with numbers at the side. This works correctly, but while doing live analysis it becomes hard on the eyes.

After thinking a bit i came up with the idea of color coding the numbers, a large negative number is red and a large positive number is green, a normal positive number is light green, slightly bigger than normal number sits between green and light green.

To illustrate it i made a image:

enter image description here

To my question. If for example we have:

50: Color.green
0: Color.white
-50: Color.red

How can i calculate the color that represents 25?

like image 796
TinusSky Avatar asked Feb 16 '23 16:02

TinusSky


1 Answers

For strictly linear representation between red <--> white <--> green,

import java.awt.Color;

/* Define the MAXIMUM saturation of RED and GREEN shades
 * Range (0-255)
 */
final int RED_MAX = 255;
final int GREEN_MAX = 255;

/* input val varies from -MAX to MAX */

/* output valColor varies from
 * -MAX = red
 *         ^
 *         |
 *         v
 *    0 = white
 *         ^
 *         |
 *         v
 *  MAX = green
 */

/* Normalised normVal varies from -255 to 255 */
normVal = (val*255)/MAX

if(val < 0) {
    /* Make it red-ish */
    valColor = new Color( RED_MAX,
                          255 + normVal, 
                          255 + normVal );
} else if (val > 0) {
    /* Make it green-ish */
    valColor = new Color( 255 - normVal), 
                          GREEN_MAX,
                          255 - normVal );
} else {
    /* Absolute White */
    valColor = new Color( 255, 255, 255 );
}
like image 95
TheCodeArtist Avatar answered Feb 24 '23 12:02

TheCodeArtist