Skip to content
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
26 changes: 23 additions & 3 deletions src/components/Gradient.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import {linearAngleToString} from '../utils/linear.ts'
import {isCylindricalSpace} from '../utils/colorspace.ts'
import {updateStops} from '../utils/stops.ts'
import {updateStops, reverseStops} from '../utils/stops.ts'
import {randomNumber} from '../utils/numbers.ts'

import GradientStops from './GradientStops.svelte'
Expand Down Expand Up @@ -334,7 +334,7 @@ import GradientImportDialog from './GradientImportDialog.svelte'
let p2 = s.position2

// Omit values equal to tool defaults only if explicitly stored in stop.auto
if (p1 != null && s.auto != null && p1 == s.auto) p1 = null
if (p1 != null && s.auto != null && p1 == s.auto && (p2 == null || String(p2) === String(p1))) p1 = null

// Omit browser default edges only when explicitly percentages
// (repeating gradients tile first→last stop, so their leading edge is never implied)
Expand Down Expand Up @@ -598,6 +598,12 @@ let user_gradient = $derived(gensyntax[$gradient_type](
{/if}

<footer class="end-of-stops">
<button class="reverse-stops" onclick={() => $gradient_stops = reverseStops($gradient_stops)} disabled={$gradient_stops.filter(s => s.kind === 'stop').length < 2} aria-label="Reverse color stops" use:tooltip={{content: 'Reverse color stops: flips the colors end to end, positions and hints mirror with them', placement: 'top'}}>
<svg aria-hidden="true" viewBox="0 0 24 24">
<path fill="currentColor" d="M6.99 11L3 15l3.99 4v-3H14v-2H6.99v-3zM21 9l-3.99-4v3H10v2h7.01v3L21 9z"/>
</svg>
Reverse
</button>
<button class="add-color" onclick={() => addStop()}>
Add a random color
<svg viewBox="0 0 24 24">
Expand All @@ -615,6 +621,7 @@ let user_gradient = $derived(gensyntax[$gradient_type](
<style>
:global(html) {
scrollbar-color: var(--surface-4) #0000;
-webkit-tap-highlight-color: transparent;
}

.import-btn { align-self: start; }
Expand Down Expand Up @@ -911,6 +918,16 @@ let user_gradient = $derived(gensyntax[$gradient_type](
align-self: end;
}

.reverse-stops {
align-self: end;
padding-inline: var(--size-3);
}

.reverse-stops svg {
inline-size: 1.25em;
block-size: 1.25em;
}

:global(.remove) {
padding-block: var(--size-1);
padding-inline: var(--size-2);
Expand Down Expand Up @@ -954,7 +971,10 @@ let user_gradient = $derived(gensyntax[$gradient_type](
}

.end-of-stops {
place-content: end;
display: flex;
flex-wrap: wrap;
gap: var(--size-2);
place-content: end center;
padding: var(--size-3);
margin-block-end: var(--size-fluid-2);
text-align: center;
Expand Down
5 changes: 4 additions & 1 deletion src/utils/gradientString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ function stopsToStrings(stops: any[], { convert_colors, new_lines, repeating }:
// Secondary positions are kept even when they equal the auto value so
// that explicit spans from presets/URL imports (like the Stripes preset)
// are preserved.
if (p1 != null && s.auto != null && String(p1) == String(s.auto)) p1 = null
// A span (p2 set and different) needs both ends, even when its start sits on the
// auto slot: `blue 50% 70%` written as `blue 70%` drops the solid 50-70% band.
const isSpan = p2 != null && String(p2) !== String(p1)
if (p1 != null && s.auto != null && String(p1) == String(s.auto) && !isSpan) p1 = null

// Omit browser default *leading* endpoint only when explicitly authored as 0%.
if (i === firstStopIdx) {
Expand Down
62 changes: 62 additions & 0 deletions src/utils/reverse.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { describe, it, expect } from 'vitest'
import { updateStops, reverseStops } from './stops'
import { buildGradientStrings } from './gradientString'

const layer = (stops: any[], extra: any = {}) => buildGradientStrings({
type: 'linear', space: 'srgb', interpolation: 'shorter', stops,
linear: { named_angle: 'to right', angle: null },
radial: { shape: 'circle', size: 'farthest-corner', named_position: 'center', position: { x: null, y: null } },
conic: { angle: 0, named_position: 'center', position: { x: null, y: null } },
...extra,
} as any).classic

const s = (color: string, position1: any = null, position2: any = position1) => ({ kind: 'stop', color, position1, position2 })
const h = (percentage: any = null) => ({ kind: 'hint', percentage })

describe('reverseStops', () => {
it('flips color order and keeps auto stops auto', () => {
const stops = updateStops([s('red'), h(), s('lime'), h(), s('blue')] as any)
expect(layer(reverseStops(stops))).toBe('linear-gradient(to right, #00f 0%, #0f0 50%, #f00 100%)')
})

it('mirrors explicit positions, spans and hints around 50%', () => {
const stops = updateStops([s('red', '10', '30'), h('40'), s('blue', '70')] as any)
expect(layer(reverseStops(stops))).toBe('linear-gradient(to right, #00f 30%, 60%, #f00 70% 90%)')
})

it('resolves CSS fixup before mirroring (Stripes preset spans start at 0%)', () => {
const stripes = updateStops([s('#fff'), h(), s('#000', '0', '20'), h(), s('#fff', '0', '40'), h(), s('#000', '0', '60')] as any)
expect(layer(reverseStops(stripes))).toBe(// visually black 0-60 (before the first stop), white 60-80, black 80-100
'linear-gradient(to right, #000 40% 60%, #fff 60% 80%, #000 80% 100%, #fff 100%)')
})

it('mirrors lengths with calc()', () => {
const stops = updateStops([s('red', '0', '20px'), h(), s('blue', '20px', '40px')] as any)
expect(layer(reverseStops(stops), { repeating: true })).toBe(
'repeating-linear-gradient(to right, #00f calc(100% - 40px) calc(100% - 20px), #f00 calc(100% - 20px) 100%)')
})

it('is its own inverse for explicit positions', () => {
const stops = updateStops([s('red', '5', '25'), h('35'), s('lime', '50'), h(), s('blue', '80', '95')] as any)
const before = layer(stops)
expect(layer(reverseStops(reverseStops(stops)))).toBe(before)
})

it('leaves a single stop alone', () => {
const one = updateStops([s('red')] as any)
expect(reverseStops(one)).toBe(one)
})
})

describe('span output', () => {
it('keeps a span whose start sits on the auto slot (dragging the 2nd slider of a middle stop)', () => {
const stops = updateStops([s('red'), h(), s('blue'), h(), s('lime')] as any)
;(stops[2] as any).position2 = '70'
expect(layer(updateStops(stops))).toBe('linear-gradient(to right, #f00 0%, #00f 50% 70%, #0f0 100%)')
})

it('still writes a plain auto stop with one position', () => {
const stops = updateStops([s('red'), h(), s('blue'), h(), s('lime')] as any)
expect(layer(stops)).toBe('linear-gradient(to right, #f00 0%, #00f 50%, #0f0 100%)')
})
})
57 changes: 57 additions & 0 deletions src/utils/stops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,60 @@ export function tileStops(stops: Stop[], fraction = 0.25): Stop[] {
return { ...s, percentage: round2(numericPct(s.percentage)! * fraction) } as Stop
})
}

function mirrorPos(p: any): any {
if (p == null) return p
const n = numericPct(p)
if (n != null) return round2(100 - n)
// A length (px, em, calc...) can't be mirrored without the line length; let calc() do it
const str = String(p).trim()
const inner = /^calc\(/i.test(str) ? str.slice(4) : str
return `calc(100% - ${inner})`
}

const isAuto = (value: any, auto: any) => value == null || (auto != null && String(value) === String(auto))

/**
* Flip the gradient line end to end: the last color becomes the first and every
* position p moves to 100% - p (a span a..b becomes 100-b..100-a, hints mirror
* too, since a hint is a position on the line). That's an exact mirror for
* repeating gradients as well, the tile just shifts phase.
*
* Positions are mirrored *after* CSS's fixup (CSS Images 3 §3.5.3: a stop or hint
* placed before an earlier one is clamped up to it), because authored values like
* the Stripes preset's `0% 40%` only mean `20% 40%` in context. Auto-placed stops
* and hints stay auto so the output keeps its short `red, blue` form.
*/
export function reverseStops(stops: Stop[]): Stop[] {
if (stops.filter(s => s.kind === 'stop').length < 2) return stops
let floor = -Infinity
const effective = (p: any) => {
const n = numericPct(p)
if (n == null) return p
floor = Math.max(floor, n)
return floor
}

const resolved = stops.map((s: any) => {
if (s.kind === 'stop') {
const autoP1 = isAuto(s.position1, s.auto)
const linked = s.position2 == null || String(s.position2) === String(s.position1)
const p1 = effective(autoP1 ? s.auto : s.position1)
const p2 = effective(s.position2 == null ? p1 : s.position2)
return { s, auto: autoP1 && linked, p1, p2 }
}
// auto hints are never written to the CSS, so they don't take part in fixup
const auto = isAuto(s.percentage, s.auto)
return { s, auto, p1: auto ? null : effective(s.percentage) }
})

const reversed = resolved.reverse().map(({ s, auto, p1, p2 }: any) => {
if (s.kind === 'stop') {
if (auto) return { ...s, position1: null, position2: null, auto: undefined }
return { ...s, position1: mirrorPos(p2), position2: mirrorPos(p1), auto: undefined, _manual: true }
}
if (auto) return { ...s, percentage: null, auto: undefined }
return { ...s, percentage: mirrorPos(p1), auto: undefined }
})
return updateStops(reversed as Stop[])
}
Loading