In the Android4.4 system, the details about the color are described in more detail and a new tutorial on using colour is provided to make our application more unique. In other words, as a designer or developer, making the perfect color match for your app has become your responsibility.
You can make your app more unique by changing the default blue of the Android framework interface elements. --From Android Design
The simplest way is to add a custom background to the Action Bar , but in an app I'm writing now I want to be more flexible (to be adaptive), the best example of adaptive color is itunes, which gets the color scheme from the album, A list of tracks that act on the popup.
So, I'm going to implement this technology on Android.
Basic Theoretical knowledge
After searching through the internet, I found a lot of open source implementations, but in other languages. The best version is a JavaScript library called color Thief, and I learned a lot of the knowledge needed to implement this technology, just what I needed.
Image Quantization
The first step here is to quantify the source image, in layman's terms, to reduce the variety of colors used on the image. If you like animated GIFs, you can use only 8-bit swatches, so you have a maximum of 256 colors per frame.
To do this, we need to reduce the use of color, only to use some of the main colors, then we use the default color swatch bar, and then to make some other color. Details will be presented later.
Now you need to select the quantization algorithm,Color thief uses a modified version of the MCQ (Median cut quantization) algorithm, also known as MMCQ (Modified Median Cut quantization), If you want to learn more about MMCQ, you can come here. Other more well-known quantitative techniques include neuquant and octtree.
I also found a Java MCQ implementation in the book "Principles of Digital Image processing", hosted on GitHub.
This MCQ algorithm has a lot of great features, so I decided to use it:
- It soon. It is faster than Neuquant and Octtree, which is especially important on mobile devices;
- It uses a statistical histogram internally, each color block is bound to a value, and then more convenient to sort.
Although the image quality generated by the MCQ algorithm is not the best, it is just a palette that needs to be generated, not to show the generated image, so it's good.
Processing Results
The following is the processed result, using the image in the example of Color thief . As I said before, MCQ has a statistical histogram, so we can arrange the frequency used for each color, which shows the list after the palette is sorted. Of course, this can continue to improve.
These results are somewhat different from the image generated by Color thief :
- My version chooses blue as the main color;
- Color thief selected blue, silver and green as the main color;
- Color thief did not select those shades of gray. So it needs to be improved.
Next, you can call the Mediancutquantizer object's Getquantizedcolors () method to get the palette. We can display this collection in descending order by the number of colors used and the comparison again. Unfortunately, the results show that most images are made of black and white (or similar color), which doesn't make our application more unique, so we have to consider what color to choose.
For my own application, I'm going to use the following color scheme:
- The primary color of the first position is a vivid color;
- The second main color is another bright color that distinguishes one from the first primary;
- The third main color is a strong contrast to the first and second main colors;
- A main font color, and the overall main color contrast obvious, strong readability;
- The second main font color is white or black, depending on the overall main color brightness, readability.
This article is mainly about how to choose these colors.
Main Color
Based on my needs above, I decided to use the average of the following factors:
- Vivid degree;
- Heat (popularity).
Vivid degree
This is also very simple, the first to convert the RGB color model to the HSV color model, using the Android built-in [COLOR.RGBTOHSV ()] (Https://developer.android.com/reference/android/ GRAPHICS/COLOR.HTML#RGBTOHSV (int, int, int, float[])) method can be done. If you do not understand the HSV color model can be seen here.
Simply put, this cylindrical shape represents the RGB color model, which represents the color by three coordinates: Hue,saturation and value (lightness).
HSV color model, from Wikipedia
I use a simple way to calculate the vividness, through the saturation (saturation) and chroma (value). In the eyes of the human eye, the higher the two values, the higher the brightness.
- Public float[] GETHSV () {
- float[] HSV = new float[3];
- COLOR.RGBTOHSV (R, G, B, HSV);
- return HSV;
- }
- Public Float calculatecolorfulness () {
- float[] HSV = GETHSV ();
- return hsv[1] * hsv[2];
- }
The results of the calculation will be within the range of 0.0 to 1.0.
Heat
Do you remember saying that each color has a bound value? You can use this value to determine how popular a color is in a palette. Remember that the worth range is between 0.0 and 1.0.
That means we get the following simple palette:
| Color | Count |----------------------| White | | | Purple | 175 | | Black | | | Red | | | Orange | | | Blue | |----------------------| Total | |
We can calculate the proportion of the color occupancy by this ratio in the image, with 800 pixels, in the case of purple, the color ratio is: 175/800 = ~0.22. But this value is very small, only close to 1.
Instead, we can choose the most popular color in the palette as a benchmark to calculate this scale. As an example, White is the most popular color, so the purple ratio is: 175/200 = 0.87. This is more representative than the popularity of color.
Final Value
Here to use these values to combine into a final value, so simple synthesis no problem, but before said black and white is the most popular color, considering this, here we do a weight, to determine the importance of some attributes, in this case we improved the brightness:
- Static float weightedaverage (float ... values) {
- assert values.length% 2 = = 0;
- float sum = 0;
- float sumweight = 0;
- For (int i = 0; i = secondary_min_diff_hue_primary) {
- return candidate;
- }
- }
- If we get here, just return the second weighted color
- return mweightedpalette[1];
Third main color
This color is similar to the second main color above, but this time we will not find hue value, we can directly compare the first two colors.
- Contrast values is in the range 0-255.
- Private static final int tertiary_min_contrast_primary = 20;
- Private static final int tertiary_min_contrast_secondary = ;
- ...
- //Find The first color which have sufficient contrast from both the primary & secondary
- for (Colornode Color:mweightedpalette) {
- if (colorutils.calculatecontrast (color, primary)
- >= tertiary_min_contrast_primary
- && colorutils.calculatecontrast (color, secondary)
- >= tertiary_min_contrast_secondary) {
- return Color.getrgb ();
- }
- }
- We couldn ' t find a colour. In this case use the primary colour, modifying it ' s
- Brightness by 45%
- Return colorutils.changebrightness (Secondary.getrgb (), 0.45f);
Take a look,calculatecontrast () where did this method come from? This I also thought for a long time, actually it comes from this article color contrast.
Finally, I converted the RGB color model into a YIQ color model, only carrying the Y (luminance) value, then you can compare the brightness values of the two colors, see what is different in the brightness, and then use the threshold to try.
- /**
- * @return difference in Luma. Possible values is 0 (no difference) to
- * 255 (max difference).
- */
- Private static final int calculatecontrast (int rgbColor1, int rgbColor2) {
- return Math.Abs (Calculateyiqluma (RgbColor1)-Calculateyiqluma (RgbColor2));
- }
- /**
- * @return Luma value. Values is in the range 0-255.
- */
- Public static final int calculateyiqluma (int color) {
- return (299 * color.red (color) + 587 * Color.green (color) + * Color.green (color))/ ;
- }
Code
I said to send the code, look here:
Https://gist.github.com/chrisbanes/ba8e7b9ec0e40f6949c6
This code may not run, so it needs to be modified and then included in your app just to let you know how to integrate it into the app, all the important things are here, you just have to think about how to integrate into your app. Come on.
Original link: Banes translation: Bole online-Chris
Link: http://blog.jobbole.com/64715/
About the implementation of adaptive color for Android color matching