GithubHelp home page GithubHelp logo

fabiospampinato / khroma Goto Github PK

View Code? Open in Web Editor NEW
36.0 3.0 5.0 242 KB

A collection of functions for manipulating CSS colors, inspired by SASS.

License: MIT License

TypeScript 43.91% JavaScript 56.09%
sass manipulation color css hex rgb hsl

khroma's Introduction

A collection of functions for manipulating CSS colors, inspired by SASS.

Features

  • Small: the entire library weighs ~5kb (min + gzip) and has no dependencies.
  • Fast: in our benchmark suite functions are executed at ~0.0025ms per call on a mid-2014 MBP.
  • Flexible: All valid CSS colors are supported.
  • SASS-like: if you're familiar with SASS you should feel right at home.

Install

npm install --save khroma

Usage

import {red, isDark, darken, change} from 'khroma';

red ( '#ffffff' ); // => 255
isDark ( 'white' ); // => false
darken ( 'hsl(0, 5%, 100%)', 50 ); // => 'hsl(0, 5%, 50%)'
change ( 'rgb(255, 255, 255)', { a: 0.5 } ); // => 'rgba(255, 255, 255, 0.5)'

Functions

These are all the provided functions, for each of them below you can find a short description, its interface and examples.

Create Convert Get channel Get more Edit channel Edit more
hex toKeyword channel contrast saturate adjust
rgb toHex red luminance desaturate change
rgba toRgba green isDark lighten invert
hsl toHsla blue isLight darken mix
hsla hue isTransparent opacify scale
saturation isValid fadeIn
lightness transparentize
alpha fadeOut
opacity rgba (alt)
complement
grayscale

Create

These functions create a new color given the provided channels.

hex

Alias for rgba.

rgb

Alias for rgba.

rgba

Creates a new color given its rgba channels, the alpha channel is optional.

function rgba ( r: string, g: number, b: number, a: number = 1 ): string;
rgba ( 255, 204, 0 ); // => '#ffcc00'
rgba ( 255, 204, 0, 0.5 ); // => 'rgba(255, 204, 0, 0.5)'

hsl

Alias for hsla.

hsla

Creates a new color given its hsla channels, the alpha channel is optional.

function hsla ( h: number, s: number, l: number, a: number = 1 ): string;
hsla ( 0, 50, 100 ); // => 'hsl(0, 50%, 100%)'
hsla ( 10, 50, 100, 0.5 ); // => 'hsla(10, 50%, 100%, 0.5)'

Convert

These functions convert supported colors to a specific format.

toKeyword

Convert a color to the keyword format, when possible.

function toKeyword ( color: string ): string | undefined;
toKeyword ( '#ff0000' ); // => 'red'
toKeyword ( '#ffcc00' ); // => undefined

toHex

Convert a color to the HEX format.

function toHex ( color: string ): string;
toHex ( 'red' ); // => '#ff0000'
toHex ( '#ff0000' ); // => '#ff0000'

toRgba

Convert a color to the RGBA format.

function toRgba ( color: string ): string;
toRgba ( 'red' ); // => 'rgb(255, 0, 0)'
toRgba ( '#ff0000' ); // => 'rgb(255, 0, 0)'
toRgba ( '#00000088' ); // => 'rgba(0, 0, 0, 0.5333333333)'

toHsla

Convert a color to the HSLA format.

function toHsla ( color: string ): string;
toHsla ( 'red' ); // => 'hsl(0, 100%, 50%)'
toHsla ( '#ff0000' ); // => 'hsl(0, 100%, 50%)'
toHsla ( 'rgb(255, 0, 0)' ); // => 'hsl(0, 100%, 50%)'

Get channel

These functions get a single channel from the provided color.

channel

Gets any single channel of the color.

function channel ( color: string, channel: 'r' | 'g' | 'b' | 'h' | 's' | 'l' | 'a' ): number;
channel ( '#ffcc00', 'r' ); // => 255
channel ( '#ffcc00', 'h' ); // => 48
channel ( '#ffcc00', 'a' ); // => 1

red

Gets the red channel of the color.

function red ( color: string ): number;
red ( '#ffcc00' ); // => 255

green

Gets the green channel of the color.

function green ( color: string ): number;
green ( '#ffcc00' ); // => 204

blue

Gets the blue channel of the color.

function blue ( color: string ): number;
blue ( '#ffcc00' ); // => 0

hue

Gets the hue channel of the color.

function hue ( color: string ): number;
hue ( 'hsl(0, 50%, 100%)' ); // => 0

saturation

Gets the saturation channel of the color.

function saturation ( color: string ): number;
saturation ( 'hsl(0, 50%, 100%)' ); // => 50

lightness

Gets the lightness channel of the color.

function lightness ( color: string ): number;
lightness ( 'hsl(0, 50%, 100%)' ); // => 100

alpha

Gets the alpha channel of the color.

function alpha ( color: string ): number;
alpha ( '#ffcc00' ); // => 1
alpha ( 'rgba(255, 205, 0, 0.5)' ); // => 0.5

opacity

Alias for alpha.

Get more

These functions get some other information from the provided color.

contrast

Gets the contrast in luminance between two colors.

Contrast values go between 1 and 10. 1 means same color, >= 4 means decent contrast, >= 7 means great contrast, 10 means great contrast.

function contrast ( color1: string, color2: string ): number;
contrast ( '#000000', '#000000' ); // => 1
contrast ( '#000000', '#ffffff' ); // => 10
contrast ( '#888888', '#ffffff' ); // => 4.0617165366

luminance

Gets the relative luminance of the color.

function luminance ( color: string ): number;
luminance ( 'black' ); // => 0
luminance ( 'white' ); // => 1
luminance ( '#ffcc00' ); // => 0.6444573127

isDark

Checks if the provided color is a dark color.

function isDark ( color: string ): number;
isDark ( 'black' ); // => true
isDark ( 'white' ); // => false
isDark ( '#ffcc00' ); // => false

isLight

Checks if the provided color is a light color.

function isLight ( color: string ): number;
isLight ( 'black' ); // => false
isLight ( 'white' ); // => true
isLight ( '#ffcc00' ); // => true

isTransparent

Checks if the provided color is a transparent color.

function isTransparent ( color: string ): boolean;
isTransparent ( 'transparent' ); // => true
isTransparent ( '#ffcc0000' ); // => true
isTransparent ( '#ffcc00' ); // => false

isValid

Checks if the provided color is a valid color.

function isLight ( color: string ): boolean;
isValid ( 'black' ); // => true
isValid ( '#ffcc00' ); // => true
isValid ( '#wtf' ); // => false

Edit channel

These functions change a single channel of the provided color.

saturate

Increases the saturation channel of the color.

function saturate ( color: string, amount: number ): string;
saturate ( 'hsl(0, 50%, 50%)', 25 ); // => 'hsl(0, 75%, 50%)'

desaturate

Decreases the saturation channel of the color.

function desaturate ( color: string, amount: number ): string;
desaturate ( 'hsl(0, 50%, 50%)', 25 ); // => 'hsl(0, 25%, 50%)'

lighten

Increases the lightness channel of the color.

function lighten ( color: string, amount: number ): string;
lighten ( 'hsl(0, 50%, 50%)', 25 ); // => 'hsl(0, 50%, 75%)'

darken

Decreases the lightness channel of the color.

function darken ( color: string, amount: number ): string;
darken ( 'hsl(0, 50%, 50%)', 25 ); // => 'hsl(0, 50%, 25%)'

opacify

Increases the opacity channel of the color.

function opacify ( color: string, amount: number ): string;
opacify ( 'rgba(255, 204, 0, 0.5)', 0.25 ); // => 'rgba(255, 204, 0, 0.75)'

fadeIn

Alias for opacify.

transparentize

Decreases the opacity channel of the color.

function transparentize ( color: string, amount: number ): string;
transparentize ( 'rgba(255, 204, 0, 0.5)', 0.25 ); // => 'rgba(255, 204, 0, 0.25)'

fadeOut

Alias for transparentize.

rgba (alt)

Sets a new value for the opacity channel.

function rgba ( color: string, amount: number ): string;
rgba ( 'rgba(255, 204, 0, 0.5)', 0.1 ); // => 'rgba(255, 204, 0, 0.1)'

complement

Gets the complement of the color, rotating its hue channel by 180 degrees.

function complement ( color: string ): string;
complement ( '#ffcc00' ); // => 'hsl(228, 100%, 50%)'

grayscale

Gets the grayscale version of the color, setting its saturation to 0.

function grayscale ( color: string ): string;
grayscale ( '#ffcc00' ); // => 'hsl(48, 0%, 50%)'

Edit more

These functions can/will change more than a single channel at once of the provided color.

adjust

Increases or decreases the value of any channel of the color.

function adjust ( color: string, channels: Record<'r' | 'g' | 'b' | 'h' | 's' | 'l' | 'a', number> ): string;
adjust ( '#ffcc00', { r: -10, g: 200 } ); // => '#f5ff00'
adjust ( '#ffcc00', { a: -0.5 } ); // => 'rgba(255, 204, 0, 0.5)'
adjust ( '#ffcc00', { h: 50, l: -30 } ); // => 'hsl(98, 100%, 20%)'

change

Sets a new value for any channel of the color.

function change ( color: string, channels: Record<'r' | 'g' | 'b' | 'h' | 's' | 'l' | 'a', number> ): string;
change ( '#ffcc00', { r: 10, g: 200 } ); // => '#0ac800'
change ( '#ffcc00', { a: 0.5 } ); // => 'rgba(255, 204, 0, 0.5)'
change ( '#ffcc00', { h: 50, l: 30 } ); // => 'hsl(50, 100%, 30%)'

invert

Gets the inverse of the color.

function invert ( color: string, weight: number = 100 ): string;
invert ( '#ffcc00' ); // => '#0033ff'
invert ( '#ffcc00', 50 ); // => '#808080'

mix

Mixes two colors together.

function mix ( color1: string, color2: string, weight: number = 50 ): string;
mix ( 'red', 'blue' ); // => '#800080'
mix ( 'red', 'blue', 15 ); // => '#2600d9'

scale

Scales any channel of the color.

function scale ( color: string, channels: Record<'r' | 'g' | 'b' | 'h' | 's' | 'l' | 'a', number> ): string;
scale ( '#ffcc00', { r: -50, b: 10 } ); // => '#80cc1a'

License

MIT © Fabio Spampinato, Andrew Maney

khroma's People

Contributors

ablelincoln avatar fabiospampinato avatar vfonic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

khroma's Issues

License reported as unknown

The license is not specified in the package.json file, therefore the package is listed as UNLICENSED (License=none) on the npm registry.

This makes the legal situation unclear and results in build failures due to unknown license constraints.

Hex alpha support

Most browsers support hex colors with an alpha channel in the format #RGBA. Should our library support this?

Logos

I've been playing around a bit in Figma trying to design a simple logo. Let me know if you like any of these (I am by no means a designer).

sasssilly
sasssans

Inconsistent percent->decimal conversion

https://github.com/fabiospampinato/sass.js/blob/778c040e6047d601e72140405aaf2480d32285fc/src/utils.ts#L28-L32

https://github.com/fabiospampinato/sass.js/blob/778c040e6047d601e72140405aaf2480d32285fc/src/utils.ts#L40-L44

I am confused on what the units should be for "percentage". Is 10% represented as 10 or 0.1? Based on the above function, one would assume that percentages are represented in their decimal format.

For instance:
per2dec(10) -> 2550 ❌ This makes no sense
per2dec(0.1) -> 26 👍

But this leads to some inconsistency with how per2dec and dec2per convert between each other.

Example:
per2dec(0.1) -> 26 dec2per(26) -> 10.2 what?

allow object oriented usage

👋 hey there I'm working on a Sass like preprocessor and was hoping to use Khroma for color logic. The issue tho is that i really need to be able to create reusable Color objects to e.g. store as the values of a variable declaration. THe problem here is that Khroma reuses the same Channels instance for all work so it doesn't seem possible to store an intermediate representation of the color channels since any other color will mutate the same data. I see why this is the case, but any chance of getting a version of the API that allows creating reusable Color instances from strings?

License missing in package

The current package.json file does not include the license attribute. This is an issue for those systems which mirror or proxy access the download of external packages from npmjs.com.
Between version 2.0.0 and 2.1.0, the file LICENSE was renamed in license. This is fine, although not the most common practice, and it is not captured by some of those system which actively look for a LICENSE file and the license attribute in the package.json. The license shown on GitHub is not captured within the package, so it's not relevant when a package is downloaded.

Thus, the package.json should correctly include the license attribute and a new version of the package should be built.

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.