Is there a way to add aplha to color that i can get from a color string.
Because now i can draw my color without the alpha by using the following code
CircleOptions options = new CircleOptions().center(new LatLng(car.getLatitude(), car.getLongitude()))
.radius(car.getRadius())
.strokeColor(Color.argb(50,232,245,248))
.strokeWidth(2)
.fillColor(Color.parseColor(car.getColorString()));
This works fine. But i want to make the color more transparent. Is there a good way to add alpha do this color because i can only get out a "ColorString"
Color is represented as a int and you can represent it with hex notation like 0xAARRGGBB, which means Alpha, Red, Green, Blue. So each value can be 0 to 255.
Thus, what you want can be achieved with some bit operations.
If you want reset alpha value then,
int newColor = 0x10000000 | (Color.parseColor(car.getColorString()) & 0xFFFFFF);
(0x10000000 means new alpha value - 16 which is about 6.274% alpha because 100% is 255)
(0xFFFFFF means we take only rgb value from a color)
See also
You can change the alpha of a colour in android with two extra digits on hex code of the colour.
Take a look at the official colour documentation
All you need is applying the following rules :
The value always begins with a pound (#) character and then followed
by the Alpha-Red-Green-Blue information in one of the following
formats:
RGB
ARGB
RRGGBB
AARRGGBB
Related
After I set a pixel of a java.awt.image.BufferedImage to a value using setRGB, a subsequent call to getRGB returns a different value than I set.
Code:
BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_GRAY);
int color1 = -16711423; // corresponds to RGB(1, 1, 1)
image.setRGB(0, 0, color1);
int color2 = image.getRGB(0, 0);
System.out.println(color1);
System.out.println(color2);
It produces the following output
-16711423
-16777216
I think it has to do something with gamma correction, but I couldn't find anything about it in the documentation.
Ideally, I want to change this behavior to return the same value as I set. Is that possible?
The BufferedImage.getRGB() method, always returns a color (as an int in "packed format") in the non-linear sRGB color space (ColorSpace.CS_sRGB). It will do so, regardless of what color space and bits per pixel etc. your image has. Thus, conversion and possible precision loss may occur.
From the JavaDoc:
Returns an integer pixel in the default RGB color model (TYPE_INT_ARGB) and default sRGB colorspace. Color conversion takes place if this default model does not match the image ColorModel.
Your TYPE_BYTE_GRAY image internally uses a linear gray color space (ColorSpace.CS_GRAY), which does not map one-to-one with sRGB.
Also, I suggest using hexadecimal notation for (A)RGB colors, it makes the colors and difference much easier to see:
-16711423 == 0xff010101
-16777216 == 0xff000000
So, there is a minor precision loss here, but nothing unexpected.
If you want direct access to the pixel data, look into the Raster, SampleModel and DataBuffer classes (and their respective subclasses).
You set a color specified with an int which stores RGB components as bytes (in the range of 0..255 inclusive).
But the color model of your image is not RGB but BYTE_GRAY. Obviously you may suffer precision losing. This explains the different colors. Should you have used image type TYPE_INT_RGB you would've ended up with the same color.
I started learning java game programming and I am doing it by watching Notch's code being explained by a guy on YouTube, so I need help with this video:
https://www.youtube.com/watch?v=7eotyB7oNHE&list=PL8CAB66181A502179&index=5
He implemented colors in his game, and so did I, but I don't understand how they work. He made a function get in the colours class and it is called like this:
Colours.get(colour1, colour2, colour3, colour4);
He is using a spritesheet to do this, he will replace the black color with the "colour1", the dark gray colour with "colour2", light gray with "colour3" and white with "colour4".
The problem is that I don't understand the following: how can I get 3 digit colors without using letters?
Thanks!
He's using the int representation of colors.
Each color is represented by 4 values of 8 bits each:
Red value
Green value
Blue value
Alpha value
The int representation packs those 8-bit values into a single 32-bit int number so that the alpha value gets the highest bits, then the red value, then the green and finally, the blue value gets the lowest bits. Therefore, using bit-wise operations, you can create the int value out of the color component values as follows:
public static int getColorIntRepresentationOutOfColorComponentByteValues(byte alpha, byte red, byte green, byte blue) {
return ((int)alpha << 24) | ((int)red << 16) | ((int)green << 8) | (int)blue;
}
I was given a .jar file which generates a matrix of integer color codes from an image.
I can tell that the black color value is -16777216, and white is -1. However, I have never seen such values used for color codes before.
I can't see the code within the .jar file.
Could have asked this somewhere like Graphic Design SE, but I guess it is more possible that a coder here has worked with similar problems before.
The reason I want to find a reference "table/whatever" is because I would like to be able to get the color name based on such values.
EDIT: Here are some colors I got:
Black is -16777216
Red is -65536
Green is -16711936
Blue is -16776961
White is -1
Color values are more easily understood when given in hexadecimal format, specified with "0x" followed by 0-9 or A-F. Here, black is 0xFF000000, and white is 0xFFFFFFFF. The format is 0xAARRGGBB, where "AA" is two hexadecimal digits for the "alpha" component (00 is completely transparent, FF is completely opaque), "RR", "GG", and "BB" are the red/green/blue components, respectively.
0xFF000000 happens to be -16777216, and 0xFFFFFFFF is -1.
Edit: Fixed -16777216 value.
If we take, say, 32-bits per pixel pictures using ARGB and ARGB_PRE (ARGB but with premultiplied alpha), are the values identical when the alpha is fully on (that is: no transparency at all)?
For example if I have an ARGB pixel with the following value: 0xFF808080 (which is a shade gray, without any transparency because the alpha is at its max value: 255), what would this become in ARGB_PRE?
How can I find this out by myself? Is it enough to instanciate one buffered image using ARGB and the other ARGB_PRE and using setRGB(...) on both and then comparing the int I'd get back?
For example if I do this:
final BufferedImage bi1 = new BufferedImage(10,10,BufferedImage.TYPE_INT_ARGB);
final BufferedImage bi2 = new BufferedImage(10,10,BufferedImage.TYPE_INT_ARGB_PRE);
bi1.setRGB(0,0,0xFF808080);
bi2.setRGB(0,0,0xFF808080);
System.out.println("bi1: " + Integer.toHexString(bi1.getRGB(0, 0)));
System.out.println("bi2: " + Integer.toHexString(bi2.getRGB(0, 0)));
I get back the same value for both, but it's normal it's the very value I gave.
Basically my question boils down to this: if pictures doesn't have a single pixel being
transparent, can I generate the exact same pictures by using the exact same values both in ARGB and ARGB_PRE modes?
Or formulated this way: if I don't have any transparent pixel, are ARGB and ARGB_PRE basically identical?
Premultiplied alpha means that colors stored in the image data are multiplied by alpha, so they dont need to be multiplied when composing(drawing).
this doesnt change how image looks when drawed, but how it's data is stored...
BTW if your image only has alpha values of 255 (255 in composing means 1.0f) then resulted color will always be 1 * color = color (not changed)
Yes. Your understanding is correct.
I'm working in Java/Android and I want to take the 0xFFFFFFFF int color and break it into 4 bytes for red, green, blue and alpha. I know Color has methods for extracting specific color values, but it gives them as an int. I've gone through the SDK and found that the Color.red(color) method is the same as saying (color >> 16) & 0xFF, honestly I have no idea what this means but I think I can use this in some way.
I've tried doing
byte red = (byte)Color.red(color);
but this doesn't seem to work. The whole point is so I can feed in the values into OpenGL with the method
glColorPointer(4, GL_UNSIGNED_BYTE, 4, myColors);
myColors being a byte[].
Any help/tips/points would be greatly appreciated.
If Color has methods for extracting specific color values then extract Red as int and make it Hex then Green then Blue. You might also want to extract alpha. with Integer.toHexString(int) you can print the Hex value of an int.
(color >> 16) & 0xFF positions the Red color (by bitshifting and adding) to the first 2 out of 6 hexdigits of a non alpha Color. non alpha Color RBG consists of 3 bytes of information. Every byte is one Color with the order R(Red) B(Blue) G(Green) . 0xFF is one byte, if set to Red means that the Color has full red etc etc