I am having a problem using a method from com.esri.arcgis.display.IColor. The method is IColor.getRGB() which returns an int.
IColor symbolColor = symbol.getColor();
int colorInt = symbolColor.getRGB();
//TODO: get RGB values from colorInt
From the documentation:
RGB returns the Long (ASCII) number calculated from the Red, Green and Blue color attributes.
I need to get the individual RGB values (standard 0-255) from this int somehow but I have been unable to find almost any information on converting from an ASCII number to RGB values.
Have solved by converting the int to hex, and then decoding the hex string using Color.decode() to obtain a Color. Using Color instance we can grab the individual RGB values.
IColor symbolColor = symbol.getColor();
int colorInt = symbolColor.getRGB();
String hexColor = Integer.toHexString(colorInt);
Color color = Color.decode("#"+hexColor);
int red = color.getRed();
int blue = color.getBlue();
int green = color.getGreen();
int alpha = color.getAlpha();`
It is possible to convert these values by transferring their RGB integer.
int rgb = symbol.getColor().getRGB();
Color color = new Color(rgb);
int red = color.getRed();
int blue = color.getBlue();
int green = color.getGreen();
int alpha = color.getAlpha();
You might need to further investigate wether that rgb variable also contains the alpha values, in which case you want to use new Color(rgb, true) instead.
Related
The code:
String green = "#99FFCC";
adjustColor(green);
private int adjustColor(String color){
int colorToAdjust = Color.parseColor(color);
//Pseudocode
int red = colorToAdjust.getRed(); //get red value from RGB
red *= 0.5; //halve it
colorToAdjust.setRed(red); //set red value
return colorToAdjust;
}
Given a string hex color, I parse it to an int. How do I change the individual RGB colors? Above is a somewhat pseudo-code of what I'm trying to do: parse hex, get individual color, change it, write it back
You can simply extract the amount of Red by calling red() function and then create the new color based on the original values of Blue & Green and the halved value of the Red you extracted before.
You can then create the new color by calling the rgb() function and passing it the new values of Red, Green & Blue.
Here is the code snippet:
String green = "#99FFCC";
adjustColor(green);
private int adjustColor(String color) {
/* Get RGB Value Of Color */
int colorToAdjust = Color.parseColor(color);
/* Get Red Value From RGB */
int redAmount = Color.red(colorToAdjust);
/* Return New Color By Halving Red */
return Color.rgb(0.5 * redAmount, Color.green(colorToAdjust),
Color.blue(colorToAdjust));
}
You can have a look at this Reference for more information.
i wrote a java code to change all the red values of a black and white image to 255, so the output would be a red image.
But its not red, instead it outputs a brighter image.
What did I do wrong?
File bwgFile = new File("X:/Java/Documents/NetBeansProjects/colour/input/bwg.png");
BufferedImage bwgImage = ImageIO.read(bwgFile);
int width=bwgImage.getWidth();
int height=bwgImage.getHeight();
for(int w=0; w<width; w++){
for(int h=0; h<height; h++){
int pixel = bwgImage.getRGB(w,h);
Color bwg = new Color(pixel);
int c=bwg.getRed();
Color red = new Color(255,c,c);
int cpixel = red.getRGB();
bwgImage.setRGB(w,h,cpixel);
}
}
ImageIO.write(bwgImage, "png", new File("X:/Java/Documents/NetBeansProjects/colour/output/c.png"));
input
output
EDIT:
I have found out what the problem was, apparently when the input is a greyscale image it will try to make the output a greyscale image as well thus making it darker when blue and green colors get removed and brighter when red gets added. not using a grayscale image as input fixed it.
If I understand what you're trying to do, you're trying to create a greyscale image, except that it is "redscale", using only shades of red. Therefore, you need to compute the greyscale constant of each pixel.
From wikipedia (Greyscale), the luminance of a pixel Y = 0.2126R + 0.7152G + 0.0722B. So, try this
int pixel = bwgImage.getRGB(w,h);
Color bwg = new Color(pixel);
float c = (0.2126f * bwg.getRed() + 0.7152f * bwg.getGreen() + 0.0722f * bwg.getBlue());
int cc = (int)Math.round(c);
Color red = new Color(cc, 0, 0);
int cpixel = red.getRGB();
bwgImage.setRGB(w,h,cpixel);
Alternatively, you can simply retain the red component and set green and blue to 0. This will leave you with just the "redness" of each pixel.
int pixel = bwgImage.getRGB(w,h);
Color bwg = new Color(pixel);
int c=bwg.getRed();
Color red = new Color(c,0,0);
int cpixel = red.getRGB();
bwgImage.setRGB(w,h,cpixel);
NOTE: This solution above only works on images that are not using IndexColorModel. You can check the color model using BufferedImage's getColorModel(). For IndexColorModel, setRGB() does not work directly and instead picks a color in the index closest to the set color, as per HaraldK's comment. To achieve the desired result for images using IndexColorModel, you can create a new BufferedImage with TYPE_INT_ARGB:
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Then, write the calculated pixel colors to this new image and save the new image instead.
I just need to check three pixel values on the whole image (Image instance). I'd really like to do this without allocating an array of pixels.
Is that possible? Something like BufferedImage's getRGB()?
Yes. You are on the right track with the getRGB method, as it would return a single int with three values (RGB). To convert the single int to three ints, you can do two things:
1. Use the Color class's built-in constructor:
int rgb = img.getGraphics().getRGB(0,0);//Get color of pixel 0,0
Color c = new Color(rgb); //c now contains the r, g, and b values.
2. Build the decoder yourself:
int rgb = img.getGraphics().getRGB(0,0) //Get color of pixel 0,0
int r = rgb >> 24;
int g = 0 >> 16;
int b = 0 >> 8;
Both methods will return a r, g, and b value, which can be used.
I want to extract the R,G and B values of the pixels of an image. I do it in two ways.
File img_file = new File("../foo.png");
BufferedImage img = ImageIO.read(img_file);
1st method(which works fine):
img.getRaster().getPixel(i, j, rgb);
2nd method(which throws new IllegalArgumentException("More than one component per pixel"))
red = img.getColorModel().getRed(img.getRGB(i, j));
What is the reason for this behaviour?
Normally when I want to extract RGB from a BufferedImage I do something like this:
File img_file = new File("../foo.png");
BufferedImage img = ImageIO.read(img_file);
Color color = new Color(img.getRGB(i,j));
int red = color.getRed();
Based on the JavaDocs
An IllegalArgumentException is thrown if pixel values for this
ColorModel are not conveniently representable as a single int
It would suggest that the underlying color model is representable by a single int value
You may also want to take a look at this answer for some more details
Typically, you would simply take the int packed pixel from the image and use Color to generate a Color representation and then extract the values from there...
First, get the int packed value of the pixel at x/y...
int pixel = img.getRGB(i, j);
Use this to construct a Color object...
Color color = new Color(pixel, true); // True if you care about the alpha value...
Extract the R, G, B values...
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
Now you could simply do some bit maths, but this is simpler and is more readable - IMHO
How do I interpret the returned array from build-in method getPixels for a Bitmap?
Here is my code:
public void foo() {
int[] pixels;
Bitmap bitmapFoo = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.test2);
int height = bitmapFoo.getHeight();
int width = bitmapFoo.getWidth();
pixels = new int[height * width];
bitmapFoo.getPixels(pixels, 0, width, 1, 1, width - 1, height - 1);
}
The array "pixels" gets returned with values from -988,602,635 to 1,242,635,509 and that was just from a few colors on a simple PNG file I made. How can I interpret the numbers that get returned from this method?
Edit: I realize this single integer represents a color. I just don't understand how to interpret this single integer into the RBG and alpha values that make up the color.
Thanks.
PS. If your asking yourself, "what is he trying to do?" I am trying to figure out a way to dynamically modify the color of a bitmap.
You can also do the following to retrieve colors from
an int :
int mColor = 0xffffffff;
int alpha = Color.alpha(mColor);
int red = Color.red(mColor);
int green = Color.green(mColor);
int blue = Color.blue(mColor);
It returns an int for the Color class.
The Color class defines methods for
creating and converting color ints.
Colors are represented as packed ints,
made up of 4 bytes: alpha, red, green,
blue. The values are unpremultiplied,
meaning any transparency is stored
solely in the alpha component, and not
in the color components. The
components are stored as follows
(alpha << 24) | (red << 16) | (green
<< 8) | blue. Each component ranges
between 0..255 with 0 meaning no
contribution for that component, and
255 meaning 100% contribution. Thus
opaque-black would be 0xFF000000 (100%
opaque but no contributes from red,
gree, blue, and opaque-white would be
0xFFFFFFFF
For example, when you use the Paint object:
Paint pRed = new Paint();
pRed.setColor(Color.RED);
setColor expects an int. Color.RED is that int value for their pre-defined meaning of "red".
Even more:
int alpha=argb>>24;
int red=(argb & 0x00FF0000)>>16;
int green=(argb & 0x0000FF00)>>8;
int blue=(argb & 0x000000FF);
If you have your alpha, red, green and blue values, your in color is equal to (alpha << 24) + (red << 16) + (green << 8) + blue.
To retrieve your alpha, red, green and blue values from an int, say argb:
int alpha=argb>>24;
int red=(argb-alpha)>>16;
int green=(argb-(alpha+red))>>8;
int blue=(argb-(alpha+red+green));
Besides that, I think it should be
bitmapFoo.getPixels(pixels, 0, width, 0, 0, width, height);
The coordinates start from 0, right?