My Web API will send the background color for my views, Background color range is from white #ffffff to black #000000. So I can not set any fix text color for my information text.
What is the best way to set my text color?
I'm thinking to invert the background color and set it as my text color. But I don't know How can I invert Any color or Hex color code.
e.g if my web(background) color is #00ff11 then my text color will be #ff00ee.
For this, I search over stack but did not find any method for color conversion.
Thanks
Try this.
This will give rgb of your hex color. Now you can invert your colors as below.
int invertColor(String myColorString) {
int color = (int)Long.parseLong(myColorString, 16);
int r = (color >> 16) & 0xFF;
int g = (color >> 8) & 0xFF;
int b = (color >> 0) & 0xFF;
int invertedRed = 255 - r;
int invertedGreen = 255 - g;
int invertedBlue = 255 - b;
int invertedColor = Color.rgb(invertedRed, invertedGreen, invertedBlue);
return invertedColor.toString();
}
private PaintType getNegativePaintType(String hexa) {
int color = Color.parseColor(hexa);
return new SolidColor((color & 0xFF000000) | (~color & 0x00FFFFFF));
}
getNegativePaintType("#00ff11") will return "#ff00ee"
Independently of the platform your using, you can achieve that using RGB values simply by subtracting the used color from it's base (255).
So for example:
color = (r,g,b)
invertedColor = (255-r,255-g,255-b)
Then you could transform it to hexadecimal value or whatever you need.
Related
I am having a problem with an image processing app I am developing (newbie here). I am trying to extract the value of specific pixels by using the getPixel() method.
I am having a problem though. The number I get from this method is a huge negative number, something like -1298383. Is this normal? How can I fix it?
Thanks.
I'm not an expert, but to me it looks like you are getting the hexadecimal value. Perhaps you want something more understandable like the value of each RGB layer.
To unpack a pixel into its RGB values you should do something like:
private short[][] red;
private short[][] green;
private short[][] blue;
/**
* Map each intensity of an RGB colour into its respective colour channel
*/
private void unpackPixel(int pixel, int row, int col) {
red[row][col] = (short) ((pixel >> 16) & 0xFF);
green[row][col] = (short) ((pixel >> 8) & 0xFF);
blue[row][col] = (short) ((pixel >> 0) & 0xFF);
}
And after changes in each channel you can pack the pixel back.
/**
* Create an RGB colour pixel.
*/
private int packPixel(int red, int green, int blue) {
return (red << 16) | (green << 8) | blue;
}
Sorry if it is not what you are looking for.
You can get the pixel from the view like this:
ImageView imageView = ((ImageView)v);
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
int pixel = bitmap.getPixel(x,y);
Now you can get each channel with:
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
getPixel() returns the Color at the specified location. Throws an exception if x or y are out of bounds (negative or >= to the width or height respectively).
The returned color is a non-premultiplied ARGB value.
Is there an easy way on Android to set the alpha component of a color retrieved from getResources().getColor(id)?
For example, this is what I'm currently doing to change an existing color to 70% opacity:
DrawerLayout drawerLayout = ...;
int color = getResources().getColor(R.color.bgColor);
color &= 0x00FFFFFF;
color |= (((int) (0.7 * 0xFF)) << 24);
drawerLayout.setScrimColor(color);
using the helper class Color. You can easily retrieve the four components, alpha, red, green, and blue, and set it back with Color.argb, for instance. The class has only static methods, and you don't need to instantiate it. E.g.
int alpha = Color.alpha(color);
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
int newColor = Color.argb((int)(alpha * 0.7f), red, green, blue);
Here you can find the documentation
I'm working on a simple bar graph application that uses a static array of colors for divvying out bar colors. I would like the functionality to either draw bars normally, or slightly transparent.
Is there a way to programmatically adjust a color integer so that it's slightly transparent? Or will I have to statically define a transparent version of each color and then switch to using these versions whenever I want transparency?
If you are using support library, you can use:
ColorUtils.setAlphaComponent(int color, int alpha);
If you are not using support library, one-line solution taken from it's source code is:
int res = (color & 0x00ffffff) | (alpha << 24);
Sure...Look at Color and there's a function:
static int argb(int alpha, int red, int green, int blue)
Return a color-int from alpha, red, green, blue components.
So your RGB values could be static and you just bump the alpha value to get a new transparent version of the color.
Hi there you could use the:
android.support.v4.graphics.ColorUtils#setAlphaComponent
note: the alpha here is from 0 to 255 and not % based.
There are also other util methods such contract and luminosity calculations in there.
Regards
Try following code
int color = (int)Long.parseLong(your_color, 16);
int r = (color >> 16) & 0xFF;
int g = (color >> 8) & 0xFF;
int b = (color >> 0) & 0xFF;
if color code has alpha then
int alpha= (color >> 24) & 0xFF;
From the top answer I created a method to do this:
private Android.Graphics.Color AddTransparencyToColour(Android.Graphics.Color color, int transparancy)
{
return Android.Graphics.Color.Argb(transparancy, color.R, color.G, color.B);
}
It's also worth noteing that this can be changed to an extension method like so
public static ColorExtensions
{
public static Android.Graphics.Color AddTransparency(this Android.Graphics.Color color, int transparancy)
{
return Android.Graphics.Color.Argb(transparancy, color.R, color.G, color.B);
}
}
In regards to the alpha value, from MSDN Color.FromArgb:
Remarks
To create an opaque color, set alpha to 255. To create a
semitransparent color, set alpha to any value from 1 through 254.
I use extension functions.
fun Int.withAlpha(#IntRange(from = 0, to = 255) alpha: Int): Int {
return (alpha shl 24) or (this and 0xFFFFFF)
}
Also possible with ColorUtils
ColorUtils.setAlphaComponent(color, alpha)
You could create a colour helper which returns same colour with applied alpha. - Lets say you want to change visibility from 0.0 to 1.0 (double)
val originalColour: Int = primaryColor
val generatedColor = ColorUtil.generateTransparentColor(originalColour, 0.5)
view.setBackgroundColor(generatedColor)
Create a colour generator helper
object ColorUtil {
fun generateTransparentColor(color: Int, alpha: Double?): Int {
val defaultAlpha = 255 // (0 - Invisible / 255 - Max visibility)
val colorAlpha = alpha?.times(defaultAlpha)?.roundToInt() ?: defaultAlpha
return ColorUtils.setAlphaComponent(color, colorAlpha)
}
}
Those in the compose world using androidx.compose.ui.graphics.Color can just use the copy-method:
val slightlyTransparentRed = Color.Red.copy(alpha = 0.9f)
I have an image with four squares red, green, blue and yellow. I need to get the rgb values of each squares. I'm able to get the rgb of the whole image, but i want it for a specific section.
The image which i am going to get will be from the camera and stored onto the SDCard
I don't know if I understand you exactly, but here it comes.
You need to create BufferedImage object to get RGB value:
File f = new File(yourFilePath);
BufferedImage img = ImageIO.read(f);
You can get RGB Color values from the image from then. You have 4 squares; to check their RGB values, you can check the corner pixels' RGB values:
Color leftTop = new Color(img.getRGB(0, 0));
Color rightTop = new Color(img.getRGB(img.getWidth - 1, 0));
Color leftBottom = new Color(img.getRGB(0, img.getHeight - 1));
Color rightBottom = new Color(img.getRGB(img.getWidth - 1, img.getHeight - 1));
After that it's easy to get red, green and blue values individually:
int red = leftTop.getRed();
int green = leftTop.getGreen();
int blue = leftTop.getBlue();
EDIT:
I'm really sorry, I didn't see it's for Android. As you said, Android doesn't have ImageIO class. To accomplish the task in Android, first initialize the image:
Bitmap img = BitmapFactory.decodeFile(yourFilePath);
From then the operation is pretty much the same:
int leftTop = img.getPixel(0, 0);
...
int red = Color.red(pixel);
int blue = Color.blue(pixel);
int green = Color.green(pixel);
Use this to crop your image.
Now to detect the color of the image take a pixel from the square and detect it's color with this.
After finding the RGB value use a simple conditional statement to see if the square is red blue or green.
I got it this way
int topLeftIndex = squareImage.getPixel(0, 0);
int R1 = (topLeftIndex >> 16) & 0xff;
int G1 = (topLeftIndex >> 8) & 0xff;
int B1 = topLeftIndex & 0xff;
and same way with
int bottomLeftIndex=squareImage.getPixel(0, picHeight - 1);
int topRightIndex=squareImage.getPixel(picWidth -1 , 0);
int bottomRightIndex=squareImage.getPixel(picWidth -1, picHieght - 1);
I need to get the color of a pixel in order to compare it with a color from my color.xml file, but all values are negative and this comparison will always return a false result. How to get the proper color value? This color may be transparent. I've read this but I need an answer, not a link to theory.
bmp.getPixel(n.x, n.y) is returning zero when I'm expecting to return a propper value for color #00FFFFFF
Thanks
You could do something like this:
int pixel = Color.RED; //bmp.getPixel(n.x, n.y);
int a = Color.alpha(pixel);
int r = Color.red(pixel);
int g = Color.green(pixel);
int b = Color.blue(pixel);
String color = String.format("#%02X%02X%02X%02X", a, r, g, b); //#FFFF0000 for RED color
but instead of Color.RED you can put your bmp.getPixel(...) method.
Hope that helps
Best Regards