Skip to content

Commit

Permalink
feat(map): add country fill fade map setting
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelMakesGames committed Feb 13, 2024
1 parent 3153b4f commit 02e74ed
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 29 deletions.
23 changes: 20 additions & 3 deletions src/renderer/src/lib/ExportModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
} from '@skeletonlabs/skeleton';
import convertSvgToPng from './convertSvgToPng';
import type { MapData } from './map/data/processMapData';
import { getFillColorAttributes, resolveColor } from './map/mapUtils';
import {
approximateBorderFadeOpacity,
getFillColorAttributes,
resolveColor,
} from './map/mapUtils';
import { mapSettings } from './mapSettings';
import stellarMapsApi from './stellarMapsApi';
import { toastError } from './utils';
Expand Down Expand Up @@ -330,7 +334,13 @@
mapSettings: $mapSettings,
colors,
countryColors: border,
colorStack: [$mapSettings.borderColor, $mapSettings.borderFillColor],
colorStack: [
$mapSettings.borderColor,
approximateBorderFadeOpacity(
$mapSettings.borderFillColor,
$mapSettings.borderFillFade,
),
],
})}
/>
<path
Expand All @@ -339,7 +349,14 @@
mapSettings: $mapSettings,
colors,
countryColors: border,
colorStack: [$mapSettings.borderFillColor],
colorStack: [
// normally only use this approximation when for background colors
// but it helps this simplified preview reflect the map
approximateBorderFadeOpacity(
$mapSettings.borderFillColor,
$mapSettings.borderFillFade,
),
],
})}
/>
{/each}
Expand Down
44 changes: 36 additions & 8 deletions src/renderer/src/lib/map/CountryBorders.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import Glow from './Glow.svelte';
import type { MapData } from './data/processMapData';
import {
approximateBorderFadeOpacity,
getFillColorAttributes,
getStrokeAttributes,
getStrokeColorAttributes,
Expand All @@ -26,13 +27,31 @@
<path
id="border-{border.countryId}-inner"
d={border.innerPath}
{...getFillColorAttributes({
mapSettings: $mapSettings,
colors,
countryColors: border,
colorStack: [$mapSettings.borderFillColor],
})}
{...$mapSettings.borderFillFade === 0
? getFillColorAttributes({
mapSettings: $mapSettings,
colors,
countryColors: border,
colorStack: [$mapSettings.borderFillColor],
})
: { fill: 'none' }}
/>
{#if $mapSettings.borderFillFade > 0}
<path
id="border-{border.countryId}-inner"
d={border.innerPath}
clip-path={`url(#border-${border.countryId}-inner-clip-path)`}
stroke-width={(1 - $mapSettings.borderFillFade) * 25}
filter="url(#fade)"
fill="none"
{...getStrokeColorAttributes({
mapSettings: $mapSettings,
colors,
countryColors: border,
colorStack: [$mapSettings.borderFillColor],
})}
/>
{/if}
{#each border.sectorBorders.filter(filterSectorBorders) as sectorBorder}
<Glow
enabled={sectorBorder.isUnionBorder && $mapSettings.unionBorderStroke.enabled
Expand All @@ -53,7 +72,13 @@
mapSettings: $mapSettings,
colors,
countryColors: border,
colorStack: [$mapSettings.sectorBorderColor, $mapSettings.borderFillColor],
colorStack: [
$mapSettings.sectorBorderColor,
approximateBorderFadeOpacity(
$mapSettings.borderFillColor,
$mapSettings.borderFillFade,
),
],
})}
fill="none"
/>
Expand All @@ -68,7 +93,10 @@
mapSettings: $mapSettings,
colors,
countryColors: border,
colorStack: [$mapSettings.borderColor, $mapSettings.borderFillColor],
colorStack: [
$mapSettings.borderColor,
approximateBorderFadeOpacity($mapSettings.borderFillColor, $mapSettings.borderFillFade),
],
})}
{filter}
/>
Expand Down
16 changes: 13 additions & 3 deletions src/renderer/src/lib/map/Hyperlanes.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
import { isColorDynamic, mapSettings } from '../mapSettings';
import Glow from './Glow.svelte';
import type { MapData } from './data/processMapData';
import { getStrokeAttributes, getStrokeColorAttributes } from './mapUtils';
import {
approximateBorderFadeOpacity,
getStrokeAttributes,
getStrokeColorAttributes,
} from './mapUtils';
export let data: MapData;
export let colors: Record<string, string>;
Expand Down Expand Up @@ -79,7 +83,10 @@
mapSettings: $mapSettings,
colors,
countryColors: border,
colorStack: [$mapSettings.hyperlaneColor, $mapSettings.borderFillColor],
colorStack: [
$mapSettings.hyperlaneColor,
approximateBorderFadeOpacity($mapSettings.borderFillColor, $mapSettings.borderFillFade),
],
})}
{...getStrokeAttributes($mapSettings.hyperlaneStroke)}
{filter}
Expand All @@ -95,7 +102,10 @@
mapSettings: $mapSettings,
colors,
countryColors: border,
colorStack: [$mapSettings.hyperRelayColor, $mapSettings.borderFillColor],
colorStack: [
$mapSettings.hyperRelayColor,
approximateBorderFadeOpacity($mapSettings.borderFillColor, $mapSettings.borderFillFade),
],
})}
{...getStrokeAttributes($mapSettings.hyperRelayStroke)}
{filter}
Expand Down
13 changes: 13 additions & 0 deletions src/renderer/src/lib/map/Map.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@
other approaches resulted in a pixellated look when zoomed in (at least when rendered with WebKitGTK)
-->
</filter>
<filter
id="fade"
filterUnits="objectBoundingBox"
x="-50%"
y="-50%"
width="200%"
height="200%"
>
<feGaussianBlur
in="SourceGraphic"
stdDeviation={(1 - $mapSettings.borderFillFade) * 25}
/>
</filter>
{/each}
{/if}
<Icons />
Expand Down
7 changes: 5 additions & 2 deletions src/renderer/src/lib/map/SystemIcons.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
type MapSettings,
} from '../mapSettings';
import type { MapData } from './data/processMapData';
import { getFillColorAttributes } from './mapUtils';
import { approximateBorderFadeOpacity, getFillColorAttributes } from './mapUtils';
export let data: MapData;
export let colors: Record<string, string>;
Expand Down Expand Up @@ -113,7 +113,10 @@
mapSettings: $mapSettings,
colors,
countryColors: systemIcons.system,
colorStack: [systemIcon.color, $mapSettings.borderFillColor],
colorStack: [
systemIcon.color,
approximateBorderFadeOpacity($mapSettings.borderFillColor, $mapSettings.borderFillFade),
],
})}
/>
{/each}
Expand Down
37 changes: 28 additions & 9 deletions src/renderer/src/lib/map/mapUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,6 @@ import type {
MapSettings,
StrokeSetting,
} from '../mapSettings';
import type { MapData } from './data/processMapData';

function getDisplayedBorders(data: MapData, settings: MapSettings) {
return data.borders.filter((border) => border.isKnown || !settings.terraIncognita);
}
interface ColorConfig {
value: ColorSetting;
background?: ColorConfig;
}

export function resolveColor({
mapSettings,
Expand Down Expand Up @@ -158,3 +149,31 @@ export function getFillColorAttributes(resolveColorOptions: Parameters<typeof re
'fill-opacity': opacity,
};
}

// used for approximating country fill color when the fading effect is active
// this is needed for reasonable MIN_CONTRAST behavior
export function approximateBorderFadeOpacity(
colorSetting: ColorSetting,
fade: number,
): ColorSetting {
const approximateOpacity = fade === 0 ? 1 : 0.4 - 0.6 * fade;
const hasOpacity = colorSetting.colorAdjustments.some(
(adjustment) => adjustment.type === 'OPACITY',
);
if (hasOpacity) {
return {
...colorSetting,
colorAdjustments: colorSetting.colorAdjustments.map((a) =>
a.type === 'OPACITY' ? { ...a, value: a.value * approximateOpacity } : a,
),
};
} else {
return {
...colorSetting,
colorAdjustments: [
...colorSetting.colorAdjustments,
{ type: 'OPACITY', value: approximateOpacity },
],
};
}
}
19 changes: 15 additions & 4 deletions src/renderer/src/lib/mapSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ADDITIONAL_COLORS } from './colors';
import { stellarisDataPromiseStore } from './loadStellarisData';
import stellarMapsApi from './stellarMapsApi';

type NumberMapSettings = 'unionLeaderSymbolSize' | 'terraIncognitaBrightness';
type NumberMapSettings = 'unionLeaderSymbolSize' | 'terraIncognitaBrightness' | 'borderFillFade';

type NumberOptionalMapSettings =
| 'countryEmblemsMaxSize'
Expand Down Expand Up @@ -294,6 +294,15 @@ export const mapSettingConfig: MapSettingGroup[] = [
type: 'color',
hideIf: (settings) => !settings.borderStroke.enabled,
},
{
id: 'borderFillFade',
name: 'Country Fill Fade',
type: 'range',
hideIf: (settings) => !settings.borderStroke.enabled,
min: 0,
max: 1,
step: 0.05,
},
{
id: 'sectorBorderStroke',
name: 'Sector Borders',
Expand Down Expand Up @@ -682,6 +691,7 @@ const defaultMapSettings: MapSettings = {
color: 'secondary',
colorAdjustments: [{ type: 'OPACITY', value: 0.5 }],
},
borderFillFade: 0,
borderColor: { color: 'primary', colorAdjustments: [] },
borderStroke: {
enabled: true,
Expand Down Expand Up @@ -932,18 +942,19 @@ export const presetMapSettings: SavedMapSettings[] = [
color: 'secondary',
colorAdjustments: [
{
type: 'MAX_LIGHTNESS',
value: 0.15,
type: 'MIN_LIGHTNESS',
value: 0.5,
},
{ type: 'OPACITY', value: 0.5 },
],
},
borderFillFade: 0.5,
backgroundColor: {
color: 'true_black',
colorAdjustments: [],
},
borderStroke: {
...defaultMapSettings.borderStroke,
width: 1,
glow: true,
},
sectorBorderStroke: {
Expand Down

0 comments on commit 02e74ed

Please sign in to comment.