Optical-js
A tiny javascript library to manipulate colors
Instalation
npm i optical-js -s
or
yarn add optical-js
Optical-js has a Color
constructor or isolated functions that allow you to manipulate colors.
In both cases, using by constructor creator or by functions, your can define a color by using one of the colorFormat:
- Using a rbga object:
{r: $red, g: $green, b: $blue, a: $alpha}
- A hsl string:
hsl($hue, $saturation, $light)
- A hsla string:
hsla($hue, $saturation, $light, $alpha)
- A rba string:
rgb($red, $blue, $green)
- A rgba string:
rgba($red, $blue, $green, $alpha)
- A 3 digitis hexa string:
#000
- A 6 digitis hexa string:
#000000
- A color name:
blue
- A color instance:
new Color(blue)
Any one of this formats is accepted.
When you has a color, you can manipulate then, with transformation methods:
- alpha: (float: [0..1])
Change the alpha channel from this color, accept only decimal values between 0 and 1, Ex:
'red' // => rgba(255, 0, 0, 0.5); // => rgba(255, 0, 0, 0.5);
- hue: (int: [0..360])
Change the hue channel from this color, accept only integer values between 0 and 360, Ex:
r: 255 g: 0 b: 0 a: 1 // => rgb(128, 255, 0); // => rgb(128, 255, 0);
- saturation: (int: [0..100])
Change the saturation channel from this color, accept only integer values between 0 and 100, Ex:
'rgb(255, 0, 0)' // => rgb(191, 64, 64); // => rgb(191, 64, 64);
- light: (int: [0..100])
Change the light channel from this color, accept only integer values between 0 and 100, Ex:
'rgba(255, 0, 0, 1)' // => rgb(235, 173, 173); // => rgb(235, 173, 173);
- red: (int: [0..255])
Change the red channel from this color, accept only integer values between 0 and 255, Ex:
'hsl(0, 100%, 50%)' // => rgb(50, 0, 0); // => rgb(50, 0, 0);
- green: (int: [0..255])
Change the red channel from this color, accept only integer values between 0 and 255, Ex:
'hsla(0, 100%, 50%, 1)' // => rgb(255, 50, 0); // => rgb(255, 50, 0);
- blue: (int: [0..255])
Change the red channel from this color, accept only integer values between 0 and 255, Ex:
'red' // => rgb(255, 0, 50); // => rgb(255, 0, 50);
- saturate: (int: [0..100])
Change the red channel from this color, accept only integer values between 0 and 100, Ex:
'brown' // => rgb(203, 1, 1); 40) // => rgb(203, 1, 1);
- desaturate: (int: [0..100])
Change the red channel from this color, accept only integer values between 0 and 255, Ex:
'red' // => rgb(191, 64, 64); // => rgb(191, 64, 64);
- lighten: (int: [0..100])
Change the red channel from this color, accept only integer values between 0 and 255, Ex:
'red' // => rgb(255, 102, 102); // => rgb(255, 102, 102);
- darken: (int: [0..100])
Change the red channel from this color, accept only integer values between 0 and 255, Ex:
'red' // => rgb(153, 0, 0); // => rgb(153, 0, 0);
- opacify: (int: [0..1])
Change the red channel from this color, accept only integer values between 0 and 255, Ex:
'red' // => rgba(255, 0, 0, 0.8); // => rgba(255, 0, 0, 0.8);
- hueRotate: (int: [0..*])
Change the red channel from this color, accept only integer values between 0 and 255, Ex:
'red' // => rgb(0, 255, 81); // => rgb(0, 255, 81);
- redish: (int: [0..255])
Change the red channel from this color, accept only integer values between 0 and 255, Ex:
'blue' // => rgb(100, 0, 255); // => rgb(100, 0, 255);
- greenish: (int: [0..255])
Change the red channel from this color, accept only integer values between 0 and 255, Ex:
'red' // => rgb(255, 100, 0); // => rgb(255, 100, 0);
- blueish: (int: [0..255])
Change the red channel from this color, accept only integer values between 0 and 255, Ex:
'red' // => rgb(255, 0, 100); // => rgb(255, 0, 100);
- multiply: (color: [colorFormat])
Apply Multiply
color algorithm, accept all of the colors formats, Ex:
'red' // => rgb(128, 0, 128); // => rgb(128, 0, 128);
- screen: (color: [colorFormat])
Apply Screen
color algorithm, accept all of the colors formats, Ex:
'red' // => rgb(255, 0, 255); screen'red' 'blue' // => rgb(255, 0, 255);
- overlay: (color: [colorFormat])
Apply Overlay
color algorithm, accept all of the colors formats, Ex:
'red' // => rgb(255, 0, 0); // => rgb(255, 0, 0);
- mix: (color: [colorFormat], weight: [0..1])
Apply Mix
color algorithm, accept all of the colors formats, and a weight to represent amount strength is the mixed color, 0.5 is the middle between the colors strength on the mix Ex:
'red' // => rgb(179, 0, 77); // => rgb(179, 0, 77);
- difference: (color: [colorFormat])
Apply Difference
color algorithm, accept all of the colors formats, Ex:
'red' // => rgb(255, 0, 255); // => rgb(255, 0, 255);
- divide: (color: [colorFormat])
Apply Divide
color algorithm, accept all of the colors formats, Ex:
'red' // => rgb(198, 255, 0); // => rgb(198, 255, 0);
- addition: (color: [colorFormat])
Apply Addition
color algorithm, accept all of the colors formats, Ex:
'red' // => rgb(255, 0, 255); // => rgb(255, 0, 255);
- subtract: (color: [colorFormat])
Apply Subtract
color algorithm, accept all of the colors formats, Ex:
'brown' // => rgb(165, 42, 0); // => rgb(165, 42, 0);
- darkenOnly: (color: [colorFormat])
Apply DarkenOnly
color algorithm, accept all of the colors formats, Ex:
'brown' // => rgb(165, 0, 42); // => rgb(165, 0, 42);
- lightenOnly: (color: [colorFormat])
Apply LightenOnly
color algorithm, accept all of the colors formats, Ex:
'brown' // => rgb(255, 42, 255); // => rgb(255, 42, 255);
Use case
A famous library call styled-component
is very usefull for react aplicattions.
styled-components is the result of wondering how we could enhance CSS for styling React component systems. By focusing on a single use case we managed to optimize the experience for developers as well as the output for end users.
by styled-component
This library replace preprocessors like sass or less, but, styled-components not has alternatives to manipulate colors like less or sass. With optical-js
you can continue to use that colors manipulation methods.
;;; const colors = primary: '#2196f3' secondary: '#8bc34a' default: '#dedede';; const getColor = colorscolor || colorsdefault; const Button = styledbutton` background: ; color: white; border: 2px solid ; border-radius: 3px; &:hover { background: ; }`;