colors-convert
Advanced tools
Comparing version 1.1.4 to 1.1.5
@@ -1,2 +0,2 @@ | ||
import { isHex, isRgb, isRgba, isCmyk, isColor } from '../types/isType'; | ||
import { isHex, isRgb, isRgba, isCmyk, isHsl, isColor } from '../types/isType'; | ||
import { toUpper } from 'lodash'; | ||
@@ -20,2 +20,5 @@ // Convert a color to a string format | ||
} | ||
else if (isHsl(color)) { | ||
return color.h + "\u00B0, " + color.s + "%, " + color.l + "%"; | ||
} | ||
else { | ||
@@ -42,2 +45,5 @@ throw new Error(color + " is not a valid type of color."); | ||
} | ||
else if (isHsl(color)) { | ||
return "hsl(" + color.h + "\u00B0, " + color.s + "%, " + color.l + "%)"; | ||
} | ||
else { | ||
@@ -44,0 +50,0 @@ throw new Error(color + " is not a valid type of color."); |
import { color2rgb } from './rgb'; | ||
import { sum, sumBy, round } from 'lodash'; | ||
import { applyFnToEachObjValue } from './utils'; | ||
// Blend two or more colors based on their weights. | ||
// Mix two or more colors based on their weights. | ||
// Given [c1, c2, c3, ...] and [w1, w2, w3, ...], it returns: | ||
@@ -6,0 +6,0 @@ // mixedCol = { |
@@ -10,2 +10,3 @@ import { color2string, color2cssString } from '../index'; | ||
expect(color2string({ c: 0, m: 0, y: 0, k: 0 })).toBe('0%, 0%, 0%, 0%'); | ||
expect(color2string({ h: 0, s: 0, l: 0 })).toBe('0°, 0%, 0%'); | ||
}); | ||
@@ -20,3 +21,4 @@ //////////////////////////////////////////////////////// | ||
expect(color2cssString({ c: 0, m: 0, y: 0, k: 0 })).toBe('cmyk(0%, 0%, 0%, 0%)'); | ||
expect(color2cssString({ h: 0, s: 0, l: 0 })).toBe('hsl(0°, 0%, 0%)'); | ||
}); | ||
//# sourceMappingURL=color.test.js.map |
{ | ||
"name": "colors-convert", | ||
"version": "1.1.4", | ||
"version": "1.1.5", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "types": "dist/index.d.ts", |
142
README.md
@@ -1,5 +0,140 @@ | ||
# Colors-convert | ||
# Colors convert 🍭🍬 | ||
A simple colors library. | ||
Using `colors-convert` you can: | ||
- read colors in different formats | ||
- analyze and manipulate colors | ||
- convert colors into different formats | ||
- give a name to a color. | ||
## How to use | ||
yarn add colors-convert | ||
or | ||
npm install --save colors-convert | ||
## API | ||
Different color formats are supported: hex, rgb, rgba, cmyk and hsl. | ||
--- | ||
### 🎨 Color formats | ||
#### Hex | ||
A valid hex color can be: | ||
- long form: `#FFFFFF` | ||
- short form: `#FFF` | ||
- long form with opacity: `#FFFFFFFF` (white with opacity `FF=1`). | ||
#### `isHex(color): boolean` | ||
Returns `true` if `color` is a valid hex, `false` otherwise. | ||
#### Rgb | ||
A valid rgb color is an object like this `{r, g, b}` with `r, b, g` numeric values in range `[0, 255]`. | ||
#### `isRgb(color): boolean` | ||
Returns `true` if `color` is a valid rgb, `false` otherwise. | ||
#### Rgba | ||
A valid rgba color is an object like this `{r, g, b, a}` with `r, g, b` numeric values in range `[0, 255]` and `a` in range `[0, 1]`. | ||
#### `isRgba(color): boolean` | ||
Returns `true` if `color` is a valid rgba, `false` otherwise. | ||
#### Cmyk | ||
A valid cmyk color is an object like this `{c, m, y, k}` with `c, m, y, k` numeric values in range `[0, 100]`. | ||
#### `isCmyk(color): boolean` | ||
Returns `true` if `color` is a valid cmyk, `false` otherwise. | ||
#### Hsl | ||
A valid hsl color is an object like this `{h, s, l}` where: | ||
- `h` (hue): `[0-359]°` | ||
- `s` (saturation): `[0-100]%` | ||
- `l` (lightness): `[0-100]%`. | ||
#### `isHsl(color): boolean` | ||
Returns `true` if `color` is a valid hsl, `false` otherwise. | ||
--- | ||
### 🎨 Conversion | ||
- **`hex2rgbOrRgba(hex): rgb | rgba`**: Converts a hex to a rgb or rgba color (depends on hex format) | ||
- **`hex2rgba(hex, alpha = 1): rgba`**: Converts a hex to a rgba object | ||
- **`hex2hexWithAlpha(hex, alpha): hex`**: Converts a hex to another hex with the given alpha | ||
- **`hex2cmyk(hex): cmyk`**: Converts a hex to a cmyk. If hex is in the long format (e.g. #000000FF) it removes the last two chars because cmyk doens't support opacity | ||
- **`hex2hsl(hex): hsl`**: Converts a hex object to hsl | ||
- **`rgb2hex(rgb): hex`**: Converts a rgb object to hex | ||
- **`rgb2cmyk(rgb): cmyk`**: Converts a rgb to a cmyk | ||
- **`rgb2hsl(rgb): hsl`**: Converts a rgb object to hsl | ||
- **`rgba2rgb(rgba): rgb`**: Converts a rgba color to a rgb color removing the alpha value | ||
- **`rgb2rgba(rgb): rgba`**: Converts a rgb color to a rgba color adding 1 as alpha | ||
- **`color2rgb(color): rgb`**: Converts a generic color (hex, rgb, rgba, cmyk, hsl) to rgb | ||
- **`cmyk2rgb(cmyk): rgb`**: Converts a cmyk color to a rgb | ||
- **`cmyk2hex(cmyk): hex`**: Converts a cmyk color to a hex | ||
- **`cmyk2hsl(cmyk): hsl`**: Converts a cmyk object to hsl | ||
- **`hsl2hex(hsl): hex`**: Converts a hsl object to hex | ||
- **`hsl2rgb(hsl): rgb`**: Converts a hsl object to rgb | ||
- **`hsl2cmyk(hsl): cmyk`**: Converts a hsl object to cmyk | ||
--- | ||
### 🎨 Name that color | ||
- **`name(color): string`**: Given a color (hex, rgb, rgba, cmyk or hsl), it returns the name of that color. It works using a list of [18315 unique color names](https://api.color.pizza/v1/). | ||
--- | ||
### 🎨 Utils | ||
- **`color2string(color): string`**: Converts a color to a string format. For example: | ||
- hex: `#E6259F` | ||
- rgb: `230, 37, 159` | ||
- rgba: `230, 37, 159, 1` | ||
- cmyk: `0%, 84%, 31%, 10%` | ||
- hsl: `322°, 79%, 52%` | ||
- **`color2cssString(color): string`**: Converts a color to a string format usable in CSS. For example: | ||
- hex: `#E6259F` | ||
- rgb: `rgb(230, 37, 159)` | ||
- rgba: `rgba(230, 37, 159, 1)` | ||
- cmyk: `cmyk(0%, 84%, 31%, 10%)` | ||
- hsl: `hsl(322°, 79%, 52%)` | ||
- **`mix(colors: Color[], weights?: number[]): rgb`**: Mix two or more colors based on their weights. | ||
<!--- | ||
@@ -30,1 +165,6 @@ | ||
--> | ||
<!-- | ||
color names from https://github.com/meodai/color-names | ||
--> |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3929709
19297
169