Giter Site home page Giter Site logo

msoftware / androidcolorx Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jorgecastilloprz/androidcolorx

0.0 1.0 0.0 2.79 MB

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

License: Apache License 2.0

Kotlin 100.00%

androidcolorx's Introduction

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()

As you can see, 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.

androidcolorx's People

Contributors

jorgecastilloprz avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.