How to make a color brighter - android

I have a color that I get in runtime. I need to find a way to make it a bit brighter. I found a few examples on line, none worked for me.
The only thing that made any difference to the color was changing to HSV. But I couldn't understand how it worked.

Use RGB values manually:
final int brighter = 25;
Color.rgb(Color.red(color) + brighter, Color.green(color) + brighter, Color.blue(color) + brighter);
You of course would have to make something that checks that any value does not get over 255.
EDIT Palette won't work with a single color.

Related

Create color value dynamically

My app shows buttons depending on logged user.
Details and Conditions of the app:
The number of buttons that will be shown is unknown (could be 1, 20, 100, etc.)
A different color must be set for each button
I want some control over color values, as text is always white, so text must always be readable
How can I create a dynamic color value under these conditions?
First you need to decide how "readable" the text should be. WCAG 2.1 is a common standard for accessibility requirements, and its minimum contrast requirement is 4.5:1. (The spec definition is here, or for a lighter overview with some nice examples there's this.)
That amount of contrast will guarantee your text can be read by people with "moderately low vision". 3:1 is recommended for "standard text and standard vision", but I'd always recommend going with the accessible ratio - especially since you're using white and random colours, which will vary in readability quite a bit!
WCAG 2.1 also allows for that 3:1 ratio for large-scale text, which is 18pt or 14pt bold. That works out to about 40dp for regular text and 31dp for bold. Depends on the font too, and also since you often use sp instead the user can control how big the fonts are, so it complicates things. But basically, big text = lower contrast requirements
Now you have your contrast level, you can check whether your colour combo meets it or not. There's a nice tool in ColorUtils that does this for you - it uses the WCAG formula for calculating contrast:
fun meetsMinContrast(#ColorInt foreground: Int, #ColorInt background: Int): Boolean {
val minContrast = 4.5
val actual = ColorUtils.calculateContrast(foreground, background)
return actual >= minContrast
}
As for actually generating colours, I don't know the "smart" way to do it, if there is one - you could possibly generate a colour space of valid colours when paired with white, and pick from that, but I don't really know anything about it - maybe someone else can chime in with a better solution!
So for a purely naive random approach:
val colours = generateSequence {
Color.valueOf(
Random.nextInt(0, 255),
Random.nextInt(0, 255),
Random.nextInt(0, 255)
)
}
val accessibleBackgrounds = colours.filter { background ->
meetsMinContrast(Color.WHITE, background)
}
and then you have a stream of valid, random colours you can set on your buttons.
If you don't like the "just keep generating randomly until you hit one that works" approach (which is pretty hacky and could be slow if you're unlucky), you could work with HSV instead:
fun getAccessibleBackground(): Color {
val hsv = floatArrayOf(
Random.nextFloat() * 360f,
Random.nextFloat(),
Random.nextFloat()
)
var colour: Color
while(true) {
colour = Color.HSVtoColor(hsv)
if (meetsMinContrast(Color.WHITE, colour)) return colour
// darken the colour a bit (subtract 1% value) and try again
hsv[2] -= 0.01f
}
}
(I'd be more explicit about error checking and providing a fallback there, especially if you made it work with a foreground colour parameter, but that should always return a valid colour before you need to worry about subtracting from value too much - it's just a simple example)

How to get desired color when using bitmap tint multiply?

I have a bitmap which base color is #EBEBEB, I tint it using multiply mode with color #55AABB and as a result I get #4EAC9D color but I want to have #55AABB as a result color. I've seen that Cout = Csrc * Cdst on the documentation but I'm not able to get #55AABB as a result. Could someone help me?
Here is what is going on. Although it is pretty well explained in the docs. What you need is, I think, DST_IN.

Android - Change only the alpha TextView's text

Say I have this code:
int color = tv.getCurrentTextColor();
How would I change only the Alpha on this color?
For Example:
if color is 0x00ffffff, how would I change it to 0xffffffff ?
Is there a method for that or I need to do some hex/int manipulations?
Well, the straight answer to your question of "if color is 0x00ffffff, how would I change it to 0xffffffff ?" to simply to use compound bitwise or:
color |= 0xff000000;.
But I reckon maybe some co-workers would rather you didn't do this kind of thing on the java-side of Android development: you have a wonderful helper class called graphics.Color provided by Android! It does just the same things underneath, but'll really help your code readibility, especially considering that an int type color in android isn't necessarily a hex color value but could also point to an artbirary id of a resource in the color xml. Argh.
An eg would be:
int color = tv.getCurrentTextColor();
int newColor = Color.argb(yourNewAlphaVal, Color.red(color), Color.green(color), Color.blue(color))
Not fast, but I'm guessing you don't need it to be.
Of course, this is a personal opinion about what to use here, and I hope this helps!

RemoteView background tranparency

I've been trying to use remoteView.setFloat(R.id.remote_background, "setAlpha", (float) 0.7); to set background transparency/alpha, but I'm getting "Error loading widget". I've read that you can set transparency by adding a hex value in front of a color. But since I'm getting the color from ColorPicker and it's an integer value (example: -13890612), I don't know how to use it.
You should use hex notation to see the color as it is in format RRGGBB, so your -13890612 should be rather peeked as 2C0BCC which makes more sense as you can easily say what are values of each components of the color. And to add alpha channel (which is value from 0 - (full transparent) to 0xff (full opaque)), just OR the right value with your color and use. I.e. to make it semi transparent OR 0x80:
int rgb = 0x2C0BCC;
int argb = 0x80000000 | rgb;
USe setInt(R.id.remote_background, "setBackgroundColor", colorInt); instead.

How do I use access colours and gradients on Android?

So i'm having a problem with using decimal colors in android.
I'm getting color codes from an external database.
For Example:
16777215 is white
16711680 is red
Now I want to use this colors to create a GradientDrawable.
int color = myDbReader.getColor(); //returns the decimal color code
new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, new int[] {color, 0 });
The code example produces always a completley white gradient object.
I tried to google how to convert the decimal colors in the right way.. But i didn't find anything.
Can anyone give me a hint how to use the decimal colors in the right way?
You should read about android colours.
Android colors are 32bit unsigned integers, not signed ones!
Also Android colors are using Alpha bit, please read the article and the solution will be clear.
Even if SO is extremely good source of information you should google it before asking, it was the first link on Google when I entered "colour android".
BTW, I'm not an android developer.
Android colors are ARGB units, so your color should actually consist of 4 bytes with 1 of them for the alpha value. In order to convert you can have something like this
0xFF000000 & yourColor
which will set the opacity of your color to 100%(alpha byte is set). Another option is to use Color.parseColor after converting your integer to a hexadecimal string. Also do not refer to colors by some magic constants. The static fields of the Color class are designed so that you don't have to do that.
So based on your suggestions I spent last night with reading about the different types of Color codes and finally found the solution:
The given numbers are access color codes so i only have to:
int color = myDbReader.getColor(); //returns the decimal color code
gradient = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, new int[] {Color.rgb((value/256/256)%256, (value/256)%256, value%256), 0 });
Thanks four your help!

Categories

Resources