Loading 8-bit grayscale image using ImageIO - java

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.

Related

Draw CMYK Color from RGB

Hello. I have a screen like above. By using the sliders, I get red, green, blue. Also, I calculate cyan, magenta, yellow and from red, green, blue for CMYK. My question is that is there any way to show CMYK colour in java like light purple in the picture.
private void stateChanged() {
red= sliderRed.getValue();
green= sliderGreen.getValue();
blue= sliderBlue.getValue();
txt_background.setBackground(new Color(red, green, blue));
}
It looks to me like the java color class, has a constructor for making a color object in cmyk
https://docs.oracle.com/javase/7/docs/api/java/awt/Color.html#Color(java.awt.color.ColorSpace,%20float[],%20float)
and
https://docs.oracle.com/javase/7/docs/api/java/awt/color/ColorSpace.html
So you would end up with something like
Color cmykColorValue = new Color(TYPE_CMYK, [cValue, mValue, yValue, kValue], alpha)
Where alpha is form 0 to 1, and cValue, mValue, yValue, kValue are the corresponding cmyk values.
That should make a new CMYK color object that can be used anywhere a color object can be used.
The correct usage of the Color constructer for CMYK is as follows:
java.awt.Color cmyk = new Color(ColorSpace.getInstance(ColorSpace.TYPE_CMYK), new float [] {cyan,magenta,yellow}, key/alpha);

Convert ARGB to RGB and get the same color

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.

Set the fore ground of a swing component by using color code

Is there any way to set the fore ground of for example a label directly by color code rather than the predefined colors that java provides.
For instance :
instead of:
SomeLabel.setForeground(Color.Red);
We do:
SomeLabel.setForeground("240,240,240");
I'm asking this question because I'm setting label colors of the required fields to red when the user skips them, so when I wanna reset their color I can't find the color that I've defined within the predefined colors that java provides.
You could use new Color(240, 240, 240)
Take a look at javax.swing.Color for more details
You can also reset the labels back to their look and feel defaults using UIManager.getColor("Label.foreground"); on most look and feels, for example
SomeLabel.setForeground(new Color(240,240,240));
You can use Color class of java
You can also add Alpha etc. Refer to the Color constructor below
Color(ColorSpace cspace, float[] components, float alpha)
Creates a color in the specified ColorSpace with the color components specified in the float array and the specified alpha.
Color(float r, float g, float b)
Creates an opaque sRGB color with the specified red, green, and blue values in the range (0.0 - 1.0).
Color(float r, float g, float b, float a)
Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0.0 - 1.0).
Color(int rgb)
Creates an opaque sRGB color with the specified combined RGB value consisting of the red component in bits 16-23, the green component in bits 8-15, and the blue component in bits 0-7.
Color(int rgba, boolean hasalpha)
Creates an sRGB color with the specified combined RGBA value consisting of the alpha component in bits 24-31, the red component in bits 16-23, the green component in bits 8-15, and the blue component in bits 0-7.
Color(int r, int g, int b)
Creates an opaque sRGB color with the specified red, green, and blue values in the range (0 - 255).
Color(int r, int g, int b, int a)
Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0 - 255).

How to get RGB values from the Paint object

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());

Color detector in Java

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.

Categories

Resources