Skip to content

Commit

Permalink
Updated README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
smikhalevski committed Aug 24, 2023
1 parent 031456c commit 0eb3a24
Showing 1 changed file with 49 additions and 34 deletions.
83 changes: 49 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,92 +16,107 @@ npm install --save-prod paint-bucket
🪣 [API documentation is available here.](https://smikhalevski.github.io/paint-bucket/classes/core_src_main.Color.html)

```ts
import { Color } from 'paint-bucket';
import { clr } from 'paint-bucket';

// or cherry-pick plugins to reduce bundle size
//
// import rgbPlugin from '@paint-bucket/rgb-plugin';
// import hslPlugin from '@paint-bucket/hsl-plugin';
// import cssPlugin from '@paint-bucket/css-plugin';
//
// Color.applyPlugins(rgbPlugin, hslPlugin, cssPlugin);
clr('#abcdef').saturation(S => S / 2).red(); // ⮕ 188
```

You can cherry-pick plugins that you need:

```ts
import { clr, Color } from 'paint-bucket/core';
import enableRGB from 'paint-bucket/plugin/rgb';

Color.parse('#abcdef').saturation(S => S / 2).red(); // ⮕ 188
enableRGB(Color);

clr().red(); //

clr().saturation(); // ❌ Error: saturation not defined
```

Most methods provide getter-setter semantics:

```ts
// Set
Color.parse('#f00').red(127.5); // ⮕ Color instance
// Setter
clr('#f00').red(127.5); // ⮕ Color instance
// or
Color.parse('#f00').red(R => R / 2); // ⮕ Color instance
clr('#f00').red(R => R / 2); // ⮕ Color instance

// Get
Color.parse('#f00').red(); // ⮕ 255
// Getter
clr('#f00').red(); // ⮕ 255
```

Mutate multiple components at the same time:

```ts
Color.parse([64, 128, 0])
.rgb(([R, G, B, a]) => [R * 3, G * 2, B, a])
.rgb();
clr([64, 128, 0])
.rgb(([R, G, B, a]) => [R * 3, G * 2, B, a])
.rgb();
// ⮕ [192, 255, 0, 1]
```

`color` returns a mutable instance of `Color`. To create a copy of the `Color` instance you can use one of these
approaches:
`clr` returns a mutable instance of the
[`Color`](https://smikhalevski.github.io/paint-bucket/classes/paint_bucket_core.Color.html) class. To create a copy of
the `Color` instance you can use one of these approaches:

```ts
const color1 = Color.parse('#f00');
const color1 = clr('#f00');

// color2 is a copy of color1
const color2 = Color.parse(color1);
const color2 = clr(color1);
// or
const color3 = color1.clone();
```

Parse and serialize CSS color strings:

```ts
Color.parse('pink').css(); // ⮕ "#ffc0cb"
clr('pink').css(); // ⮕ "#ffc0cb"

Color.parse('rgba(255, 192, 203)').css(); // ⮕ "#ffc0cb"
clr('rgba(255, 192, 203)').css(); // ⮕ "#ffc0cb"
```

Create gradients and obtain color at arbitrary position:

```ts
Color.parse('red').gradient('blue').at(0.70).css(); // ⮕ "#4d00b3"
clr.gradient(['red', 'blue']).at(0.7).css(); // ⮕ "#4d00b3"
```

Create multi-stop gradients:
Create multi-stop gradients with custom stop values:

```ts
color
.gradient(['red', 'pink', 'blue'], [0, 0.5, 1])
.at(0.70)
.css();
clr.gradient()
.stop(0, 'red')
.stop(50, 'pink')
.stop(100, 'blue')
.at(70)
.css();
// ⮕ "#9973e0"
```

# Concepts

Color manipulation isn't possible without the color model concept.
<dl>
<dt><a href="https://en.wikipedia.org/wiki/Color_model">Color model</a></dt>
<dd>

> [Color model](https://en.wikipedia.org/wiki/Color_model) is an abstract mathematical model describing the way colors
> can be represented as tuples of numbers (aka color components).
An abstract mathematical model describing the way colors can be represented as tuples of numbers (aka color components).

There's a large variety of color models aimed for different purposes. Different color models define different color
components. For example, RGB color model defines three color components: red, green and blue; while HSL defines hue,
saturation and lightness color components. One color model can be converted to the other.

> [Color space](https://en.wikipedia.org/wiki/Color_space) defines how color components of the particular color model
> are serialized.
</dd>
<dt><a href="https://en.wikipedia.org/wiki/Color_space">Color space</a></dt>
<dd>

Defines how color components of the particular color model are serialized.

For example, RGB color model can be represented as Adobe RGB or sRGB color space.

</dd>
</dl>

Paint Bucket provides an abstraction for color models which are represented as objects that define methods to convert
color components between color model representation and RGB. Color components are an array of numbers.

Expand Down

0 comments on commit 0eb3a24

Please sign in to comment.