Generate next color in spectrum - colors

everyone. How would I generate the next color in the color spectrum? Like, a function that takes a red value, a green value, and a blue value for input and output. I could input solid red (RGB 255, 0, 0) and it would output an orangish-red.
EDIT: Some more background info: I'm assuming the H, S, and V values have numeric ranges from 0-255. The C program I'm writing increments the hue value if it is less than 256, resets it to 0 if it's not, converts the HSV to RGB, displays the color on the screen, and loops. I've tried a couple HSV-to-RGB functions, but they're not working.

Instead of the RGB domain for colors, you should work with HSV values. This way, you can modify the H value to move around the spectrum.

Do you have to work with RGB values? If you don't, then use HSL as #sukru suggested, otherwise, try to convert it into HSL by following the instructions here, then increment the H value by 1/12, and convert to RGB.

Related

Color gradient based on point's Z value

I am trying to decide on the color of various points in a 3D coordinate system. The way I want to do it is to decide the RGB Values for a particular 3D Point based on the Z Cordinate value of that point, For the point having maximum Z value to have only Red color and for a point having minimum Z value to have only blue color. But I am not sure how to transition the values for R, G and B in between for all the points.
Thanks
RGB color scheme is not very useful for that. Throw a look at HSV.
You have to scale your Z to (0, 360) and set Saturation and Value to 1 and then convert this into RGB
Or if you have something like the matlab jet color map in mind:
Grayscale to Red-Green-Blue (MATLAB Jet) color scale

Getting alpha from normal and darken color

Given
darkColor = darken(normalColor, alpha)
darkColor and normalColor are known, alpha is unknown.
How can I calculate alpha?
How should I interpolate alpha if multiple color tuples (normalColor, darkColor) are presented?
As per Less docs, the below is how darken() function is defined:
Decrease the lightness of a color in the HSL color space by an absolute amount.
So given the normal color and its darkened version, the logic to find the percentage is to find out the lightness of both the normal color, the dark color and then subtract the latter from the former. Less has a built-in function to calculate the lightness() of a given color also and so it can be used directly.
#normalColor: #AAAAAA;
#darkColor: #6A6A6A; /* this is darken(#normalColor, 25%) */
#dummy{
percentage: lightness(#normalColor) - lightness(#darkColor);
}
Notes:
Calculation is lightness of normal color - lightness of dark color as darken decreases lightness.
The output is an approximate value and not accurate. For example, in the above case the output is 25.09803922% and not 25%. We cannot round down the output value also because deviation can be positive or negative. For example, if the dark color is #919191 (= darken(#normalColor, 10%)), the calculated output is 9.80392157%.
This method works only when the dark color is actually a darkened version of the normal color. That is, the hue and saturation of the two colors should be the same as the darken function modifies only the lightness.

Calculating similarities in colors in RGB

I'm writing a program that hides text in the image (Steganography).
I needed an algorithm that can calculate a pixel color to be similar to others in the vicinity. For example, this image.
The central pixel: the closest color to the neighborhood. But I need to get the same R G B value. For example: 200, 200, 200.

Create a false color palette and associate pixel values with it

I have raw pixel data (640x480 pixels) from an infrared camera which stand for a specific measured temperature. These pixel values have a 16 bit range from 0 to 65535.
I can display the pixel values as 8 bit greyscale, which works very well.
But now I want to display those pixels by using a false color palette.
I noticed 2 challenges here:
1.) Creating a false color palette. This means not just a simple RGB or HSV palette...I am thinking of a transition from black to yellow, to orange, to red and finally to purple
2.) Associating the pixel values to a color on my palette (e.g. 0 = black, 65535 = purple, but 31521 = ???)
Do you have an idea how I should approach this problem? I use Qt4 and Python (PyQt) but also I would be very happy if you just share the way for a solution.
One simple way would be to define colors at certain points in your range - as in your example, 0 is black, 65535 is purple, maybe 10000 is red, whatever you want to do. Set up a table with those key rgb values, and then simply interpolate between the rgb values of the key values above and below your input value to find the rgb color for any given value.
eg. if you're looking up the color for the value 1000, and your table has
value=0, color=(0,0,0)
value=5000, color=(255, 0, 255)
Then you would interpolate between these values to get the color (51, 0, 51)
The easiest method is as follows:
Cast your unsigned short to a QRgb type, and use that in the QColor constructor.
unsigned short my_temp=...;
QColor my_clr((QRgb)my_temp);
This will make your values the colors between black and cyan.

How do I calculate a four colour gradient?

If I have four colours (A, B, C & D) on four corners of a square and I want to fill that square with a gradient that blends nicely between the four colours how would I calculate the colour of the point E?
The closer E is to any of the other points, the strong that colour should affect the result.
Any idea how to do that? Speed and simplicity is preferred to accuracy.
colours http://rabien.com/image/colours.png
The best solution when a gradient is required between two colors, is to use the HSV representation (Hue Saturation Value).
If you have the HSV values for your two colors, you just make linear interpolation for H, S and V, and you have nice colors (interpolation in RGB space always lead to "bad" results).
You also find here the formulae to go from RGB to HSV and from HSV to RGB, respectively.
Now, for your problem with the four corner, you can make a linear combination of the four H/S/V values, weighted by the distance from E to that four points A,B,C and D.
EDIT: same method than tekBlues, but in HSV space (it is quite easy to test it in RGB and in HSV spaces. And you will see the differences. In HSV, you just turn around the chromatic cylinder, and this is why it gives nice result)
EDIT2: if you prefer "speed and simplicity", you may use a L1-norm, instead of a L2-norm (euclidian norm)
So, if a is the size of your square and the coordinate of your points are A(0,0), B(0,a), C(a,0), D(a,a), then the Hue of a point E(x,y) can be computed with:
Hue(E) = ( Hue(B)*y/a + Hue(A)*(1-y/a) ) * (x/a) + ( Hue(D)*y/a + Hue(C)*(1-y/a) ) * (1-x/a)
where Hue(A) is the Hue of point A, Hue(B) the Hue of B, etc...
You apply the same formulae for the Saturation and Value.
Once you have the Hue/Saturation/Value for your point E, you can transform it in RGB space.
Check out this site, which gives a visual demo of #ThibThib's comment that "gradients in HSV will be more satifying":
http://www.perbang.dk/rgbgradient/
It is a gradient creator that will create and show BOTH an RGB gradient and an HSV gradient.
If you try 9 steps from FFAAAA to AAFFAA (light red to green), you’ll get a nice transition through light yellow, and the HSV and RGB ones look similar.
But try 9 steps from FF0000 to 00FF00 (bold red to green), and you’ll see the RGB one transition through a yucky greenish brown. The HSV gradient, however, transitions through bold yellow.
Determine the distance of point E to each point A,B,C,D
The color for point E will be the combination of Red / Green / Blue. Calculate each color axis as the average of the same color axis for A,B,C,D, ponderating by distance.
distance_a = sqrt((xa-xe)^2+(ya-ye)^2)
distance_b = ....
sum_distances = distance_a + distance_b ...
red = (red_adistance_a + red_bdistance_b ... ) / sum_distances
color_E = ColorFromARgb(red,green,blue)

Resources