How is transparency actually implemented ? - graphics

Given two images A,B I want a third image C which is as if B had transparency of t=0.5 and placed on top of A.
How is C calculated in reality and how n affects it ? I am not interested in any program or pseudo code I just want to know the basic rationale.
One way I think is C is nothing but alternating pixels of A and B. What are the other ways ?

The color and optionally transparency of each pixel of A and B is combined according to the weight.
If the transparency is 0.75, then typically 25% of the color values from B and 75% of the color values from A (the underlying image) would be used.
Basically, the red, green, blue, and optionally the alpha channels are each calculated like this, and then recombined to form one resulting pixel.
Example:
A = [1 0 0] <-- red
B = [0 1 0] <-- blue
a = 0.75 (which means B is more transparent than it is opaque)
C = [
0.75 = 1 * 0.75 + 0 * 0.25 (red component)
0.25 = 0 * 0.75 + 1 * 0.25 (green component)
0.00 = 0 * 0.75 + 0 * 0.25 (blue component)
] // A B
If the images have alpha channels on their own, the calculation becomes more complex.

I am not sure if by "reality" you mean source code that hacks such effect, or how it works in nature.
In code
You can overlay images transparently by a simple linear interpolation of both input images:
Color lerp (Color lhs, Color rhs, real f) {
return (1-f)*lhs + f*rhs;
}
Image overlay_transparent (Image a, Image b, real f) {
assert (a.width == b.width);
assert (a.height == b.height);
Image output;
for_each (y : 0 .. a.height)
for_each (x : 0 .. a.width)
output(x,y) = lerp(a(x,y), b(x,y), f);
return output;
}
You could then do a 50% overlay by calling overlay_transparent (a,b, 0.5).
In reality
Transparent materials reflect a fraction of the incoming light, some fraction is absorbed,
and another fraction is transmitted through the material.
A perfect mirror reflects all of the incoming light specularly, each particles outgoing vector
solely depends on its incoming vector.
A perfectly diffuse material reflects all of the incoming light, but for each incoming vector
there are infinitely many possible outoing vectors within the hemisphere over the hit-point.
A perfectly absorbing material is black like The Void itself.
A perfectly transmitting material that does not perturb particles on their way through the material
would be invisible.
All of these perfect materials are not found in nature so far, and most real world materials are a mix of them.
Note that this is a science in itself; for deeper knowledge, you could begin studying Realistic Image Synthesis
and Path Tracing, as well as the concept of BRDF/BSDF/... .

For each pixel, the value of each component in C (Red, Green Blue) is the average of the values for the components of A and B.
If the transparency is not 50% a weighted average is used.
If you use alternating pixels the result will not be smooth. If A is all black and B is all wight alternating pixels would give a striped pattern while average RGB values on each pixel gives an even 50% gray surface.

Related

How to solve two balls select event without replacement

Q)A box contains 4 red balls, 3 green balls and 3 blue balls. Two balls are selected at
random without replacement. Let X represent the number of red balls in the sample and
Y the number of green balls in the sample.
a) Arrange the different pairs of values of (X, Y ) as the cells in a table, each cell being
filled with the probability of that pair of values occurring, i.e. provide the joint
probability distribution.
b) What does the random variable Z = 2 - X - Y represent?
c) Calculate Cov(X, Y ).
d) Calculate P(X = 1 | -2 < X - Y < 2).
I couldn't understand how to think to solve the part a) in this question and so on.
To solving this question first of all you have to create a tree with this two events. First data in this question is that we can take is , these are not independent event. so you can create tree like this,
In first part you have to create the joint table of X and Y.
there is 0,1,2 are the only possible values that each variable can get.
The critical situations are that X-1 , Y-0 and X-0 , Y-1 .Because they got two possible chances in same situation that one color ball take first and that same color ball select the second time.
So this is the table that can get according to this tree.
part b represent the blue balls in selected sample

Chord height from circle edge in 3 tangent circles

I don't know if this question makes sense, but is there a formulaic way to calculate the height of a chord from a circle's edge in one of the circles from 3 tangent circles?
I have included a diagram to provide detail. Circles C1, C2 and C3 are connected at tangents and have equal radii (in this case 1 mm, but that is only for depiction). Triangle ABC is formed by connecting the centers of these circles. Line LM meets sides AB and AC and is tangential to Circle C1. Line PQ cuts through Circle C1 and triangle ABC and is tangential to both circles C2 and C3.
Diagram for question
What is the formula for the distance (x) between lines LM and PQ?
Given that the radii are equal (say, r), the total height of these 3 circles (line RS shown in diagram, which is my objective to calculate) is 4 times the radius minus the distance (x) between lines LM and PQ. In other words,
|RS| = {(4*r) - x}
variable x needs to be converted into a formula based exclusively on radius r so as to solve this equation.
It has been a while since I revisited my high school geometry lessons, so I hope this can be solved.
Distance A-LM is equal to r (circle center - tangent)
Distance BC-PQ is equal to r
If we add these distances and subtract distance PQ-LM (x), we'll get height of equilateral triangle ABC (with edge 2*r)
r + r - x = height of ABC = 2 * r * sqrt(3)/ 2
x = r * (2 - sqrt(3))

A more natural color representation: Is it possible to convert RGBA color to a single float value?

Is it possible to represent an RGBA color to a single value that resembles the retinal stimulation? The idea is something like:
0.0 value for black (no stimulation)
1.0 for white (full stimulation)
The RGBA colors in between should be represented by values that capture the amount of stimulation they cause to the eye like:
a very light yellow should have a very high value
a very dark brown should have a low value
Any ideas on this? Is converting to grayscale the only solution?
Thanks in advance!
Assign specific bits of a single number to each part of RGBA to represent your number.
If each part is 8 bits, the first 8 bits can be assigned to R, the second 8 bits to G, the third 8 bits to B, and the final 8 bits to A.
Let's say your RGBA values are= 15,4,2,1. And each one is given 4 bits.
In binary, R is 1111, G is 0100, B is 0010, A is 0001.
In a simple concatenation, your final number would be 1111010000100001 in binary, which is 62497. To get G out of this, 62497 / 256, round it to an integer, then modulo 16. 256 is 16 to the second power because it is the 2nd position past the first from the right(R would need third power, B would need first power). 16 is 2 to the fourth power because I used 4 bits.
62497 / 256 = 244, 244 % 16 = 4.

In RGB model how many distinct hues are available?

In RGB model, each pixel is defined by 3 bytes, for R,G and B respectively. This gives a total of 224 colors, including 256 tones of grey.
It is very common to represent HSV/HSB/HSL models with floats (not bytes). Most descriptions describe hue as the "angle" in a cone, so it's sensible to treat it as a real number.
But how does this relate to the down-to-earth limit of 224 total colors..? How many distinct hues are available? More over, it seems to me that the number should depend on other parameters - saturation for instance..
Interesting reading: https://web.archive.org/web/20170622125306/http://www.dig.cs.gc.cuny.edu/manuals/Gimp2/Grokking-the-GIMP-v1.0/node52.html
In HSV, the hue is defined as
H = atan2( sqrt(3)*(G-B), 2R-G-B )
(link). In each of the six sectors (R-Y, Y-G ...), there are equally many hues. Additionally, there are six hues at the boundary between the regions. So, 6 + 6 * huesRY.
In the red-yellow sector, R > G > B, so both arguments to atan2 are positive.
count sqrt(3) * (G-B) / (2R-G-B)
=count (G-B) / (2R-G-B)
=count (G-B) / ((G-B) + (2R-2G))
since we can apply any linear transformation to the sets of [x,y] and not change the count of its ratios, x / (x+2y) == x / y
=count (G-B) / (R-G)
if we subtract the same value from all R,G,B, the ratio does not change, so assume B=0
=count G / (R-G)
=count G / R
so, there are six times as many hues as there are ratios between two positive integers that are both below 2^8 (assuming 8 bits per channel), and six more. There are as many ratios as there are pairs of coprime positive integers. The number of positive integers below n that are coprime with n is called the Euler's totient function. OEIS lists its partial sums. There are exactly 19948 pairs of coprime positive integers below 256.
6 * 19948 + 6 = 119 694
There are exactly 119 694 different hues in the HSV model that correspond to a color in the 8-bit RGB model. Note that they are not spaced evenly.
If 8 bits per channel are used in the HSV model, then there are less colors than in the RGB model with 8 bits per channel simply because some HSV triples map to the same color while every RGB triple defines a different color.
IN RGB color the hues can be calculated from (2^3*depth-2^depth/Luminance)/3= so 15 bit color has 341 distinct hues
24bit color has 21845 Distinct Hues
if there were 119000 hues the remaing colors All hues-Red hues of the red hue would be 256,X,Y around 2^16 which means there are less green and blue hues than red?
RGB<0,128,255> is the named color, Azure, Hue 210 deg.
For RGB<0,n,128> n can only be 0 or 255 to be a distinct hue,
which would be either Navy Blue or Spring Green.
RGB<0,64,128> is a shade of Azure, still 210 deg. It is not a distinct hue.
For RGB<0,n,245> again, n can only be 0 or 255,
which would be either a blue shade at 240 deg. or a cyan shade at 177.65 deg.
Floating point was introduced as a distinction between HSL and RGB (which must be byte valued integers.)
My answer to the original post is a count of distict RGB hues, very focused, and not unproven.
The order of my loops is not arbitrary; it loads an array with the 1,530 unique hues of the full spectrum.
Write it up in C++ or C#, then loop, drawing a line from the top to the bottom of the screen and you will see the full spectrum of unique hues.
I would upload a bitmap, but the denials have kept me from the points I would need for the privilege, and it would probably need to be as a fractalized jpg which ruins the idea, anyway.
'There are 1,530 Hues in RGB(256,256,256) It really is straightforward.
'This effectively reveals the "resolution" of RGB, since to be a distinct hue, one of rgb must be 255, another 0, and the third has 256 values to increment through, less duplicates at the extremes. Everything else is a tint, tone or shade. So, let's add them up in the six combinations of 0 and 255, and also count them up as I, as we go:
Dim I As Integer
Dim R, G, B As Byte
Dim Spectrum(0 to 1529) as Long
I = -1 'Incremented before each use
R = 255: B = 0 'G inc RED
For G = 0 To 255 '256
I = I + 1: Spectrum(I) = RGB(R, G, B)
Next
G = 255: B = 0 'R dec YELLOW
For R = 254 To 0 Step -1 '255
I = I + 1: Spectrum(I) = RGB(R, G, B)
Next
R = 0: G = 255 'B inc GREEN
For B = 1 To 255 '255
I = I + 1: Spectrum(I) = RGB(R, G, B)
Next
R = 0: B = 255 'G dec CYAN
For G = 254 To 0 Step -1 '255
I = I + 1: Spectrum(I) = RGB(R, G, B)
Next
G = 0: B = 255 'R inc BLUE
For R = 1 To 255 '255
I = I + 1: Spectrum(I) = RGB(R, G, B)
Next
R = 255: G = 0 'B dec MAGENTA
For B = 254 To 1 Step -1 '254
I = I + 1: Spectrum(I) = RGB(R, G, B)
Next
'I = 1,529 = 256+255+255+255+255+254 No duplicates
'Hue = I * 0.23529411764705882353°
'0° is Red at I = 0, so I=0 counts as 1 so, 1 + 1,529 = 1530

How to calculate mean and standard deviation for hue values from 0 to 360?

Suppose 5 samples of hue are taken using a simple HSV model for color, having values 355, 5, 5, 5, 5, all a hue of red and "next" to each other as far as perception is concerned. But the simple average is 75 which is far away from 0 or 360, close to a yellow-green.
What is a better way to calculate this mean and associated std?
The simple solution is to convert those angles to a set of vectors, from polar coordinates into cartesian coordinates.
Since you are working with colors, think of this as a conversion into the (a*,b*) plane. Then take the mean of those coordinates, and then revert back into polar form again. Done in matlab,
theta = [355,5,5,5,5];
x = cosd(theta); % cosine in terms of degrees
y = sind(theta); % sine with a degree argument
Now, take the mean of x and y, compute the angle, then
convert back from radians to degrees.
meanangle = atan2(mean(y),mean(x))*180/pi
meanangle =
3.0049
Of course, this solution is valid only for the mean angle. As you can see, it yields a consistent result with the mean of the angles directly, where I recognize that 355 degrees really wraps to -5 degrees.
mean([-5 5 5 5 5])
ans =
3
To compute the standard deviation, it is simplest to do it as
std([-5 5 5 5 5])
ans =
4.4721
Yes, that requires me to do the wrap explicitly.
I think the method proposed by user85109 is a good way to compute the mean, but not the standard deviation:
imagine to have three angles: 180, 180, 181
the mean would be correctly computed, as a number aproximately equal to 180
but from [180,180,-179] you would compute a high variance when in fact it is near zero
At first glance, I would compute separately the means and variances for the half positive angles , [0 to 180] and fot the negative ones [0,-180] and later I would compute the combined variance
https://www.emathzone.com/tutorials/basic-statistics/combined-variance.html
taking into account that the global mean and the difference between it and the local means has to be computed in both directions: clockwise and counterclockwise, and the the correct one has to be chosen.

Resources