Android changing a color's brightness - android

I want to change the brightness of any given color (Note: I am not talking about screen brightness), I have looked at the Color class, it has a few methods for conversions between RGB and HSV, I'm a newbie in this area. To start with, how do I change the brightness of red, if its value is spefied in RGB (#FF0000)?

The easiest way would be to convert the color to HSL (not HSV! they are different - see http://en.wikipedia.org/wiki/HSL_and_HSV) and change the L component - increase to make it brighter, decrease to make it darker.

Considering that you are talking about brightness (color enhance) and not luminance (white amount), your model is the HSV (aka HSB) and not HSL.
On fast briefing, if you enhance the V channel on HSV over, lets say... some blue, you have a "more blue" color. If you enhance the L channel on HSL model you have a more "clear and washed" blue.
The android.graphics.Color class have built-in support to HSV model. Use Color.colorToHSV() and Color.HSVToColor() to edit the brightness value (or hue, or saturation, if you like).
On HSV model, H (hue) define the base color, S (saturation) control the amount of gray and V controls the brightness. So, if you enhance V and decrease S at same time, you gets more luminance, in pratice.

For starters, you need to remember two things -
To reduce brightness, you can change red from #FF0000 to #AA0000 or #880000 - basically reduce the Red component.
You can also try reducing opacity - often you'll realize that it works better than just reducing brightness.

You can use Color.colorToHSV to convert the color to HSV, then change the brightness of the HSV color, then use Color.HSVToColor to convert it back to a color int. For example, the following code sets the brightness to 0.5:
#ColorInt int originalColor = /*your original color*/;
float[] hsv = new float[3]; //Create an array to pass to the colorToHSV function
Color.colorToHSV(originalColor, hsv); //Put the HSV components in the array created above
hsv[2] = 0.5f; //Whatever brightness you want to set. 0 is black, 1 is the pure color.
#ColorInt int newColor = Color.HSVToColor(hsv); //Convert it back to a ColorInt

Related

OpenCV InRange parameter

I'm using OpenCV on Android to find circles of specific colour's in real time. My first step is to keep only pixels which corresponds to my defined color i'm looking for (red or green in this example). Example Image.
For this purpose i'm using the method inRange().
Here is my Question: What kind of color model (RGB, BGR, HSV, ..) is required as lower-/upper-bound color parameter's? And: what is a good practice to define these color bounds in respect to natural brightness changes?
matRgba = inputFrame.rgba();
Scalar lowerColorBound = Scalar(0.0, 0.0, 0.0); // Blue, Green, Red?
Scalar upperColorBound = Scalar(0.0, 0.0, 0.0);
// convert to HSV, necessary to use inRange()
Imgproc.cvtColor(matRgba, matRgba, Imgproc.COLOR_RGB2HSV);
// keep only the pixels defined by lower and upper bound range
Core.inRange(matRgba, lowerColorBound, upperColorBound, matRgba);
The required color model for the inRange(src, lowerb, upperb, dst) function in OpenCV is HSV.
The lowerb and upperb parameters specify the required lower and upper color bounds in the HSV format. In OpenCV, for HSV, Hue range is [0,179], Saturation range is [0,255] and Value range is [0,255].
For object tracking applications a possible practice (as suggested in the official documentation) to define these two color bounds can be:
Start from a color to track in RGB format.
Convert the color to the HSV format. Let (H, S, V) be its value.
Assign the value (H - deltaH, minS, minV) to lowerb and the value (H - deltaH, maxS, maxV) to upperb.
Possible starting values for the parameters defined in step 3 can be:
deltaH = 10
minS = 100, minV = 100
maxS = 255, maxV = 255
Then you can adjust them to narrow down or enlarge the H, S, V intervals as needed.

Detect font color from image in android after OCR

I have an image and using tesseract I am finding text from it.
I have bounding box of the text in the form of rect(left,top,right,bottom)
I want to find the font colour of the text for which I tried to traverse the 2D bounding box matrix and I am comparing the background colour with every colour pixel in the bounding box. Wherever I am getting a colour other than background colour I am returning the colour which would be the font colour.
//rectArrayList is an array list of rect for a line in the image
for(int i=rectArrayList.get(0).left;i<rectArrayList.get(0).right ;i++){
for(int j=rectArrayList.get(0).top;j<rectArrayList.get(0).bottom;j++){
pixel=colorbit.getPixel(i,j);
R = (pixel & 0xff0000) >> 16; //channel the pixel in RGB values
G = (pixel & 0xff00) >> 8;
B = pixel & 0xff;
//backColour is the background colour of the image
if(backColour!=Color.rgb(R,G,B)){
return Color.rgb(R,G,B);
}
}
Lets say the background colour is a shade of yellow. But while traversing the matrix, I get a different shade of yellow which is background colour behind the font colour and not the font colour. So I get wrong font colour.
I know this technique will fail as the background colour of image would have varying shades of same colour depending on brightness etc.
What should I do to get exact font colour from image?
I could give you many different solutions to that problem. Here's something simple to start with:
Don't stop when color 1 is unequal color 2. Add some tolerance.
Calculate the Euclidean distance between both RGB tuples. Then check if the distance is bigger than some threshold.
You could also transform RGB to Hue and calculate the absolute difference between both Hue values.
Of course there are more complex and better solutions, but given your knowledge about image processing they would not help you for now.

Difference between brightness and lightness in Image Manipulation

I have an application to adjust the lightness/brightness of the image and the saturation and i found this link here which uses android colormatrix to adjust the brightness. Here's the function to adjust brightness in colormatrix.
public static void adjustBrightness(ColorMatrix cm, float value) {
value = cleanValue(value,50);
if (value == 0) {
return;
}
float[] mat = new float[]
{
1,0,0,0,value,
0,1,0,0,value,
0,0,1,0,value,
0,0,0,1,0
};
cm.postConcat(new ColorMatrix(mat));
}
But i don't know if there's a difference with lightness in brightness. Because in GIMP software there is lightness and brightness. See the screenshot below. If there's another method to adjust lightness/brigthness saturation at the same time I would be happy to see it :).
Brightness is visual perception related with reflecting or radiating of lights of different frequencies. On the otherhand lightness is TONE which moves from Darkest color to lightest color of same color Gamma is nothing but an attribute to handle luminescence Means how bright should be the image be (it's more a mathematical calculation rather definition.
In non technical words we can say
Lightness seems to more affect the saturation of the photograph, whereas brightness seems to affect how vivid the colors look.
If you want to go in deep for the proof of concepts, please follow this
http://www.sciencedirect.com/science/article/pii/S0042698910004578

Android find if color is too close to black or white

I have a int representation of a color. What I want to do is if this color is too black or too white, I want to set background to a default color. I want to find if the color is too close to black or white using RGB or HSV representation is fine. By too black or too white, I mean to say I can set some threshold depending on the scale. Say there is a scale from 0 to 1 where 0 is black and 1 is white, I want to reject all the colors below 0.2 and above 0.8. I tried the below code but it actually does not work. This does not work since I see some other colors also get rejected which are not shades of black or white. Please help.
public static boolean isWhitish(int color) {
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
return hsv[2] > 0.8;
}
First you will want to get the HEX CODE of your color, then you can use this solution to extract the brightness of that color.
You can then use your (brightness < .2 || brightness > .8) to decide what to do next.
Hope this helps, good luck :-)

alpha value always reset after color change of paint

I'm drawing several shapes in different colors, using a single instance of Paint. I would like the alpha value (< 255) of each painted region to be the same. I set the desired alpha value when initializing the Paint. However, whenever I change the color of my Paint using setColor, the alpha value of my paint is reset to 255 (fully opaque).
The following code demonstrates this issue:
myPaint.setAlpha(100);
// myPaint.getAlpha() returns 100, as expected;
myPaint.setColor(Color.DKGRAY);
// myPaint.getAlpha() now returns 255;
Why does this happen? Will there be any impact on performance if I am forced to call paint.setAlpha(...) every time I change the paint color?
This is because the color you are using is of format argb. See the Paint and Color javadoc. You need to specify a color with just RGB or set the color alpha to the value you want to use. And it shouldn't be much of performance hit if you set the alpha each time, if you want to go that route.
As an added example you could do
paint.setColor(new Color(100, Color.red(Color.DKGRAY), Color.green(Color.DKGRAY), Color.blue(Color.DKGRAY)));
If you were going to always set the color to something like dark gray I would say you are better off creating a color resource with the values you want and using it.

Categories

Resources