I have some color, coded in RGB format: 121E31 hex. How do I pass this color to Java's Color class?
This is normally done using decode:
Color color = Color.decode("0x121E31");
The RGB value of your "121E31" color is : 18,30,49
For this refer http://www.yellowpipe.com/yis/tools/hex-to-rgb/color-converter.php
Now this RGB value can be added using java class as follows
Color c = new Color(18,30,49);
Related
I get #AARRGGBB color from
int getColor=bitmap.getPixel()
(Bitmap is ARGB_8888)
As example, getColor value is: #20000000(light grey)
I need to get same color in #RRGGBB, something like #BDBDBD(light grey).
How can I do this?
If you want the color that would appear when placing that color on top of white you have to blend it. Since alpha is 0x20 you multiply that color by 0x20 and white by 0xff-0x20 and add them up.
Of course do this to R, G and B separately.
Have a look here:
how to change the color of certain pixels in bitmap android
In your case it would be
for (int i=0; i<myBitmap.getWidth()*5; i++)
pixels[i] = 0xFFBDBDBD;
The 32 bit integer starts with 0xFF=255 for the alpha value to provide 100% opacity.
How can I get my color and set opaque to it?
int myColor = getResources().getColor(R.color.ColorPrimary);
You can use ColorUtils.setAlphaComponent to create a new color value with the same RGB but different alpha. The alpha value needs be from 0 - 255 so it would look something like this:
int color = getResources().getColor(R.color.ColorPrimary);
int color50percent = ColorUtils.setAlphaComponent(color, 128);
this is example color black alpha 65
<color name="ColorPrimary">#A6000000</color>
if you want change opacity just change 2 number in front
hope it help
setAlpha is applied on views. Get your view and then use .setColor(...) and .setAlpha(...) .
I want to load a 8-bit grayscale image using grayscaleImage = ImageIO.read(grayscaleFile). However, the BufferedImage class (which is the type of grayscaleImage) only provides a method getRGB(int x, int y). So, what are the values of color.getRed(), color.getGreen(), color.getBlue() and color.getAlpha() where
Color color = new Color(grayscaleImage.getRGB(x, y), true);
From a first observation, it seems like the 8-bit value, stored in the image file at pixel (x,y), is propagated into the red, green and blue component of color while it's alpha value is fixed at 255.
Can someone confirm this observation?
The value that is obtained with image.getRGB(x,y) is an int that consists of the three color components Red, Green and Blue, each having 8 bit. The Alpha value is fixed as 255 when the image does not contain transparency.
When the image is a grayscale image, the Red, Green and Blue components of this RGB value will be all equal. So you can obtain the "gray" value as
int rgb = image.getRGB(x,y);
// This yields a value between 0 (black)
// and 255 (white) :
int gray = rgb & 0xFF;
That's right. A gray color is just a color with same value in three color's components (red, green, blue). Although you only need one value to determine the color (because red, green and blue components have the same value), at image loading that value is assign to all three color components.
If you get the value for each pixel of the image, you must obtain the same value for three color's components for the same pixel x,y. For alpha component, as it's a grayscale image, is correct it be at 255.
I have the following:
mPaintRefernce.getColor()
How would I retrieve the RGB values in the range of 0-255, so Red = 200, Blue = 120 etc?
That's possible, use the android.graphics.Color class provided by android framework
Color.red(int color); to get the red integer value from color
Color.green(int color); to get the green integer value from color
Color.blue(int color); to get the blue integer value from color
For example, to get the red color.
int red = Color.red(mPaintRefernce.getColor());
I have list of colors in HEX format (for example #000000) and I would like to detect color type (blue, red, green etc.) and then change color type to another color type. Is this possible and are there any frameworks/libraries for this task?
Example:
I have color #EB1369 (red) then I convert it to blue and it becomes for example #1313EB (blue).
Here's a function that will let you shift colors around the hue circle. You should read the wikipedia page on the HSB (or HSV) color system to really understand what is going on: http://en.wikipedia.org/wiki/HSV_color_space
/** Converts an input color given as a String such as "ab451e" to
* the HSB color space. Shifts its hue from the given angle in degrees.
* Then returns the new color in the same format it was given.
*
* For example shift("ff0000", 180); returns "80ff00" (green is the opposite of red).*/
public static String shift(String rgbS, int angle) {
// Convert String to integer value
int value = Integer.parseInt(rgbS, 16);
// Separate red green and blue
int r = value >> 16;
int g = (value >> 8) & 0xff;
int b = value & 0xff;
// Convert to hsb
float[] hsb = Color.RGBtoHSB(r, g, b, null);
// Convert angle to floating point between 0 and 1.0
float angleF = (float)(angle/360.0);
// Shift the hue using the angle.
float newAngle = hsb[0] + angleF;
if(newAngle > 1.0)
newAngle = newAngle - 1.0f;
hsb[0] = newAngle;
// Convert back to RGB, removing the alpha component
int rgb = Color.HSBtoRGB(hsb[0], hsb[1], hsb[2]);
rgb = rgb & 0xffffff;
// Build a new String
return Integer.toHexString(rgb);
}
Detecting colors can be complex, it depends on the result you really expect.
If what you want is simply an approximation (red, green, blue, yellow, etc.) then you can look at the hue circle of the HSB color-space, choose a hue value for each color you want to define, and then map the color you get in input to the closest one you chose.
You can also rely on things like named HTML colors: http://www.w3schools.com/html/html_colornames.asp . Take this list, create a mapping in your program, then all you have to do is map the color you get to the closest one in your map, and return its name. Be wary though: computing the distance between two colors can be tricky (especially in RGB) and naive approaches (such as channel-by-channel difference) can give surprisingly bad results. Colorimetry is a complex topic, and you will find good methods on this page: http://en.wikipedia.org/wiki/Color_difference
Try convert RGB values to HSV (HSB exactly) - it is format for colors which is more comfortable for human. After conversion, all u need to do is change H V (probably) and convert it back to RGB.
I guess that you like to convert RGB color to HSB. YOu can do this wuth:
java.awt.Color.RGBtoHSB(...)
then you can easily determine whetther H value fits in your definition of blue, and modify it to whatever you like. After this, you can easily convert it back to RGB via:
java.awt.Color.getHSBColor(...)
And ifg you do not like jawa.awt.color just multiply color vector by transofrmation matrix.
Each HEX Color has three parts in it, red, green and blue the # identifies a HEX color, the following two letters are the amount of red; the next two are green and the next two are blue. i.e: RGB
The two letters can have a maximum hexidecimal value of FF which is 255, and a minimum of 00 which is zero.
So you can argue like this, I want a color with 2 red parts, 7 green parts, and zero blue parts, which will give you #020700
That is why #FFFFFF is white (all the colors together) and #000000 is black (no colors at all)
With this logic you can modify the color in any way you want; The Color class can also help a lot.