How do I calculate a four colour gradient? - graphics

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)

Related

Why are the colors in the 1931 CIE xyY chromaticity diagram white?

When we look at the 1931 CIE chromaticity diagram, represented within the x y plane of xyY space, it renders white colors (or close to white) at points of luminance like the D65 point highlighted here with E.
But why is this the case? The point for D65 is supposed to be represented at x = 0.33, y = 0.33. Given the formula Y = 1 - x - y, wouldn't that mean Y is 0.34?
The sRGB correlate or xyY at 0.33,0.33,0.34 is 158.4182, 155.5676, 176.8565 according to every converter I found. This is a light brown and not the near-white seen in every 1931 chromaticity diagram.
It seems like I need to scale the Y to get the proper luminance value for every channel.
Using the Y = 1 - x - y formula, my diagram looks like this, a muted diagram:
What don't I understand?
Edit
Setting Y = 1 and the diagram looks like the below, better.
Edit
Now looks like the below.
There is some imprecision on the interpretation of chromacity diagrams.
CIE xyY is a 3D figures. Often we see only a projections (often not a intersecting plane, just a projection).
One common projection is the "additive" xy chromacity diagram. You may notice it because it has yellow at border, and the white somewhere near the center. In such projection you show the maximum Y given a chromacity x,y.
Common is also the "subtractive" diagram, like your second one. No yellow, no white. This diagram has just the subtractive mix of the primaries, so the brighter colour are the primaries, and you get darken between them.
Note: usually the chromacity diagram are also extended also out of gamut, so the primaries are no more the real primaries, and white could not be white, and the yellow could be cut off, as your diagrams. You may try at first just the triangle between primaries, then expand. It is easier to debug.
The white will be just on top of 3D figure. In the first case, you take the outer surface of gamut, so you get the white. In the second case, you get a plane inside the figure, so you will never get white. But it is still a xy chromacity diagram.
On your case, I think you clipped the colour values (Note 1), which it is wrong: by clipping you will not get the correct chromacities (by clipping, one remove a certain value of a colour, so the ratio between channel is not maintained). One should use float or larger numbers for calculations, before to normalize (channel values in range 0 to 255). [Normalize (in this case): keep chromacity, but adapt Y so that final colour is in gamut]. In practice: you get the maximum value between R, G, B, and you multiply every channels by 255/max(R,G,B).
Note: this is not fully correct/precise. The above normalization should be done in linear space (light mix linearly), and only after normalization, the gamma funtion should be applied. On the other hand, on above figures, we do not have the correct colour for every point x,y. We can do it correctly only on a triangle (of gamut). By expanding the available colour on screen to full xz chromacity, we create errors/imprecisions. So normalization before or after gamma correction is not more so relevant (and it just change slightly the colours).
Note 1: From comment: this (clipping) it is not true, OTOH the very tiny part of blue (dark blue), and too much magenta and cyan, make me thinking about some numerical prolem)
The white point of CIE 1931 is not in x=1/3, y=1/3, and white color is not x=1/3, y=1/3, Y = 1/3.
According to Wikipedia:
The CIE 1931 color space chromaticity coordinates of D65 are
x=0.31271
y=0.32902
Since D65 represents white light, its co-ordinates are also a white point, corresponding to a correlated color temperature of 6504 K. Rec. 709, used in HDTV systems, truncates the CIE 1931 coordinates to x=0.3127, y=0.329.
The meaning of x=1/3, y=1/3 is different:
Light with a flat power spectrum in terms of wavelength (equal power in every 1 nm interval) corresponds to the point (x, y) = (1/3, 1/3).
Important: D65 is not a "flat power spectrum".
Computer systems (PCs) uses sRGB color format.
In sRGB the color components are after gamma (in contrast to CIE 1931 which applies linear curve).
In xyY color space, x,y are the chromaticity and Y is the luminance.
x=0.31271, y=0.32902 is the chromaticity without luminance and applies gray chromaticity.
For white color use Y = 1
Rec. 709, used in HDTV systems, truncates the CIE 1931 coordinates to x=0.3127, y=0.329
Lets compute sRGB of x=0.3127, y=0.329, Y = 1:
X = (Y/y)*x = 0.95046
Y = 1
Z = Y/y*(1-x-y) = 1.0891
Rlinear 3.240600 -1.537200 -0.498600 X 0.99984
Glinear = -0.968900 1.875800 0.041500 * Y = 1.00010
Blinear 0.055700 -0.204000 1.057000 Z 1.00007
Assume result is 1, 1, 1.
Last stage is applying gamma for converting "Linear sRGB" to sRGB.
Since all values are 1, the result is sRGB = 1, 1, 1.
We can repeat the computation for Y = 0.2, and the result is Linear sRGB = 0.2, 0.2, 0.2.
Apply gamma:
gamma(u) = 1.055*u^(1/2.4) - 0.055 for u > 0.0031308
1.055*0.2^(1/2.4) - 0.055 = 0.48453
So sRGB = 0.48453, 0.48453, 0.48453.
For converting to the standard range of [0, 255] (one byte per color channel), we need to scale by 255 and round the result: RGB888 = 124, 124, 124.

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

How are CIE xyY Luminance Values for Color Primaries Determined?

In the sRGB color space, the luminance values for the red, green, and blue primaries are specified as 0.21216, 0.7152, and 0.0722, respectively. The white point is defined to have luminance 1. In other words, the sRGB values <1,0,0>, <0,1,0>, <0,0,1>, and <1,1,1> map to xyY values <0.64, 0.33, 21.216>, <0.3, 0.6, 71.52>, <0.15, 0.06, 7.217>, and <0.31273, 0.32902, 100> (with Y scaled by 100 by convention).
How are the luminance values for the primaries determined? Are they purely a function of the xy primaries, or a combination of the primaries and the illuminant (e.g. D65)? If so, what is the relationship? More generally, how can I determine the luminance values for an arbitrary set of primaries?
Finding the RGB-to-XYZ matrix is determined by the chromaticities (xy values) of the red, green, and blue primaries and by the chromaticies of the white point. The white point, in turn, is determined, at least in part, by the light source and by the color matching functions in use (for example, the D65 illuminant and the CIE 1931 standard observer, respectively).
The conversion is explained in further detail on Bruce Lindbloom's Web site:
http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
After generating the matrix, the luminances (Y values) of the three primaries are given in the second row of that matrix (see the pregenerated matrices further down on that page). Note that the formula given there takes the xy form of the primaries and the XYZ form of the white point, which can be converted from xy form by [x/y, 1, (1-(y+x))/y].

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.

Generate next color in spectrum

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.

Resources