Mismatch of values in the color triangle - colors

Maxwell Color Triangle with Matplotlib
If you maximize the brightness based on formula RGB(r,g,b) -> 1 / max(r,g,b) * RGB(r, g, b), moving from any vertex along the side of the triangle, the value of 128 will not coincide with its quarter (0.25), but will be slightly shifted to the center (0.35). Any ideas how to fix it?
1 / max(r,g,b) * RGB(r, g, b)
Result

Related

Is there anyway to convert a float to a brightness value for a pixel?

You can cycle through a bitmap image and get the brightness of each pixel as a float value between 0 and 1.
But is there anyway of doing the reverse?
As in you take a brightness value, and you convert that to a brightness of a pixel. Obviously you wouldn't have any colour but that doesn't matter.
All I want to do is take a float and get a pixel with a brightness corresponding to that float value. So 0.0 would become black, 1.0 would become white and 0.5 would be in-between.
Any thoughts?
Never mind I figured it out.
I just had to multiply the float value against 255 and then use the rounded integer in the RGB fields:
int n = (int)(255 * brightness)
Color c = Color.FromArgb(n, n, n)

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 to find the tangent to any pixel?

I want to find tangent at each pixel in image.
NOTE: image is having white background and shape border color is block.
What i did is,
Algo
While(true)
take pixel
if pixel color is black
make 3 X 3 matrix => fill the matrix by surrounding pixel color
...means assume white =0 and black=1 then keeping selected pixel
at center for 3 X 3 matrix and finding all other value;
----------------------------here i want to find tangent line to selected pixel;
end if
Move to next pixel.
End while
Please help Exams on head .
What you're looking for is probably a Sobel Operator. It's implemented as a convolution of the neighborhood around a pixel with the matrix:
-1 0 1
-2 0 2
-1 0 1
and again with:
-1 -2 -1
0 0 0
1 2 1
Call the results of the 2 convolutions x and y, respectively. Once you have them, you can get the magnitude of the gradient by taking the square root of the sum of the squares:
mag = sqrt(x * x + y * y);
and the direction of the gradient (which should be tangent to the pixel you're examining) by taking the arctangent of y over x:
tangent = atan2(y / x)

how to calculate gradient color by percent

I have a float score, which is 0 to 1
I need translate this score to color,
0 is green
1 is red
0.5 should at the middle of green to red gradient color
and so on
how to write this? I have no idea.
let gradient colour parameter be t, 0.0 =< t =< 1.0
colour = RGB(255 * t, 255 * (1 - t), 0)
Multiply the float by 255 to get your green value, and multiply (1-float) by 255 to get your red value. If you need to output a css color code, use rgb(x,y,z).

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