Skip to content

Commit

Permalink
Group formats
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesnw committed Oct 26, 2023
1 parent f92ec4a commit ddfb010
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 16 deletions.
38 changes: 38 additions & 0 deletions src/lib/components/colors/FormatGroup.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<script lang="ts">
import { inGamut, to } from 'colorjs.io/fn';
import type { PlainColorObject } from 'colorjs.io/types/src/color';
import Output from '$lib/components/colors/Output.svelte';
import type { ColorFormatId } from '$lib/constants';
import { getSpaceFromFormatId } from '$lib/utils';
import ExternalLink from '../util/ExternalLink.svelte';
export let type: 'bg' | 'fg';
export let color: PlainColorObject;
export let name: string;
export let formats: ColorFormatId[];
$: targetSpace = getSpaceFromFormatId(formats[0] as ColorFormatId);
$: targetColor = to(color, targetSpace);
$: isInGamut = inGamut(targetColor);
</script>

<div data-content="format-group">
<h5>{name}</h5>
{#if !isInGamut}
<span data-color-info="warning"
>Selected color is outside the {targetColor.space.name} gamut.</span
><ExternalLink href="https://www.w3.org/TR/css-color-4/#out-of-gamut"
></ExternalLink>
{/if}
{#each formats as format (format)}
<Output {type} {color} {format} />
{/each}
</div>

<style lang="scss">
[data-color-info='warning'] {
grid-area: message;
}
</style>
26 changes: 21 additions & 5 deletions src/lib/components/colors/Formats.svelte
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
<script lang="ts">
import type { PlainColorObject } from 'colorjs.io/types/src/color';
import Output from '$lib/components/colors/Output.svelte';
import FormatGroup from '$lib/components/colors/FormatGroup.svelte';
import type { ColorFormatId } from '$lib/constants';
import { FORMATS } from '$lib/constants';
import { FORMAT_GROUPS, FORMATS } from '$lib/constants';
export let type: 'bg' | 'fg';
export let color: PlainColorObject;
export let format: ColorFormatId;
function otherFormatGroups(
selectedFormat: ColorFormatId,
): typeof FORMAT_GROUPS {
const otherFormats: typeof FORMAT_GROUPS = {};
FORMATS.filter((s) => s !== format);
Object.keys(FORMAT_GROUPS).forEach((key) => {
const groupFormats = FORMAT_GROUPS[key]!.filter(
(s) => s !== selectedFormat,
);
if (groupFormats.length) {
otherFormats[key] = groupFormats;
}
});
return otherFormats;
}
$: displayType = type === 'bg' ? 'Background' : 'Foreground';
$: otherFormats = FORMATS.filter((s) => s !== format);
$: otherFormats = otherFormatGroups(format);
</script>

<div data-content="formats" data-column="tool">
<h4 class="small-only label">{displayType} Color</h4>
{#each otherFormats as format (format)}
<Output {type} {color} {format} />
{#each Object.entries(otherFormats) as [name, formats]}
<FormatGroup {type} {color} {name} {formats} />
{/each}
</div>

Expand Down
12 changes: 1 addition & 11 deletions src/lib/components/colors/Output.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { inGamut, serialize, to } from 'colorjs.io/fn';
import { serialize, to } from 'colorjs.io/fn';
import type { PlainColorObject } from 'colorjs.io/types/src/color';
import SupportWarning from '$lib/components/colors/SupportWarning.svelte';
Expand All @@ -13,7 +13,6 @@
$: targetSpace = getSpaceFromFormatId(format);
$: targetColor = to(color, targetSpace);
$: isInGamut = inGamut(targetColor);
$: targetColorValue = serialize(targetColor, {
format,
inGamut: false,
Expand All @@ -25,11 +24,6 @@
<CopyButton text={targetColorValue} />
<span data-color-info="value">{targetColorValue}</span>
<SupportWarning {format} />
{#if !isInGamut}
<span data-color-info="warning"
>This color is outside the {targetColor.space.name} gamut.</span
>
{/if}
</li>
</ul>

Expand All @@ -45,8 +39,4 @@
[data-color-info='value'] {
grid-area: color;
}
[data-color-info='warning'] {
grid-area: message;
}
</style>
6 changes: 6 additions & 0 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ export const FORMATS: ColorFormatId[] = [
'srgb',
];

export const FORMAT_GROUPS: { [key: string]: ColorFormatId[] } = {
'sRGB Formats': ['hex', 'hsl', 'srgb'],
'Unbounded Spaces': ['lab', 'lch', 'oklab', 'oklch'],
'Display p3 Space': ['p3'],
};

export const RATIOS = {
AA: {
Normal: 4.5,
Expand Down

0 comments on commit ddfb010

Please sign in to comment.