• Stars
    star
    316
  • Rank 132,587 (Top 3 %)
  • Language
    Kotlin
  • License
    Apache License 2.0
  • Created about 5 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

AndroidColorX is a library to provide color utilities as Kotlin extension functions.

AndroidColorX CircleCI

AndroidColorX (i.e: Android Color Extensions) is an Android library written in Kotlin that provides color utilities as Kotlin extension functions. The library relies on AndroidX ColorUtils for some of its calculations.

How to use

Add the following dependency to your build.gradle. It's available in Maven Central.

dependencies {
    implementation 'me.jorgecastillo:androidcolorx:0.2.0'
}

Color conversion

This library provides seamless conversion between the following color types:

  • android.graphics.Color (ColorInt)
  • RGBColor
  • ARGBColor
  • HEXColor
  • HSLColor
  • HSLAColor
  • HSVColor
  • CMYKColor

To convert a color type to any of the other types, you can use the extensions provided for it.

val color = Color.parseColor("#e91e63")

val rgb = color.asRgb()
val argb = color.asArgb()
val hex = color.asHex()
val hsl = color.asHsl()
val hsla = color.asHsla()
val hsv = color.asHsv()
val cmyk = color.asCmyk()

val colorHsl = HSLColor(hue = 210f, saturation = 0.5f, lightness = 0.5f)

val colorInt = colorHsl.asColorInt()
val rgb = colorHsl.asRgb()
val argb = colorHsl.asArgb()
val hex = colorHsl.asHex()
val cmyk = colorHsl.asCmyk()
val hsla = colorHsl.asHsla()
val hsv = colorHsl.asHsv()

The same extensions are available for all the mentioned color types. Note that when converting from a color with an alpha component (i.e: ARGB or HSLA) to one without it (i.e: RGB or HSL) it will be a lossy conversion. If it's the other way around, conversions will assume full alpha (255 or 1f).

Also note that conversions from colors that store their components as Float (i.e: cmyk, hsl, hsla) to colors that store them as Int (i.e: RGBColor, ColorInt) and back will imply precision loss.

Shades and tints

If you're aware of webs like Color-Hex you've probably seen those shades and tints palettes. AndroidColorX provides extensions for calculating those:

Here's the code for all the color types available:

val color = Color.parseColor("#e91e63")

// shades
val shades: List<Int> = color.shades()
val shadesHsl: List<HSLColor> = color.asHsl().shades()
val shadesHsla: List<HSLAColor> = color.asHsla().shades()
val shadesHsv: List<HSVColor> = color.asHsv().shades()
val shadesCmyk: List<CMYKColor> = color.asCmyk().shades()
val shadesHex: List<HEXColor> = color.asHex().shades()
val shadesRgb: List<RGBColor> = color.asRgb().shades()
val shadesArgb: List<ARGBColor> = color.asArgb().shades()

// tints
val tints: List<Int> = color.tints()
val tintsHsl: List<HSLColor> = color.asHsl().tints()
val tintsHsla: List<HSLAColor> = color.asHsla().tints()
val tintsHsv: List<HSVColor> = color.asHsv().tints()
val tintsCmyk: List<CMYKColor> = color.asCmyk().tints()
val tintsHex: List<HEXColor> = color.asHex().tints()
val tintsRgb: List<RGBColor> = color.asRgb().tints()
val tintsArgb: List<ARGBColor> = color.asArgb().tints()

These functions calculate 10 shades or tints by default that will be returned along with the original color, but you can also generate an arbitrary amount if you need to:

val color = Color.parseColor("#e91e63")

// shades
val shades: List<Int> = color.shades(count = 3)
val shadesHsl: List<HSLColor> = color.asHsl().shades(count = 3)
...

// tints
val tints: List<Int> = color.tints(count = 15)
val tintsHsl: List<HSLColor> = color.asHsl().tints(count = 15)
...

Note that the colors will always range from 0 to 1 luminance for tinting or shading regardless of the amount of colors to generate, so the more you generate the more gradual the contrast will be between each consecutive pair of colors.

These extensions are available for all the mentioned color types.

Complimentary, triadic, tetradic and analogous colors

The library provides extensions to calculate all those. There is a brief but very useful post to understand all those here.

val color = Color.parseColor("#e91e63")

// Returns the complimentary color to the given one.
val complimentary: Int = color.complimentary()

// Returns the other 2 colors to complete the triadic color scheme.
val triadic: Pair<Int, Int> = color.triadic()

// Returns the other 3 colors to complete the tetradic color scheme.
val tetradic: Triple<Int, Int, Int> = color.tetradic()

// Returns the other 2 colors to complete the analogous color scheme.
val analogous: Pair<Int, Int> = color.analogous()

Once again, you've got those extensions available for all the color types in the library.

Darken / Lighten a color

You can darken or lighten a color by an amount:

val color = Color.parseColor("#e91e63")

// Int in the 0...100 range. Anything else is clamped to 0...100
val darkerColor = color.darken(50)
val lighterColor = color.lighten(50)

// Float in the 0...1 range. Anything else is clamped to 0...1
val darkerColor = color.darken(0.5)
val lighterColor = color.lighten(0.5)

You can use this extensions over any color type available.

Other interesting extensions

isDark

You can use this one to detect whether a color is considered "dark" or not. It delegates in the following AndroidX ColorUtils code ColorUtils.calculateLuminance(this) < 0.5. This is frequently used for inferring which color you should tint an icon or a text with, so it can have a good contrast on top of the underlying background.

icon.setImageDrawable(
    ContextCompat.getDrawable(
        context,
        if (bgColor.isDark()) {
            R.drawable.ic_fav_light
        } else {
            R.drawable.ic_fav_dark
        })
)

contrasting

The library also provides a handy extension to get a nicely contrasting color for a given color. You can use it for the same purpose and to remove a bit of boilerplate.

val color = HEXColor("#e91e63")

// Defaults to black or white, the one with better contrast
color.contrasting()

// You can pass custom light and dark colors
color.contrasting(lightColor = HEXColor("#ffffff"), darkColor = HEXColor("#000000"))

These extensions can be used over any color types provided by the library.

License

Copyright 2019 Jorge Castillo PΓ©rez

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

More Repositories

1

AndroidFillableLoaders

Android fillable progress view working with SVG paths. This is a nice option too if you want to create an interesting branding logo for your app. Based on the iOS project: https://github.com/poolqf/FillableLoaders
Java
1,996
star
2

FABProgressCircle

Material progress circle around any FloatingActionButton. 100% Guidelines.
Java
1,250
star
3

ArrowAndroidSamples

Functional Programing Android architecture using http://arrow-kt.io/
Kotlin
426
star
4

ExpandablePanel

Android library for creating an expandable to full screen view inside a viewgroup composition.
Java
422
star
5

hiroaki

Write idiomatic API integration tests using Kotlin (Unit and Instrumentation)
Kotlin
370
star
6

Dagger2Scopes

Dagger 2 sample app implementing multiple scopes. Clean arquitecture.
Java
152
star
7

AndroidKodeinSample

Android Scoped DI using Kodein. Sample repo for https://caster.io/
Kotlin
68
star
8

Corleone

Java annotation processor to dispatch and concatenate background tasks through a simple syntax.
Java
46
star
9

ScalaForTheImpatient

Project created to learn Scala basics by solving all the scala exercises proposed by the book.
Scala
27
star
10

ComposeFillableLoaders

Port of AndroidFillableLoaders library to Jetpack Compose.
Kotlin
19
star
11

ShenronAPI

Dragon Ball character rest API written in Scala using Play framework
Scala
13
star
12

JetpackComposeAndInternals

The ultimate Jetpack Compose online course. Created and delivered by Jorge Castillo.
JavaScript
13
star
13

Lifecolors

Android camera app to pick colors by clicking on the image and infer complimentary color palettes.
Kotlin
9
star
14

ComposeConstraintLayoutSamples

Backup repo for a series of posts in https://jorgecastillo.dev
Kotlin
7
star
15

GetAPet

Pet adoption Android app currently under active development.
Kotlin
6
star
16

ConwaysGameOfLife

An implementation of Conway's Game of Life in Kotlin using Arrow
Kotlin
6
star
17

FlutterFabLoader

Flutter loading spinner to wrap a FloatingActionButton.
Dart
5
star
18

android-dev-challenge-compose

Android Dev Challenge Week 2
Kotlin
4
star
19

jorgecastilloprz.github.io

Personal blog https://jorgecastillo.dev
CSS
3
star
20

AndroidLintDocs

Gather all the possible knowledge about Android Lint in a single place.
Kotlin
3
star
21

DigitalNomadWallpapers

Flutter sample app gathering digital nomad, remote work and developer desktop setup wallpapers
Dart
2
star
22

LibGDXSampleGame

Sample game playground using LibGDK
Kotlin
2
star
23

ElectronSample

Basics on how to prepare the environment to build electron apps
JavaScript
2
star
24

SupportFragmentLifecycleTests

Basic lifecycle test project to double check some lifecycle calls ordering when a support fragment is replaced.
Kotlin
1
star