Colours is a port of the Colours Library for iOS made by my good friend Ben Gordon. You can find that project here.
Colours is available in Maven Central under the groupId
com.github.matthewyork and the artifactId
Incorporate Colours via Gradle with:
- Import the Colours library into your workspace, found in the ColoursLibrary Folder.
- Right-click on your android project folder and click on the project properties.
- Click on the "Android" tab and then "Add" under the Library section.
- Select the ColoursLibrary project to link it with your project
Note all of the colors in ActionBarCompat included Holo Colors.
Using Predefined Colors
Colours works exactly like the predefined Android colors. In fact, the Colour class is a subclass of android.graphics.Color, so you can actually use the
Colour class where you normally use
Color to gain access to the cool new methods of the Colour Library without losing any methods in the
To use your HUGE new palette of colors in XML, reference a color just as you would a color in a local Color.xml resource file:
<View . . . android:background="@color/seafoamColor" />
Huzzah! Colours automagically integrates all of its colors to your project, just as if you had defined them yourself. (You can tell all your friends that you made them. We won't tell!)
Let's say, however, that you would like to set the color of something in code. Colours has you covered. Every single color available in XML is also avalable as a static method, much like the android system colors. To retrieve a predefined color's int representation, simply call it's corresponding method:
int seashellColor = Colour.seashellColor();
Android comes pre-baked with RGB and HSV color space methods. However, this may not be enough. This library adds CMYK, which is normally used for printing, and CIE_LAB, a color space meant for modeling an equal space between each color. You can access these methods like so:
float cmyk = Colour.colorToCMYK(inputColor); int color = Colour.CMYKToColor(cmyk); float cie_lab = Colour.colorToCIE_LAB(inputColor); int color = Colour.CIE_LABToColor(cie_lab);
Color Helper Methods
Beyond giving you a list of a ton of colors with no effort, this category also gives you some methods that allow different color manipulations and translations. Here's how you use these:
Generating white or black that contrasts with a Color
A lot of times you may want to put text on top of a view that is a certain color, and you want to be sure that it will look good on top of it. With this method you will return either white or black, depending on the how well each of them contrast on top of it. Here's how you use this:
int contrastingColor = Colour.blackOrWhiteContrastingColor(inputColor)
Generating a complementary color
This method will create a color int that is the exact opposite color from another color int on the color wheel. The same saturation and brightness are preserved, only the hue is changed.
int complementaryColor = Colour.complementaryColor(inputColor);
Distance between 2 Colors
Detecting a difference in two colors is not as trivial as it sounds. One's first instinct is to go for a difference in RGB values, leaving you with a sum of the differences of each point. It looks great! Until you actually start comparing colors. Why do these two reds have a different distance than these two blues in real life vs computationally? Human visual perception is next in the line of things between a color and your brain. Some colors are just perceived to have larger variants inside of their respective areas than others, so we need a way to model this human variable to colors. Enter CIELAB. This color formulation is supposed to be this model. So now we need to standardize a unit of distance between any two colors that works independent of how humans visually perceive that distance. Enter CIE76,94,2000. These are methods that use user-tested data and other mathematically and statistically significant correlations to output this info. You can read the wiki articles below to get a better understanding historically of how we moved to newer and better color distance formulas, and what their respective pros/cons are.
double distance = Colour.distanceBetweenColorsWithFormula(colorA, colorB, ColorDistanceFormulaCIE94); boolean isNoticablySimilar = distance < threshold;
Generating Color Schemes
You can create a 5-color scheme based off of a color using the following method. It takes in a color int and one of the ColorSchemeTypes defined in Colours. It returns an int of 4 new colors to create a pretty nice color scheme that complements the root color you passed in.
int complementaryColors = Colour.colorSchemeOfType(inputColor, ColorScheme.ColorSchemeComplementary);
Here are the different examples starting with a color scheme based off of
- Matthew York - Author of Colours for Android
- Ben Gordon - Author of original iOS version of Colours
- Aaron Fleshner - Teaching me Android and being awesome like that. (Also adding holo to the mix)
I would also like to thank God through whom all things live and move and have their being. Acts 17:28
The MIT License (MIT)
Copyright (c) 2014 Matthew York
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.