-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Show gamut in sliders #226
Open
jamesnw
wants to merge
7
commits into
main
Choose a base branch
from
gamut
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+145
−16
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f90ce41
Show gamut in sliders
jamesnw 525812e
Fix initial edge bug
jamesnw 5c83b32
Add gamut selector
jamesnw 5188883
Keep sliders opaque, add checkbox to alpha slider
jamesnw 74eac4b
Remove pattern background
jamesnw 06bd0f4
Add no gamut map, tests
jamesnw fdecc3b
Unique id for GamutSelect
jamesnw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<script lang="ts"> | ||
import { GAMUTS } from '$lib/constants'; | ||
import { gamut } from '$lib/stores'; | ||
</script> | ||
|
||
<div data-field="color-gamut"> | ||
<label for="color-gamut" data-label>Gamut</label> | ||
<select name="color-gamut" id="color-gamut" bind:value={$gamut}> | ||
{#each GAMUTS as gamut (gamut.format)} | ||
{#if gamut} | ||
<option value={gamut.format}>{gamut.name}</option> | ||
{/if} | ||
{/each} | ||
</select> | ||
</div> | ||
|
||
<style lang="scss"> | ||
@use 'config'; | ||
|
||
[data-field='color-gamut'] { | ||
align-items: center; | ||
column-gap: var(--gutter); | ||
display: grid; | ||
grid-template: | ||
'format-label' auto | ||
'format-input' auto / 1fr; | ||
justify-content: end; | ||
|
||
@include config.above('sm-page-break') { | ||
grid-template: 'format-label format-input' auto / 1fr minmax(10rem, auto); | ||
} | ||
} | ||
|
||
label { | ||
grid-area: format-label; | ||
|
||
@include config.above('sm-page-break') { | ||
text-align: right; | ||
} | ||
} | ||
|
||
select { | ||
grid-area: format-input; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,74 @@ | ||
import { | ||
clone, | ||
display, | ||
inGamut, | ||
type PlainColorObject, | ||
serialize, | ||
set, | ||
steps, | ||
to, | ||
} from 'colorjs.io/fn'; | ||
|
||
import { type ColorFormatId, FORMATS } from '$lib/constants'; | ||
import { type ColorFormatId, type ColorGamutId, FORMATS } from '$lib/constants'; | ||
|
||
export const getSpaceFromFormatId = (formatId: ColorFormatId) => | ||
formatId === 'hex' ? 'srgb' : formatId; | ||
|
||
export const sliderGradient = ( | ||
color: PlainColorObject, | ||
channel: string, | ||
range: [number, number], | ||
) => { | ||
export const sliderGradient = ({ | ||
color, | ||
channel, | ||
range, | ||
gamut, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is somewhat computationally slow. I toyed around with caching results, but the cache quickly grew to 12 Mbs in about 30 seconds of heavy usage 🫨 . I could likely do some more optimization if it is slow on slower machines. |
||
}: { | ||
color: PlainColorObject; | ||
channel: string; | ||
range: [number, number]; | ||
gamut: ColorGamutId; | ||
}) => { | ||
const start = clone(color); | ||
const end = clone(color); | ||
if (channel === 'alpha') { | ||
start.alpha = range[0]; | ||
end.alpha = range[1]; | ||
start.alpha = 0; | ||
end.alpha = 1; | ||
} else { | ||
set(start, channel, range[0]); | ||
start.alpha = 1; | ||
set(end, channel, range[1]); | ||
end.alpha = 1; | ||
} | ||
|
||
const gradientSteps = steps(start, end, { | ||
steps: 10, | ||
space: color.space, | ||
hue: 'raw', | ||
maxDeltaE: 10, | ||
}); | ||
let wasInGamut: boolean; | ||
const inGamutSteps: string[] = []; | ||
const stepWidth = 100 / (gradientSteps.length - 1); | ||
|
||
if (channel === 'alpha' || gamut === null) { | ||
return gradientSteps.map((c) => display(c)).join(', '); | ||
} | ||
|
||
gradientSteps.forEach((step, index) => { | ||
if (inGamut(step, gamut)) { | ||
if (wasInGamut === false) { | ||
inGamutSteps.push(`transparent ${stepWidth * (index + 1)}%`); | ||
} | ||
wasInGamut = true; | ||
inGamutSteps.push(`${display(step)} ${stepWidth * index}%`); | ||
} else { | ||
if (wasInGamut === true) { | ||
inGamutSteps.push(`transparent ${stepWidth * (index - 1)}%`); | ||
} | ||
inGamutSteps.push(`transparent ${stepWidth * index}%`); | ||
|
||
wasInGamut = false; | ||
} | ||
}); | ||
|
||
return gradientSteps.map((c) => display(c)).join(', '); | ||
return inGamutSteps.join(', '); | ||
}; | ||
|
||
function decodeColor(colorHash: string, format: ColorFormatId) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { fireEvent, render } from '@testing-library/svelte'; | ||
import { get } from 'svelte/store'; | ||
|
||
import Gamut from '$lib/components/GamutSelect.svelte'; | ||
import { gamut, INITIAL_VALUES, reset } from '$lib/stores'; | ||
|
||
describe('Space', () => { | ||
afterEach(() => { | ||
reset(); | ||
}); | ||
|
||
it('renders editable gamut select', async () => { | ||
const { getByLabelText } = render(Gamut); | ||
|
||
expect(get(gamut)).toBe(INITIAL_VALUES.gamut); | ||
|
||
const select = getByLabelText('Gamut'); | ||
await fireEvent.change(select, { target: { value: 'rec2020' } }); | ||
|
||
expect(get(gamut)).toBe('rec2020'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is copy pasted from SpaceSelect- should we make this a pattern somewhere?