Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add culori.formatHsl() #130

Merged
merged 2 commits into from
Jul 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ culori.formatRgb('lab(50 0 0 / 25%)');
// ⇒ "rgba(119, 119, 119, 0.25)"
```

<a name="formatHsl" href="#formatHsl">#</a> culori.**formatHsl**(_color_ or _string_) → _string_ &middot; [Source](https://github.com/evercoder/culori/blob/master/src/formatter.js)

Returns the `hsl(…)` / `hsla(…)` string for a color. Fully opaque colors will be serialized as `hsl()`, and semi-transparent colors as `hsla()`. All values are rounded to a precision of two digits. The Saturation and Lightness are clamped to the interval `[0%, 100%]`.

```js
culori.formatHsl('lab(50 0 0 / 25%)');
// ⇒ 'hsla(194.33, 0%, 46.63%, 0.25)'
```

## Clamping

Some color spaces (Lab and LCh in particular) allow you to express colors that can't be displayed on-screen. The methods below allow you to identify when that's the case and to produce displayable versions of the colors.
Expand Down
31 changes: 27 additions & 4 deletions src/formatter.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import converter from './converter';
import round from './round';
import fixup from './util/fixup';

let rgb = converter('rgb');
let roundAlpha = round(2);
let hsl = converter('hsl');
let twoDecimals = round(2);

const clamp = value => Math.max(0, Math.min(1, value));
const fixup = value => Math.round(clamp(value) * 255);

const formatHex = c => {
let color = rgb(c);
Expand Down Expand Up @@ -47,7 +50,27 @@ const formatRgb = c => {
return `rgb(${r}, ${g}, ${b})`;
} else {
// transparent color
return `rgba(${r}, ${g}, ${b}, ${roundAlpha(color.alpha)})`;
return `rgba(${r}, ${g}, ${b}, ${twoDecimals(clamp(color.alpha))})`;
}
};

const formatHsl = c => {
let color = hsl(c);

if (color === undefined) {
return undefined;
}

const h = twoDecimals(color.h || 0);
const s = twoDecimals(clamp(color.s) * 100);
const l = twoDecimals(clamp(color.l) * 100);

if (color.alpha === undefined || color.alpha === 1) {
// opaque color
return `hsl(${h}, ${s}%, ${l}%)`;
} else {
// transparent color
return `hsla(${h}, ${s}%, ${l}%, ${twoDecimals(clamp(color.alpha))})`;
}
};

Expand All @@ -62,4 +85,4 @@ const formatter = (format = 'rgb') => {
return undefined;
};

export { formatHex, formatHex8, formatRgb, formatter };
export { formatHex, formatHex8, formatRgb, formatHsl, formatter };
8 changes: 7 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,13 @@ export {
oklch
};

export { formatter, formatHex, formatHex8, formatRgb } from './formatter';
export {
formatter,
formatHex,
formatHex8,
formatRgb,
formatHsl
} from './formatter';
export { default as round } from './round';
export {
interpolate,
Expand Down
3 changes: 0 additions & 3 deletions src/util/fixup.js

This file was deleted.

21 changes: 20 additions & 1 deletion test/formatter.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import tape from 'tape';
import { formatHex, formatHex8, formatRgb, rgb } from '../src/index';
import { formatHex, formatHex8, formatRgb, formatHsl, rgb } from '../src/index';

tape('formatHex', function (test) {
test.equal(formatHex('tomato'), '#ff6347');
Expand All @@ -20,3 +20,22 @@ tape('formatRgb', function (test) {

test.end();
});

tape('formatHsl', function (test) {
test.equal(formatHsl('red'), 'hsl(0, 100%, 50%)');
test.equal(
formatHsl({
mode: 'hsl',
h: 30.21,
s: 0.2361,
l: 0.48321,
alpha: -0.2
}),
'hsla(30.21, 23.61%, 48.32%, 0)'
);
test.equal(
formatHsl({ mode: 'hsl', h: 405, s: 1.2, l: -1 }),
'hsl(405, 100%, 0%)'
);
test.end();
});