Giter Club home page Giter Club logo

colormath's Introduction

Colormath

Colormath is a Kotlin Multiplatform library that allows you to convert between a number of color spaces. Colormath can also parse and render CSS colors.

Try it online

Supported color spaces

Usage

Conversion

Each color space is represented with a data class, and contains .toXXX() methods to convert to other spaces.

All color classes contain an alpha channel, which defaults to 1 (fully opaque) for color spaces that don't support transparency (such as ANSI color codes).

> RGB("#adcdef").toHSV()
HSV(h=211, s=28, v=94)

> RGB(r=12, g=128, b=255, a=.5f).toCMYK()
CMYK(c=95, m=50, y=0, k=0, a=.5f)

> HSL(180, 50, 50).toHex()
"#40bfbf"

CSS Parsing and rendering

You can parse any color allowed by the CSS Color Module Levels 1 through 4.

> Color.fromCss("#ff009980")
RGB(r=255, g=0, b=153, a=.5)

> Color.fromCss("rgb(100%, 0%, 60%)")
RGB(r=255, g=0, b=153, a=1)

> Color.fromCss("rgb(1e2, .5e1, .5e0, +.25e2%)")
RGB(r=100, g=5, b=1, a=.25)

> Color.fromCss("hsl(.75turn, 60%, 70%)")
HSL(h=270, s=60, l=70, a=1)

> Color.fromCss("rebeccapurple").toHex()
"#663399"

You can also render any color in CSS rgb or hsl functional or whitespace notation

> RGB(255, 0, 128).toCssRgb()
"rgb(255, 0, 128)"

> RGB(255, 0, 128, .5f).toCssRgb(rgbStyle=PERCENT)
"rgb(100%, 0%, 50%, .5)"

> XYZ(25.0, 50.0, 75.0, .5f).toCssHsl(commas = false, hueUnit = RADIANS)
"hsl(3.1241rad 100% 44% / .5)"

Installation

Colormath is distributed through Maven Central.

dependencies {
   implementation("com.github.ajalt.colormath:colormath:2.1.0")
}
If you're using Maven instead of Gradle, use <artifactId>colormath-jvm</artifactId>
In version 2.0, the maven coordinates changed. Make sure you're using the new coordinates if you're updating from an older version.

Multiplatform

Colormath supports the following targets: jvm, mingwX64, linuxX64, macosX64, ios, watchos, tvos, and js (for both NodeJS and Browsers). You'll need to use Gradle 6 or newer.

Snapshots

Snapshot builds are also available

You'll need to add the Sonatype snapshots repository:

repositories {
    maven {
        url = uri("https://oss.sonatype.org/content/repositories/snapshots/")
    }
}

License

Copyright 2018-2021 AJ Alt

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.

colormath's People

Contributors

ajalt avatar laurelkeys 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.