mirror of
https://github.com/gpxstudio/gpx.studio.git
synced 2026-02-06 00:13:09 +00:00
improve color management
This commit is contained in:
@@ -34,11 +34,10 @@
|
|||||||
import { editStyle } from '$lib/components/file-list/style/utils.svelte';
|
import { editStyle } from '$lib/components/file-list/style/utils.svelte';
|
||||||
import { getSymbolKey, symbols } from '$lib/assets/symbols';
|
import { getSymbolKey, symbols } from '$lib/assets/symbols';
|
||||||
import { selection, copied, cut } from '$lib/logic/selection';
|
import { selection, copied, cut } from '$lib/logic/selection';
|
||||||
import { map } from '$lib/components/map/map';
|
|
||||||
import { fileActions, pasteSelection } from '$lib/logic/file-actions';
|
import { fileActions, pasteSelection } from '$lib/logic/file-actions';
|
||||||
import { allHidden } from '$lib/logic/hidden';
|
import { allHidden } from '$lib/logic/hidden';
|
||||||
import { boundsManager } from '$lib/logic/bounds';
|
import { boundsManager } from '$lib/logic/bounds';
|
||||||
import { gpxLayers } from '$lib/components/map/gpx-layer/gpx-layers';
|
import { gpxColors, gpxLayers } from '$lib/components/map/gpx-layer/gpx-layers';
|
||||||
import { fileStateCollection } from '$lib/logic/file-state';
|
import { fileStateCollection } from '$lib/logic/file-state';
|
||||||
import { waypointPopup } from '$lib/components/map/gpx-layer/gpx-layer-popup';
|
import { waypointPopup } from '$lib/components/map/gpx-layer/gpx-layer-popup';
|
||||||
import { allowedPastes } from './sortable-file-list';
|
import { allowedPastes } from './sortable-file-list';
|
||||||
@@ -58,19 +57,11 @@
|
|||||||
|
|
||||||
let singleSelection = $derived($selection.size === 1);
|
let singleSelection = $derived($selection.size === 1);
|
||||||
|
|
||||||
let nodeColors: string[] = $state([]);
|
let nodeColors: string[] = $derived.by(() => {
|
||||||
|
|
||||||
$effect.pre(() => {
|
|
||||||
let colors: string[] = [];
|
let colors: string[] = [];
|
||||||
if (node && $map) {
|
if (node) {
|
||||||
if (node instanceof GPXFile) {
|
if (node instanceof GPXFile) {
|
||||||
let defaultColor = undefined;
|
let defaultColor = $gpxColors.get(item.getFileId());
|
||||||
|
|
||||||
let layer = gpxLayers.getLayer(item.getFileId());
|
|
||||||
if (layer) {
|
|
||||||
defaultColor = layer.layerColor;
|
|
||||||
}
|
|
||||||
|
|
||||||
let style = node.getStyle(defaultColor);
|
let style = node.getStyle(defaultColor);
|
||||||
colors = style.color;
|
colors = style.color;
|
||||||
} else if (node instanceof Track) {
|
} else if (node instanceof Track) {
|
||||||
@@ -83,14 +74,14 @@
|
|||||||
colors.push(style['gpx_style:color']);
|
colors.push(style['gpx_style:color']);
|
||||||
}
|
}
|
||||||
if (colors.length === 0) {
|
if (colors.length === 0) {
|
||||||
let layer = gpxLayers.getLayer(item.getFileId());
|
let defaultColor = $gpxColors.get(item.getFileId());
|
||||||
if (layer) {
|
if (defaultColor) {
|
||||||
colors.push(layer.layerColor);
|
colors.push(defaultColor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
nodeColors = colors;
|
return colors;
|
||||||
});
|
});
|
||||||
|
|
||||||
let symbolKey = $derived(node instanceof Waypoint ? getSymbolKey(node.sym) : undefined);
|
let symbolKey = $derived(node instanceof Waypoint ? getSymbolKey(node.sym) : undefined);
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import { fileActionManager } from '$lib/logic/file-action-manager';
|
|||||||
import { fileActions } from '$lib/logic/file-actions';
|
import { fileActions } from '$lib/logic/file-actions';
|
||||||
import { splitAs } from '$lib/components/toolbar/tools/scissors/scissors';
|
import { splitAs } from '$lib/components/toolbar/tools/scissors/scissors';
|
||||||
import { mapCursor, MapCursorState } from '$lib/logic/map-cursor';
|
import { mapCursor, MapCursorState } from '$lib/logic/map-cursor';
|
||||||
|
import { gpxColors } from '$lib/components/map/gpx-layer/gpx-layers';
|
||||||
|
|
||||||
const colors = [
|
const colors = [
|
||||||
'#ff0000',
|
'#ff0000',
|
||||||
@@ -43,16 +44,35 @@ for (let color of colors) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get the color with the least amount of uses
|
// Get the color with the least amount of uses
|
||||||
function getColor() {
|
function getColor(fileId: string) {
|
||||||
let color = colors.reduce((a, b) => (colorCount[a] <= colorCount[b] ? a : b));
|
let color = colors.reduce((a, b) => (colorCount[a] <= colorCount[b] ? a : b));
|
||||||
colorCount[color]++;
|
colorCount[color]++;
|
||||||
|
gpxColors.update((colors) => {
|
||||||
|
colors.set(fileId, color);
|
||||||
|
return colors;
|
||||||
|
});
|
||||||
return color;
|
return color;
|
||||||
}
|
}
|
||||||
|
|
||||||
function decrementColor(color: string) {
|
function replaceColor(fileId: string, oldColor: string, newColor: string) {
|
||||||
|
if (colorCount.hasOwnProperty(oldColor)) {
|
||||||
|
colorCount[oldColor]--;
|
||||||
|
}
|
||||||
|
colorCount[newColor]++;
|
||||||
|
gpxColors.update((colors) => {
|
||||||
|
colors.set(fileId, newColor);
|
||||||
|
return colors;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeColor(fileId: string, color: string) {
|
||||||
if (colorCount.hasOwnProperty(color)) {
|
if (colorCount.hasOwnProperty(color)) {
|
||||||
colorCount[color]--;
|
colorCount[color]--;
|
||||||
}
|
}
|
||||||
|
gpxColors.update((colors) => {
|
||||||
|
colors.delete(fileId);
|
||||||
|
return colors;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getSvgForSymbol(symbol?: string | undefined, layerColor?: string | undefined) {
|
export function getSvgForSymbol(symbol?: string | undefined, layerColor?: string | undefined) {
|
||||||
@@ -121,7 +141,7 @@ export class GPXLayer {
|
|||||||
constructor(fileId: string, file: Readable<GPXFileWithStatistics | undefined>) {
|
constructor(fileId: string, file: Readable<GPXFileWithStatistics | undefined>) {
|
||||||
this.fileId = fileId;
|
this.fileId = fileId;
|
||||||
this.file = file;
|
this.file = file;
|
||||||
this.layerColor = getColor();
|
this.layerColor = getColor(fileId);
|
||||||
this.unsubscribe.push(
|
this.unsubscribe.push(
|
||||||
map.subscribe(($map) => {
|
map.subscribe(($map) => {
|
||||||
if ($map) {
|
if ($map) {
|
||||||
@@ -158,7 +178,7 @@ export class GPXLayer {
|
|||||||
file._data.style.color &&
|
file._data.style.color &&
|
||||||
this.layerColor !== `#${file._data.style.color}`
|
this.layerColor !== `#${file._data.style.color}`
|
||||||
) {
|
) {
|
||||||
decrementColor(this.layerColor);
|
replaceColor(this.fileId, this.layerColor, `#${file._data.style.color}`);
|
||||||
this.layerColor = `#${file._data.style.color}`;
|
this.layerColor = `#${file._data.style.color}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -364,7 +384,7 @@ export class GPXLayer {
|
|||||||
|
|
||||||
this.unsubscribe.forEach((unsubscribe) => unsubscribe());
|
this.unsubscribe.forEach((unsubscribe) => unsubscribe());
|
||||||
|
|
||||||
decrementColor(this.layerColor);
|
removeColor(this.fileId, this.layerColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
moveToFront() {
|
moveToFront() {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { GPXFileStateCollectionObserver } from '$lib/logic/file-state';
|
import { GPXFileStateCollectionObserver } from '$lib/logic/file-state';
|
||||||
|
import { writable } from 'svelte/store';
|
||||||
import { GPXLayer } from './gpx-layer';
|
import { GPXLayer } from './gpx-layer';
|
||||||
|
|
||||||
export class GPXLayerCollection {
|
export class GPXLayerCollection {
|
||||||
@@ -42,3 +43,4 @@ export class GPXLayerCollection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const gpxLayers = new GPXLayerCollection();
|
export const gpxLayers = new GPXLayerCollection();
|
||||||
|
export const gpxColors = writable(new Map<string, string>());
|
||||||
|
|||||||
Reference in New Issue
Block a user