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.
Related
I'm reading this article about material design. In the list of colors, there is a number next to each color that seems to darken the color as its value goes up.
What does this number means, more precisely?
Edit: As all the answers are about the hex values, I'm adding this edit to clarify the question. My question is about the left hand side numbers like 700, 500, ... not the hex numbers (#3f51b5, ...)
Edit 2: In RGB model, each of the Red, Green or Blue can have a value in scale of 0 - 255. 0 means lack of the color and 255 means the color exists in full power. Is there a numerical meaning for the left hand side numbers? Can I calculate the '700' of a color, assuming '500' of it is #3F51B5? Or these numbers are just name for different shades of color in a palette?
Those values are the relative lightness/darkness or "tint" of the color, where 50 is lightest and 900 is darkest. The Material Design guidelines suggest using the 500 tint as your primary color and the 700 tint as the darker status bar color.
The Annn values are if you're using the color as your accent color.
See https://www.google.com/design/spec/style/color.html#color-ui-color-application
The other answers are correct as well, but I think you are asking about the left hand side numbers. You can use these to specify your theme colors in Angular-Material.
$mdThemingProvider.theme('default')
.primaryPalette('purple', {
'default': '700', // by default use shade from the palette for primary intentions
'hue-1': 'A400', // use shade for the <code>md-hue-1</code> class
'hue-2': '600', // use shade for the <code>md-hue-2</code> class
'hue-3': 'A100' // use shade for the <code>md-hue-3</code> class
})
// If you specify less than all of the keys, it will inherit from the default shades
.accentPalette('deep-purple', {
'default': '200' // use shade 200 for default, and keep all other shades the same
})
The numbers you see in use, correspond the left hand side numbers to set up colors. My site is using variations of the purple theme in this example, and I can set the hue's different from what the Google settings were.
The numbers refer to the darkness of a shade variant (inverse of HSL lightness). The numbers use a scale of 0 to 1000, where 0 is white and 1000 is black.
From the Android documentation for R.color:
system_accent1_0
Lightest shade of the accent color used by the system. White.
system_accent1_10
Shade of the accent system color at 99% lightness.
system_accent1_100
Shade of the accent system color at 90% lightness.
And so on.
The general formula is shadeVariant = 1000 - (lightness * 1000).
(The one curious exception is that the 500 shade variant uses 49% lightness instead of 50%, but this is probably an implementation detail that could be ignored when re-implementing.)
Knowing the formula should additionally make it easy to calculate these values directly. For example, using Polished, you would be able to setLightness(accent, 0.9) to calculate the 100 shade variant of an accent colour yourself in a Node.js app. From there it would be easy to build a utility function that can generate any variant of any colour.
I found some information in this angular.io guide to theming:
In Material Design, each hues in a palette has an identifier number. These identifier numbers include 50, and then each 100 value between 100 and 900. The numbers order hues within a palette from lightest to darkest.
There you have it, the answer to your question: Those numbers are just static identifiers.
As an example of how they can be used, this guide to "Reading hues from palettes" states:
You can use the get-color-from-palette function to get specific hues from a palette by their number identifier.
#use '~#angular/material' as mat;
$my-palette: mat.define-palette(mat.$indigo-palette);
.my-custom-style {
background: mat.get-color-from-palette($my-palette, '500');
color: mat.get-color-from-palette($my-palette, '500-contrast');
}
The number which you are seeing is the HEX (hexadecimal) values for the color tone and the color. You can use it in CSS files instead of writing i.e. black, white or blue.
From WIKI:
"A hex triplet is a six-digit, three-byte hexadecimal number used in HTML, CSS, SVG, and other computing applications to represent colors. The bytes represent the red, green and blue components of the color. One byte represents a number in the range 00 to FF (in hexadecimal notation), or 0 to 255 in decimal notation"
More about it here https://en.wikipedia.org/wiki/Web_colors
The number is codes given to each and all colorssupported by the system. Eachh color code contains symbol "#" and 6 letters or numbers. These numbers are in hexadecimal numeral system. For example "FF" in hexadecimal represents number 255 in Decimal.
Meaning of symbols:
The first two symbols in HTML color code represents the intensity of red color. 00 is the least and FF is the most intense. The third and fourth represents intensity of green and fifth and sixth represents the intensity of blue. So with combining the intensity of red, green and blue we can mix almost any color that our heart desire.
Examples:
#FF0000: With this HTML code we tell browser to show maximum of red and no green and no blue. The result is of course pure red color.
#00FF00 - This results in pure green.
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!
Given:
an #color specified in RGB, no alpha
a png that is filled with pixels at that same RGB value, no alpha
a layout with the color as background and containing the image
Will the rendering always - on a single given device - be flawless? Or is there a possibility of some color difference between the image and the background?
Yes. The color rendered on a device for an image filled with a given RGB value and a layout with the same value specified will be the same. That is, you will not notice a difference between your image and the layout color.
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!
I'm trying to figure out one simple thing: how to set a background color in Android view. Here is the code in an Activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View v = new View(this);
setContentView(v);
v.setBackgroundColor(23423425);
}
and all I get is black screen.
The integer you set is easier represented as a hex value. The hex values are 0xAARRGGBB.
A - represents the Alpha value which is how transparent the color is. A value of FF means it's not transparent at all. A value of 00 means the color won't be shown at all and everything behind it will be visible.
R - Red value; self-explanatory
G - Green value; self-explanatory
B - Blue value; self-explanatory
What you entered in hex is 0x016569C1 which has an Alpha values of 1 (barely visible). Put, 0xFFFF0000 and you'll have a red background.
You are passing in the color incorrectly. DeeV got to it before me but you need to use a hex value.
Here is a link that lists all combinations for easy access.
Colors for Android
You can also set in the XML by using
android:background = "#FF00000000"
Which would be black.
Common way to represent color in ARGB(sometimes RGBA but it is just a naming) model is hexadecimal. No one uses decimal numeral system to represent color digitally.
let's set yellow color to button's text: button.setTextColor(0xFFFFFF00);. Now We set yellow to out button's text.
ARGB consists of 4 cannel. each with 8-bit. first channel is alfa - 0xFFFFFFFF; alfa is opacity level(in this case we have max value of it). second is red - 0xFFFFFF00, and so on; green and blue respectively.
The easiest way to create color in ARGB color model with decimal numeral system is to use Color class.
Color class has all basic static functions and fields.
In your case you can use static function Color.rgb(int red, int, green, int blue) where red, green, blue must be in the range of 0 to 255. Alfa bits by default is set to max - 255 or in hex - 0xff.
Now you know how to represent color in hexadecimal numeric system it will be very easy to create color in xml resource file.