Getting alpha from normal and darken color - colors

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.

Related

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].

How does ncurses' init_color function translate to traditional rbg colors?

Sorry for the oddly worded title. I'd like to know how the ncurses init_color function maps it's input to colors. Essentially, most developers are used to colors being represented by red, green, and blue on a 0 - 255 scale, but init_color takes an int on a 0 - 1000 scale.
for example:
If I wanted to get the color (75, 0, 130) in ncurses, would I call init_color(COLOR_NAME, 300, 0, 520)?
short:
(n) * 1000 / 256
which is a little different from your numbers:
293 0 508
long: That of course assumes that the terminal description is written to match ncurses' documentation. But the assumption is from X/Open Curses:
The init_color() function redefines colour number color, on terminals that support the redefinition of colours, to have the red, green, and blue intensity components specified by red, green, and blue, respectively. Calling init_color() also changes all occurrences of the specified colour on the screen to the new definition.
The color_content() function identifies the intensity components of colour number color. It stores the red, green, and blue intensity components of this colour in the addresses pointed to by red, green, and blue, respectively.
For both functions, the color argument must be in the range from 0 to and including COLORS-1. Valid intensity values range from 0 (no intensity component) up to and including 1000 (maximum intensity in that component).

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.

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.

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