Giter VIP home page Giter VIP logo

tinycolor's Introduction

tinycolor

npm coverage bundlesize

TinyColor is a small library for color manipulation and conversion

A fork of tinycolor2 by Brian Grinstead

DEMO: https://tinycolor.vercel.app

Changes from tinycolor2

  • reformatted into TypeScript / es2015 and requires node >= 8
    • tree shakeable "module" export and no package sideEffects
  • tinycolor is now exported as a class called TinyColor
  • default export removed, use import { TinyColor } from '@ctrl/tinycolor'
  • new random, an implementation of randomColor by David Merfield that returns a TinyColor object
  • several functions moved out of the tinycolor class and are no longer TinyColor.<function>
    • readability, fromRatio moved out
    • random moved out and renamed to legacyRandom
    • toFilter has been moved out and renamed to toMsFilter
  • mix, equals use the current TinyColor object as the first parameter
  • added polyad colors tinycolor PR 126
  • color wheel values (360) are allowed to over or under-spin and still return valid colors tinycolor PR 108
  • added tint() and shade() tinycolor PR 159
  • isValid, format are now propertys instead of a function

Install

npm install @ctrl/tinycolor

Use

import { TinyColor } from '@ctrl/tinycolor';
const color = new TinyColor('red').toHexString(); // '#ff0000'

Accepted String Input

The string parsing is very permissive. It is meant to make typing a color as input as easy as possible. All commas, percentages, parenthesis are optional, and most input allow either 0-1, 0%-100%, or 0-n (where n is either 100, 255, or 360 depending on the value).

HSL and HSV both require either 0%-100% or 0-1 for the S/L/V properties. The H (hue) can have values between 0%-100% or 0-360.

RGB input requires either 0-255 or 0%-100%.

If you call tinycolor.fromRatio, RGB and Hue input can also accept 0-1.

Here are some examples of string input:

Hex, 8-digit (RGBA) Hex

new TinyColor('#000');
new TinyColor('000');
new TinyColor('#369C');
new TinyColor('369C');
new TinyColor('#f0f0f6');
new TinyColor('f0f0f6');
new TinyColor('#f0f0f688');
new TinyColor('f0f0f688');

RGB, RGBA

new TinyColor('rgb (255, 0, 0)');
new TinyColor('rgb 255 0 0');
new TinyColor('rgba (255, 0, 0, .5)');
new TinyColor({ r: 255, g: 0, b: 0 });

import { fromRatio } from '@ctrl/tinycolor';
fromRatio({ r: 1, g: 0, b: 0 });
fromRatio({ r: 0.5, g: 0.5, b: 0.5 });

HSL, HSLA

new TinyColor('hsl(0, 100%, 50%)');
new TinyColor('hsla(0, 100%, 50%, .5)');
new TinyColor('hsl(0, 100%, 50%)');
new TinyColor('hsl 0 1.0 0.5');
new TinyColor({ h: 0, s: 1, l: 0.5 });

HSV, HSVA

new TinyColor('hsv(0, 100%, 100%)');
new TinyColor('hsva(0, 100%, 100%, .5)');
new TinyColor('hsv (0 100% 100%)');
new TinyColor('hsv 0 1 1');
new TinyColor({ h: 0, s: 100, v: 100 });

CMYK

new TinyColor('cmyk(0, 25, 20, 0)');
new TinyColor('cmyk(0, 100, 100, 0)');
new TinyColor('cmyk 100 0 100 0)');
new TinyColor({c: 0, m: 25, y: 25, k: 0});

Named

new TinyColor('RED');
new TinyColor('blanchedalmond');
new TinyColor('darkblue');

Number

new TinyColor(0x0);
new TinyColor(0xaabbcc);

Accepted Object Input

If you are calling this from code, you may want to use object input. Here are some examples of the different types of accepted object inputs:

{ r: 255, g: 0, b: 0 }
{ r: 255, g: 0, b: 0, a: .5 }
{ h: 0, s: 100, l: 50 }
{ h: 0, s: 100, v: 100 }

Properties

originalInput

The original input passed into the constructer used to create the tinycolor instance

const color = new TinyColor('red');
color.originalInput; // "red"
color = new TinyColor({ r: 255, g: 255, b: 255 });
color.originalInput; // "{r: 255, g: 255, b: 255}"

format

Returns the format used to create the tinycolor instance

const color = new TinyColor('red');
color.format; // "name"
color = new TinyColor({ r: 255, g: 255, b: 255 });
color.format; // "rgb"

isValid

A boolean indicating whether the color was successfully parsed. Note: if the color is not valid then it will act like black when being used with other methods.

const color1 = new TinyColor('red');
color1.isValid; // true
color1.toHexString(); // "#ff0000"

const color2 = new TinyColor('not a color');
color2.isValid; // false
color2.toString(); // "#000000"

Methods

getBrightness

Returns the perceived brightness of a color, from 0-255, as defined by Web Content Accessibility Guidelines (Version 1.0).

const color1 = new TinyColor('#fff');
color1.getBrightness(); // 255

const color2 = new TinyColor('#000');
color2.getBrightness(); // 0

isLight

Return a boolean indicating whether the color's perceived brightness is light.

const color1 = new TinyColor('#fff');
color1.isLight(); // true

const color2 = new TinyColor('#000');
color2.isLight(); // false

isDark

Return a boolean indicating whether the color's perceived brightness is dark.

const color1 = new TinyColor('#fff');
color1.isDark(); // false

const color2 = new TinyColor('#000');
color2.isDark(); // true

getLuminance

Returns the perceived luminance of a color, from 0-1 as defined by Web Content Accessibility Guidelines (Version 2.0).

const color1 = new TinyColor('#fff');
color1.getLuminance(); // 1

const color2 = new TinyColor('#000');
color2.getLuminance(); // 0

getAlpha

Returns the alpha value of a color, from 0-1.

const color1 = new TinyColor('rgba(255, 0, 0, .5)');
color1.getAlpha(); // 0.5

const color2 = new TinyColor('rgb(255, 0, 0)');
color2.getAlpha(); // 1

const color3 = new TinyColor('transparent');
color3.getAlpha(); // 0

setAlpha

Sets the alpha value on a current color. Accepted range is in between 0-1.

const color = new TinyColor('red');
color.getAlpha(); // 1
color.setAlpha(0.5);
color.getAlpha(); // .5
color.toRgbString(); // "rgba(255, 0, 0, .5)"

onBackground

Compute how the color would appear on a background. When the color is fully transparent (i.e. getAlpha() == 0), the result will be the background color. When the color is not transparent at all (i.e. getAlpha() == 1), the result will be the color itself. Otherwise you will get a computed result.

const color = new TinyColor('rgba(255, 0, 0, .5)');
const computedColor = color.onBackground('rgb(0, 0, 255)');
computedColor.toRgbString(); // "rgb(128, 0, 128)"

String Representations

The following methods will return a property for the alpha value, which can be ignored: toHsv, toHsl, toRgb

toHsv

const color = new TinyColor('red');
color.toHsv(); // { h: 0, s: 1, v: 1, a: 1 }

toHsvString

const color = new TinyColor('red');
color.toHsvString(); // "hsv(0, 100%, 100%)"
color.setAlpha(0.5);
color.toHsvString(); // "hsva(0, 100%, 100%, 0.5)"

toHsl

const color = new TinyColor('red');
color.toHsl(); // { h: 0, s: 1, l: 0.5, a: 1 }

toHslString

const color = new TinyColor('red');
color.toHslString(); // "hsl(0, 100%, 50%)"
color.setAlpha(0.5);
color.toHslString(); // "hsla(0, 100%, 50%, 0.5)"

toCmykString

const color = new TinyColor('red');
color.toCmykString(); // "cmyk(0, 100, 100, 0)"

toNumber

new TinyColor('#aabbcc').toNumber() === 0xaabbcc // true
new TinyColor('rgb(1, 1, 1)').toNumber() === (1 << 16) + (1 << 8) + 1 // true

toHex

const color = new TinyColor('red');
color.toHex(); // "ff0000"

toHexString

const color = new TinyColor('red');
color.toHexString(); // "#ff0000"

toHex8

const color = new TinyColor('red');
color.toHex8(); // "ff0000ff"

toHex8String

const color = new TinyColor('red');
color.toHex8String(); // "#ff0000ff"

toHexShortString

const color1 = new TinyColor('#ff000000');
color1.toHexShortString(); // "#ff000000"
color1.toHexShortString(true); // "#f000"

const color2 = new TinyColor('#ff0000ff');
color2.toHexShortString(); // "#ff0000"
color2.toHexShortString(true); // "#f00"

toRgb

const color = new TinyColor('red');
color.toRgb(); // { r: 255, g: 0, b: 0, a: 1 }

toRgbString

const color = new TinyColor('red');
color.toRgbString(); // "rgb(255, 0, 0)"
color.setAlpha(0.5);
color.toRgbString(); // "rgba(255, 0, 0, 0.5)"

toPercentageRgb

const color = new TinyColor('red');
color.toPercentageRgb(); // { r: "100%", g: "0%", b: "0%", a: 1 }

toPercentageRgbString

const color = new TinyColor('red');
color.toPercentageRgbString(); // "rgb(100%, 0%, 0%)"
color.setAlpha(0.5);
color.toPercentageRgbString(); // "rgba(100%, 0%, 0%, 0.5)"

toName

const color = new TinyColor('red');
color.toName(); // "red"

toFilter

import { toMsFilter } from '@ctrl/tinycolor';
toMsFilter('red', 'blue'); // 'progid:DXImageTransform.Microsoft.gradient(startColorstr=#ffff0000,endColorstr=#ff0000ff)'

toString

Print to a string, depending on the input format. You can also override this by passing one of "rgb", "prgb", "hex6", "hex3", "hex8", "name", "hsl", "hsv" into the function.

const color1 = new TinyColor('red');
color1.toString(); // "red"
color1.toString('hsv'); // "hsv(0, 100%, 100%)"

const color2 = new TinyColor('rgb(255, 0, 0)');
color2.toString(); // "rgb(255, 0, 0)"
color2.setAlpha(0.5);
color2.toString(); // "rgba(255, 0, 0, 0.5)"

Color Modification

These methods manipulate the current color, and return it for chaining. For instance:

new TinyColor('red')
  .lighten()
  .desaturate()
  .toHexString(); // '#f53d3d'

lighten

lighten: function(amount = 10) -> TinyColor. Lighten the color a given amount, from 0 to 100. Providing 100 will always return white.

new TinyColor('#f00').lighten().toString(); // '#ff3333'
new TinyColor('#f00').lighten(100).toString(); // '#ffffff'

brighten

brighten: function(amount = 10) -> TinyColor. Brighten the color a given amount, from 0 to 100.

new TinyColor('#f00').brighten().toString(); // '#ff1919'

darken

darken: function(amount = 10) -> TinyColor. Darken the color a given amount, from 0 to 100. Providing 100 will always return black.

new TinyColor('#f00').darken().toString(); // '#cc0000'
new TinyColor('#f00').darken(100).toString(); // '#000000'

tint

Mix the color with pure white, from 0 to 100. Providing 0 will do nothing, providing 100 will always return white.

new TinyColor('#f00').tint().toString(); // "#ff1a1a"
new TinyColor('#f00').tint(100).toString(); // "#ffffff"

shade

Mix the color with pure black, from 0 to 100. Providing 0 will do nothing, providing 100 will always return black.

new TinyColor('#f00').shade().toString(); // "#e60000"
new TinyColor('#f00').shade(100).toString(); // "#000000"

desaturate

desaturate: function(amount = 10) -> TinyColor. Desaturate the color a given amount, from 0 to 100. Providing 100 will is the same as calling greyscale.

new TinyColor('#f00').desaturate().toString(); // "#f20d0d"
new TinyColor('#f00').desaturate(100).toString(); // "#808080"

saturate

saturate: function(amount = 10) -> TinyColor. Saturate the color a given amount, from 0 to 100.

new TinyColor('hsl(0, 10%, 50%)').saturate().toString(); // "hsl(0, 20%, 50%)"

greyscale

greyscale: function() -> TinyColor. Completely desaturates a color into greyscale. Same as calling desaturate(100).

new TinyColor('#f00').greyscale().toString(); // "#808080"

spin

spin: function(amount = 0) -> TinyColor. Spin the hue a given amount, from -360 to 360. Calling with 0, 360, or -360 will do nothing (since it sets the hue back to what it was before).

new TinyColor('#f00').spin(180).toString(); // "#00ffff"
new TinyColor('#f00').spin(-90).toString(); // "#7f00ff"
new TinyColor('#f00').spin(90).toString(); // "#80ff00"

// spin(0) and spin(360) do nothing
new TinyColor('#f00').spin(0).toString(); // "#ff0000"
new TinyColor('#f00').spin(360).toString(); // "#ff0000"

mix

mix: function(amount = 50) => TinyColor. Mix the current color a given amount with another color, from 0 to 100. 0 means no mixing (return current color).

let color1 = new TinyColor('#f0f');
let color2 = new TinyColor('#0f0');

color1.mix(color2).toHexString(); // #808080

Color Combinations

Combination functions return an array of TinyColor objects unless otherwise noted.

analogous

analogous: function(results = 6, slices = 30) -> array<TinyColor>.

const colors = new TinyColor('#f00').analogous();
colors.map(t => t.toHexString()); // [ "#ff0000", "#ff0066", "#ff0033", "#ff0000", "#ff3300", "#ff6600" ]

monochromatic

monochromatic: function(, results = 6) -> array<TinyColor>.

const colors = new TinyColor('#f00').monochromatic();
colors.map(t => t.toHexString()); // [ "#ff0000", "#2a0000", "#550000", "#800000", "#aa0000", "#d40000" ]

splitcomplement

splitcomplement: function() -> array<TinyColor>.

const colors = new TinyColor('#f00').splitcomplement();
colors.map(t => t.toHexString()); // [ "#ff0000", "#ccff00", "#0066ff" ]

triad

triad: function() -> array<TinyColor>. Alias for polyad(3).

const colors = new TinyColor('#f00').triad();
colors.map(t => t.toHexString()); // [ "#ff0000", "#00ff00", "#0000ff" ]

tetrad

tetrad: function() -> array<TinyColor>. Alias for polyad(4).

const colors = new TinyColor('#f00').tetrad();
colors.map(t => t.toHexString()); // [ "#ff0000", "#80ff00", "#00ffff", "#7f00ff" ]

polyad

polyad: function(number) -> array<TinyColor>.

const colors = new TinyColor('#f00').polyad(4);
colors.map(t => t.toHexString()); // [ "#ff0000", "#80ff00", "#00ffff", "#7f00ff" ]

complement

complement: function() -> TinyColor.

new TinyColor('#f00').complement().toHexString(); // "#00ffff"

Color Utilities

equals

let color1 = new TinyColor('red');
let color2 = new TinyColor('#f00');

color1.equals(color2); // true

random

Returns a random TinyColor object. This is an implementation of randomColor by David Merfield. The difference input parsing and output formatting are handled by TinyColor.

You can pass an options object to influence the type of color it produces. The options object accepts the following properties:

  • hue – Controls the hue of the generated color. You can pass a string representing a color name: red, orange, yellow, green, blue, purple, pink and monochrome are currently supported. If you pass a hexidecimal color string such as #00FFFF, its hue value will be extracted and used to generate colors.
  • luminosity – Controls the luminosity of the generated color. You can specify a string containing bright, light or dark.
  • count – An integer which specifies the number of colors to generate.
  • seed – An integer which when passed will cause randomColor to return the same color each time.
  • alpha – A decimal between 0 and 1. Only relevant when using a format with an alpha channel (rgba and hsla). Defaults to a random value.
import { random } from '@ctrl/tinycolor';
// Returns a TinyColor for an attractive color
random();

// Returns an array of ten green colors
random({
  count: 10,
  hue: 'green',
});

// Returns a TinyColor object in a light blue
random({
  luminosity: 'light',
  hue: 'blue',
});

// Returns a TinyColor object in a 'truly random' color
random({
  luminosity: 'random',
  hue: 'random',
});

// Returns a dark RGB color with specified alpha
random({
  luminosity: 'dark',
  alpha: 0.5,
});

Readability

TinyColor assesses readability based on the Web Content Accessibility Guidelines (Version 2.0).

readability

readability: function(TinyColor, TinyColor) -> number. Returns the contrast ratio between two colors.

import { readability } from '@ctrl/tinycolor';
readability('#000', '#000'); // 1
readability('#000', '#111'); // 1.1121078324840545
readability('#000', '#fff'); // 21

Use the values in your own calculations, or use one of the convenience functions below.

isReadable

isReadable: function(TinyColor, TinyColor, Object) -> Boolean. Ensure that foreground and background color combinations meet WCAG guidelines. Object is optional, defaulting to {level: "AA",size: "small"}. level can be "AA" or "AAA" and size can be "small" or "large".

Here are links to read more about the AA and AAA requirements.

import { isReadable } from '@ctrl/tinycolor';
isReadable("#000", "#111"); // false
isReadable("#ff0088", "#5c1a72", { level: "AA", size: "small" }); // false
isReadable("#ff0088", "#5c1a72", { level: "AA", size: "large" }), // true

mostReadable

mostReadable: function(TinyColor, [TinyColor, TinyColor ...], Object) -> Boolean. Given a base color and a list of possible foreground or background colors for that base, returns the most readable color. If none of the colors in the list is readable, mostReadable will return the better of black or white if includeFallbackColors:true.

import { mostReadable } from '@ctrl/tinycolor';
mostReadable('#000', ['#f00', '#0f0', '#00f']).toHexString(); // "#00ff00"
mostReadable('#123', ['#124', '#125'], { includeFallbackColors: false }).toHexString(); // "#112255"
mostReadable('#123', ['#124', '#125'], { includeFallbackColors: true }).toHexString(); // "#ffffff"
mostReadable('#ff0088', ['#2e0c3a'], {
  includeFallbackColors: true,
  level: 'AAA',
  size: 'large',
}).toHexString(); // "#2e0c3a",
mostReadable('#ff0088', ['#2e0c3a'], {
  includeFallbackColors: true,
  level: 'AAA',
  size: 'small',
}).toHexString(); // "#000000",

See index.html in the project for a demo.

Common operations

clone

clone: function() -> TinyColor. Instantiate a new TinyColor object with the same color. Any changes to the new one won't affect the old one.

const color1 = new TinyColor('#F00');
const color2 = color1.clone();
color2.setAlpha(0.5);

color1.toString(); // "#ff0000"
color2.toString(); // "rgba(255, 0, 0, 0.5)"

tinycolor's People

Contributors

07akioni avatar afc163 avatar ariasuni avatar bluwy avatar chandlervs avatar dependabot[bot] avatar jogibear9988 avatar jungzl avatar michaellocher avatar mikaello avatar renovate-bot avatar renovate[bot] avatar scttcper avatar songkeys avatar vandercloak 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  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  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  avatar  avatar  avatar  avatar  avatar

tinycolor's Issues

Idea: toFloat or toNumber method

Numbers can be faster than strings, f.e. when working with the and passing them into WebGL.

It'd be nice to have a method like toFloat, which converts to the numerical equivalent of hex values. F.e. #ffffff would be the same as the JS number 0xffffff. An optional parameter might also allow the value to be represented as a number between 0 and 1 (common in WebGL). 1 would be equivalent to 0xffffff.

The automated release is failing 🚨

🚨 The automated release from the master branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you could benefit from your bug fixes and new features.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can resolve this 💪.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit the master branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here is some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


The push permission to the Git repository is required.

semantic-release cannot push the version tag to the branch master on remote Git repository.

Please refer to the authentication configuration documentation to configure the Git credentials on your CI environment.


Good luck with your project ✨

Your semantic-release bot 📦🚀

webpack can't find import entry

import { inputToRGB } from '@ctrl/tinycolor';

then inputToRGB is undefined. because it resolved to @ctrl/tinycolor/dist/index.js

but if i change like this it can work.

import { inputToRGB } from '@ctrl/tinycolor/dist/public_api.js';

seem like package.json entry not working here, but others package is fine, only this package has the problem.

my dependency chain is : @ant-design/[email protected] -> @ant-design/[email protected] -> @ctrl/[email protected]

Running error on AWS Elastic Beanstalk

The latest version 3.4.0 cause a deployment error on our AWS Elastic Beanstalk server. It makes server unable to start. Error Log:

UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 24) /var/app/current/node_modules/@ctrl/tinycolor/dist/module/conversion.js:1 import { bound01, pad2 } from './util'; ^^^^^^ SyntaxError: Cannot use import statement outside a module at wrapSafe (internal/modules/cjs/loader.js:915:16) at Module._compile (internal/modules/cjs/loader.js:963:27) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10) at Module.load (internal/modules/cjs/loader.js:863:32) at Function.Module._load (internal/modules/cjs/loader.js:708:14) at Module.require (internal/modules/cjs/loader.js:887:19) at require (internal/modules/cjs/helpers.js:74:18) at Object. (/var/app/current/node_modules/@ant-design/colors/dist/index.js:5:18) at Module._compile (internal/modules/cjs/loader.js:999:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10) /var/app/current/node_modules/@ctrl/tinycolor/dist/module/conversion.js:1

Credit the original author

I did not find any credit to the original author of this "cloned" repo:

https://github.com/bgrins/TinyColor

I would like to use your code since it seems better maintained, but I fell that moral demands a credit should be givven since I've been used the real "tinyColor" years ago from the original repo

Unable to extend TinyColor class and chaine methods

I would like to extent TinyColor class to provide more functionality for my own needs, however its not possible to extend and chain methods because of the return types.

Here is an example of extending class:

export class MyTinyColor extends TinyColor {
  css(): string {
    return 'do some work and return value';
  } 
}

chaining 'css' method will cause compiler error (Property 'css' does not exist on type 'TinyColor'):

const color= new MyTinyColor ('#000').setAlpha(0.54).css(); 

The the reason is that the setAlpha returns TinyColor and TinyColor has not implemented css mehtod.

To solve it we can either not specifying return type or return this:

 setAlpha(alpha?: string | number) : this {
     ...
  }

Not sure if there is any other way!

Color Picker Show

Hi guys, i was searching a way to show in my aplication a color picker to the user and I saw that in the first TinyColor, in JS, was a possible to show to the color picker so I wanted to know if this TypScript version has this possibility and if is it possible to create a Pallete.

s=0 sets h=0, v=0 sets both h=0,s=0

Creating a new TinyColor object from HSV, with s=0, internally somehow also h is set to 0:

c = new TinyColor({ h: 200, s: 0, v: 0.7 }).toHsv();
console.log(c.h, c.s, c.v);
Result: 0, 0, 0.7
Expected result: 200, 0, 0.7

and setting v=0, both h and s are set internally to 0:

c = new TinyColor({ h: 200, s: 0.5, v: 0 }).toHsv();
console.log(c.h, c.s, c.v);
Result: 0, 0, 0
Expected result: 200, 0.5, 0

Support alpha in from Ratio

It would be awesome if an rgba object could be supported as well.

new TinyColor({ r: 255, g: 0, b: 0, a: 0.2 });

import { fromRatio } from '@ctrl/tinycolor';
fromRatio({ r: 1, g: 0, b: 0, a: 0.2 });

build fail error with ctrl_tinycolor

hi, I haven't build my project for about 1 month. Today I tried to build it without any change except that I deleted my node_modules(then npm i). And my builder resulted in this error:

ERROR in ./~/_@[email protected]@@ctrl/tinycolor/dist/index.js
Module parse failed: /Users/sihanzhao/Documents/code/acorn-vc-asset/node_modules/source-map-loader@1.1.3@source-map-loader/dist/cjs.js!/Users/sihanzhao/Documents/code/acorn-vc-asset/node_modules/@[email protected]@@ctrl/tinycolor/dist/index.js Unexpected token (74:43)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (74:43)

@ctrl_tinycolor is not my direct dependencies, I didn't know this package before. can someone help me how can I build through this error? thx

Support Lab color

The Lab color space is part of CSS color 4, and is implemented in Safari already. There are also some operations, like color distance, nicely defined in Lab space.

It's be great to support the lab() syntax in parsing, and toLab() and toLabString() methods.

Add a function to compute how a semi-transparent color would appear on a background

The other day I wanted to compute the contrast between two colors but the luminance and hence the readability function doesn’t take the alpha canal into account: one need to know the “final” color, in other words what’s the color is on its background, to compute the contrast.

There are probably other uses for being able to compute how a color would appear on a background, and it’s very short to implement. It’s also easy to get it wrong, so I think it makes sense to have a correct and checked implement in a library like this one.

The implementation could simply be (if you’re interested I can make a PR):

onBackground(background: ColorInput): TinyColor {
  const fg = this.toRgb();
  const bg = new TinyColor(background).toRgb();

  return new TinyColor({
    r: bg.r + (fg.r - bg.r) * fg.a,
    g: bg.g + (fg.g - bg.g) * fg.a,
    b: bg.b + (fg.b - bg.b) * fg.a,
  })
}

I also though of rgbaToRgb as a function name but that may be confusing with all the other function with rgb in their names I guess.

Adapted from this StackOverflow answer.

[feature request] Add description to all methods so they appear in intellisense.

The README has some nice documentation, but none of this appears inside VS Code when using intellisense. For example, hovering on the mix method I see the following tooltip:

tooltip-with-no-description

The tooltip has no helpful description.

As an example, we can write a description on a method in JSDoc format like this:

Screenshot from 2020-04-24 15-29-06

Then when we hover on it in other place we see the JSDoc description in the tooltip:

helpful-info

Feature request: ability to control the brightness value at which a color is considerd dark or light

Currently the isDark and isLight is determined by checking the brightness against a static 128 value, however it would be nice to be able to adjust this value(I've found, for example, that 170 fits my needs better). Potentially add an option in the constructor, maybe as part of TinyColorOptions: {brightnessThreshold: number}.

I'm aware that you can get around it by doing const isDark = tinyColor.getBrightness() < 170;, but this proposal would allow the user to create a wrapper around TinyColor, and have a default brightness threshold across the entire app, and not have to manually specify it every time.

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Preset name not found within published preset config (monorepo:angularmaterial). Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

Sourcemaps linked correctly?

I'm noticing this warning in my local dev terminal since upgrading the package recently.

WARNING in ./node_modules/@ctrl/tinycolor/dist/module/util.js
Module Warning (from ./node_modules/source-map-loader/dist/cjs.js):
Failed to parse source map from '/src/main/webpack/node_modules/@ctrl/tinycolor/src/util.ts' file: Error: ENOENT: no such file or directory, open '/src/main/webpack/node_modules/@ctrl/tinycolor/src/util.ts'
Error: Failed to parse source map from '/src/main/webpack/node_modules/@ctrl/tinycolor/src/util.ts' file: Error: ENOENT: no such file or directory, open '/src/main/webpack/node_modules/@ctrl/tinycolor/src/util.ts'
    at fetchFromFilesystem (/src/main/webpack/node_modules/source-map-loader/dist/utils.js:130:11)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
 @ ./node_modules/@ctrl/tinycolor/dist/module/index.js 4:0-54 101:17-27 198:51-58 210:51-58 296:16-23 320:16-23 350:16-23 361:16-23
 @ ./node_modules/@ctrl/tinycolor/dist/module/public_api.js
 @ ./funnel/components/header/header-full-width.tsx
 @ ./funnel/pages/Funnel.tsx
 @ ./funnel/pages/funnel-loader.tsx

util.js is showing a reference to ../src/util.ts

{"version":3,"file":"util.js","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":";;;AAAA;;;GAGG;AACH,SAAgB,OAAO,CAAC,CAAM,EAAE,GAAW;IACzC,IAAI,cAAc,CAAC,CAAC,CAAC,EAAE;QACrB,CAAC,GAAG,MAAM,CAAC;KACZ;IAED,MAAM,SAAS,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC;IAClC,CAAC,GAAG,GAAG,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAEhE,+CAA+C;IAC/C,IAAI,SAAS,EAAE;QACb,CAAC,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC,GAAG,GAAG,CAAC,EAAE,EAAE,CAAC,GAAG,GAAG,CAAC;KACzC;IAED,wCAAwC;IACxC,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,QAAQ,EAAE;QAChC,OAAO,CAAC,CAAC;KACV;IAED,gDAAgD;IAChD,IAAI,GAAG,KAAK,GAAG,EAAE;QACf,kCAAkC;QAClC,sDAAsD;QACtD,4BAA4B;QAC5B,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,UAAU,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;KACnE;SAAM;QACL,kCAAkC;QAClC,iDAAiD;QACjD,CAAC,GAAG,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,UAAU,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;KACzC;IAED,OAAO,CAAC,CAAC;AACX,CAAC;AA/BD,0BA+BC;AAED;;;GAGG;AACH,SAAgB,OAAO,CAAC,GAAW;IACjC,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC;AACvC,CAAC;AAFD,0BAEC;AAED;;;;GAIG;AACH,SAAgB,cAAc,CAAC,CAAkB;IAC/C,OAAO,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;AACzE,CAAC;AAFD,wCAEC;AAED;;;GAGG;AACH,SAAgB,YAAY,CAAC,CAAkB;IAC7C,OAAO,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;AAClD,CAAC;AAFD,oCAEC;AAED;;;GAGG;AACH,SAAgB,UAAU,CAAC,CAAmB;IAC5C,CAAC,GAAG,UAAU,CAAC,CAAW,CAAC,CAAC;IAE5B,IAAI,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE;QAC9B,CAAC,GAAG,CAAC,CAAC;KACP;IAED,OAAO,CAAC,CAAC;AACX,CAAC;AARD,gCAQC;AAED;;;GAGG;AACH,SAAgB,mBAAmB,CAAC,CAAkB;IACpD,IAAI,CAAC,IAAI,CAAC,EAAE;QACV,OAAO,GAAG,MAAM,CAAC,CAAC,CAAC,GAAG,GAAG,GAAG,CAAC;KAC9B;IAED,OAAO,CAAC,CAAC;AACX,CAAC;AAND,kDAMC;AAED;;;GAGG;AACH,SAAgB,IAAI,CAAC,CAAS;IAC5B,OAAO,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;AAC9C,CAAC;AAFD,oBAEC"}

Are the mapping files meant to be part of the distribution? If so, looks like they aren't referencing the distributed .js files.

import { bound01, pad2 } from './util'

There is a problem with the new version 3.4.0
/node_modules/@ctrl/tinycolor/dist/module/conversion.js:1
(function (exports, require, module, __filename, __dirname) { import { bound01, pad2 } from './util';
^

SyntaxError: Unexpected token {
at new Script (vm.js:80:7)
at createScript (vm.js:274:10)
at Object.runInThisContext (vm.js:326:10)
at Module._compile (internal/modules/cjs/loader.js:664:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)

When I switch to 3.3.4, there is no problem

Not compatible with IE11

It it me or compatibility with es5 only browser has been dropped in 3.x branch?
Compiled code contains ES6 syntax which then crash in IE11 with SCRIPT1003: Expected ':'.

v4: warning when installed with yarn

We've just upgraded to @ctrl/[email protected] and yarn started giving: warning Workspaces can only be enabled in private projects.

Looking into the package.json of the new release, it is marked like a monorepo:

{
...
  "workspaces": [
    "demo"
  ],
...
}

Backwards compatibility broken in 2.5.0

I used to import interfaces in my typescript app like this:

import { RGBA } from '@ctrl/tinycolor/interfaces';

but after upgrade to 2.5.0 I get an error:

TS2307: Cannot find module '@ctrl/tinycolor/interfaces'.

Imports must be changed manually:

import { RGBA } from '@ctrl/tinycolor/dist/interfaces';

I found this issue critical, because it can unexpectedly affect each TS app that have tinycolor as a dependency in following manner:

"@ctrl/tinycolor": "^2.2.1",

Like I used to have above entry in my package.json.

[Feature request]: toRgbRatio

Hey, in some cases, e.g. when delivering data to iOS you need a ratio rgb color. It would be great if tinycolor could also support toRgbRatio.

I think it would work like this

const color = new TinyColor('red');
color.toRgbRatio(); // { r: 1, g: 0, b: 0, a: 1 }

It should be pretty similar to toPercentageRgb since the ratio is basically a percentage value as a float from 0 to 1.

Space-separated color params and aliases aren't parsed correctly

This is an excellent library!

Thanks to a generous PR by @ajmchambers, TinyColor is now being used to power the color picker in Shoelace. 🎉

While reviewing the PR, I noticed a few small parsing blips that could be improved. On the website, the following space-separated color params fail to parse. All of the following are equivalent and should result in white @ 25% alpha, or #ffffff40.

rgb(255 255 255 / 25%) => #ffffffff
rgb(255 255 255 / .25) => #ffffffff

hsl(0 0% 100% / 25%)   => #ffffffff
hsl(0 0% 100% / .25)   => #ffffffff

The resulting values are all white, but with 100% instead of 25% alpha.

Similarly, rgba and hsla are effectively aliases of rgb and hsl now, but alpha is only considered when the alias is used.

rgb(255, 255, 255, 25%)  => #ffffffff
rgb 255 255 255 .25      => #ffffffff

rgba(255, 255, 255, 25%) => #ffffff40
rgba 255 255 255 .25     => #ffffff40

Let me know if you need any additional info on this!

[Bug] Next.js 14.0.4 every operation on TinyColor returns black #000000

When doing a production build of next.js 14.0.4 (does not happen in 14.0.3 and below) whenever i construct a TinyColor, it is always considered as pure black. During the development build everything seems to work, but in production build it breaks.

I made an example on code sandbox with this issue here

to reproduce, run a build of the server npm run build (you will see a console log of the #000000 color too) and then start it

Dependency deprecation warning: travis-deploy-once (npm)

On registry https://registry.npmjs.org/, the "latest" version (v5.0.11) of dependency travis-deploy-once has the following deprecation notice:

We recommend to use Travis Build Stages instead

Marking the latest version of an npm package as deprecated results in the entire package being considered deprecated, so contact the package author you think this is a mistake.

Affected package file(s): package.json

If you don't care about this, you can close this issue and not be warned about travis-deploy-once's deprecation again. If you would like to completely disable all future deprecation warnings then add the following to your config:

"suppressNotifications": ["deprecationWarningIssues"]

Saturation (HSV) values not reflected properly

In hsv format the values are in degree , %, % respectively and when we provide color value as hsv(360,1,88), it assumes '1' as '100%'. Apparently for any value x∈[0,1] it converts it in percentage and assumes accordingly.
Thus hsv(360,1,88) is considered as hsv(360,100,88) and there is practically no way to input color values hsv(360,1,88).
The issue can be seen here

The issue can be seen in the attached pictures as well where hsv(360,1,88) is assumed as hsv(360,100,88) but hsv(360,1.0001,88) is correctly rendering color.

Screenshot 2024-04-05 at 4 33 16 PM Screenshot 2024-04-05 at 4 33 31 PM

New export type broke lots of apps

Hi there,

Your 3.3.0 update just broke all of our apps. We're using @ant-design/colors, which uses tinycolor, and the 3.3.0 ES module exports broke our apps with this error:

import { tinycolor } from './index';
^^^^^^
Cannot use import statement outside a module

It might be an issue with @ant-design/colors as well, but I don't think 3.3.0 is backward-compatible with 3.2.1. It's always better to respect semver and not make breaking changes on minor version updates 🤞

Question about class usage

It feels a little weird or unnecessary to new up an instance. I was wondering why that design decision was made? What are your thoughts on exposing a function as well, the way @bgrins does in the original repo?

Also & aside from that, great work breathing new life into this! 👏

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.