Color Addition in Android - c#

How is color addition done in android? For my pathtracer i am storing a Color but i didn't find a way to get the r,g,b values back.
I think if i want to get red i do this:
returnColor.red(0)
but it doesn't work.
Since i work more with android in the last week i do more and more think why stuff is made so complicated in android? In C# you just add the r,g,b values and you don't get an integer back if you type
Color.White

you can use the static method red of the Color class:
int red = Color.red(color)
The same class has the argb method that allows you to get the int representing your color, through the 4 components. E.g.
int color = Color.argb(255, 255, 255, 255);
Here you can find the documentation

Related

The color gets changed via script but doesn't get updated in game

I have a simple line that basically makes the light color switch according to 3 color values.
GameObject.Find("Directional Light").GetComponent<Light>().color = new Color(color.R, color.G, color.B, 255);
But when I actually use it (I made it so it gets triggered when I press C), the color value itself gets changed, but it's not updated until I change it manualy through inspector.
If anyone knows how I can fix it, I would be very grateful.
It's the same thing with Camera background color, and I am really unsure if it's me doing something wrong about changing the color, me not doing something that has to be done for it to update, or it just not working like that.
I tried changing the color property with a matching color value. It should have changed AND updated so the change can be observed in game, but instead the new color value is stored in the inspector's color menu, and won't change until I actually change it manually. There are no errors, and the color DOES appear in the inspector, it's just not changed to.
Color values had to be divided by 255 due to Color only accepting values from 0 to 1
So, it will be
color.R/255, color.G/255, color.B/255
Try this :
GameObject.Find("Directional Light").GetComponent<Light>().color = new Color(4, 191, 4, 255);

Emgu GetAverage Color of black color of Image

I am new at Emgu cv. My target is to compare two photos which are the same photo but the brightness is slightly different and gets the dark color or dark spot percentage of ROI.
I saw GetAverage method but how can I get color percentage of image by specify color. eg black is 80%, white 20%.
What is the mask parameter in the GetAverage method?
I read the documentation but I don't understand.
My idea is I will change both photos to grayscale and set ROI then get the average value. I don't know its the correct way to get my target.
So How can I done this?
Update
Below are ROI images of grayscale.
Left photo average intensity: 37.4879
Right photo average intensity: 40.9773
I add some scratch to the right photo.
Left photo average intensity: 37.4879
Right photo average intensity:
40.7638
Why scratch photo intensity is lower than without scratch photo. By right, It should be greater right? because it have more gray color right? Why?
Or
I added scratch, it means more black, So gray color reduction?
What is the mask parameter in the GetAverage method?
The mask parameter in the overloaded GetAverage(mask) allow you to find the average color of the masked area only according to the official documentation.
Regarding the concept of mask (which is related to the concept of ROI), it allows you to analyze or elaborate (depending on the context) only the location of the image where the mask is non-zero. I invite you to deepen on this here. If you find the documentation not clear enough, try to understand how masks works in Photoshop, the concept is the same and surely you will find many more explanations and much more intuitive.
Why scratch photo intensity is lower than without scratch photo. By
right, It should be greater right? because it have more gray color
right? Why?
No, it shouldn't be grather. Here as indicated by the documentation, GetAverage() returns to you the average color with the structure you originale image have (TColor), Gray (grayscale) in this case.
As you can see here the grayscale value for darker color tents to 0, on the contrary the white corresponds to 255.
So adding scratch implies that your image darkens.
If you try apply GetAverage() to an image with a different color type (Bgr, Bgra, Hsv, Hls, ...), it will return you the average as color of that type.
Last thing,
I saw GetAverage method but how can I get color percentage of image by
specify color. eg black is 80%, white 20%.
you cannot with this approach. As said above GetAverage() returns to you a Gray color. The only thing I can suggest is that you manually convert the result obtained into a percentage of color with something like this:
private double ConvertToPercentage(double valueToConvert, int rangeStartAt, int rangeEndAt) {
return valueToConvert/(rangeEndAt - rangeStartAt) * 100;
}
Obviously this piece of code only works with single values colors (not rgb for example, which has 3 values). You can very easily re-implement something like this in case you need it.

C# get good color for label

I am using a random color for the background in my Windows Forms application. Now I want to display a label.
The problem is that when the random color is white and the label is too, then the label is not visible.
How can I get a perfect color that is visible on my background color? (My background color is a random color from System.Drawing.Color.)
There are various ways to ensure a proper contrast.
Option one : I usually stick to keeping the text Black or White, depending on the brightness of back color.
To get the brightness one could simply go for the built-in function Color.GetBrightness()
Unfortunately this is not really a good solution, as the result is not perceptually correct; to wit: Green and Yellow have the same values, which is obviously not what our eyes will perceive.
Instead this tiny function will help:
float getBrightness(Color c)
{ return (c.R * 0.299f + c.G * 0.587f + c.B *0.114f) / 256f; }
Now we can pick either Black or White:
Label lbl = new Label();
lbl.BackColor = colors[rnd.Next(colors.Count)];
lbl.ForeColor = getBrightness(lbl.BackColor) < 0.55 ? Color.White : Color.Black;
The code uses a list of known colors:
List<Color> colors = ((KnownColor[])Enum.GetValues(typeof(KnownColor))).
Select(x => Color.FromKnownColor(x)).ToList();
Option two : If you want to get colors in the foreground you could pick it randomly and repeat until you get a decent contrast by comparing e.g.
while (Math.Abs(c1.GetBrightness() - c2.GetBrightness()) < 0.5f )
c2 = colors[rnd.Next(colors.Count)];
Note that you must not push the epsilon value too high or else it won't find a suitable color. This happens when trying to find a color that is too far away from a medium brightness! You could add a counter and after a while pick simply black or white..
Option three : Yet another way would be to construct a color with Color.FromArgb().
You could start by inverting each channel, which will give nice color contrasts; but if the color one is of medium brightness and/or saturation you would have to correct, maybe again by picking black or white..
Note: for the above image I have enumerated all KnownColors, which already looks pretty random.
To add some order you may sort the list by color properties, e.g. by hue, then by brightness:
List<Color> allcolors = ((KnownColor[])Enum.GetValues(typeof(KnownColor)))
.Select(x => Color.FromKnownColor(x))
.OrderBy(x => x.GetHue()).ThenBy(x => getBrightness(x)).ToList();

Win Form Colors

I have an access color (light yellow, or 153,255,255) but when I try to use this RGB combo in my Windows Form by using such syntax
System.Drawing.Color ly = System.Drawing.Color.FromArgb(153, 255, 255);
this.BackColor = ly;
the windows form does not produce the same color as my Access Form color does. I also tried to use this
this.BackColor = Color.FromName("LightYellow");
to no such success. Did I translate the color incorrectly? Or can I not use the same colors?
EDIT -
#Alex K thank you for your response, I used this website to attempt to convert and that is the RGB color that it gave. Take a look here:
http://www.numberconverter.net/get-computer-data/color-code-converter/from-rgb-to-ms-access-color/
Access colors seems to be represented as BGR, not RGB (You can verify that quickly by inputting the values in MSPaint). You must switch the values of the first and last values.
So you will need to use
System.Drawing.Color ly = System.Drawing.Color.FromArgb(255, 255, 153);
to obtain your light yellow color.
If instead you want to use the literal name, you can use FromKnownColor which will give you an enum of all color names. It's much more error-proof.
this.BackColor = Color.FromKnownColor(KnownColor.LightYellow);
This won't restrict you on the color choice either, according to the doc FromName takes as an input
Valid names are the same as the names of the elements of the
KnownColor enumeration.

How to determine the color hue?

let's say that I have a HEX color #a08040. How can I determine in C#, if that color is the one of the many hues of the Brown color ?
In the other words, I have 4 colors: Brown, Red, Black, Gray.
How to determine what color hue is my HEX color?
If you are using WinForms, the Color.GetHue method will do exactly what you want.
Edit
Unfortunately, there is no WPF equivalent to GetHue, you'll have to convert to a WinForms Color if you want to use GetHue from WPF. You could also compute the Hue yourself, if you're feeling frisky... Wikipedia has the formula you'd need to use.

Categories