Improve and fix issues with color generation

This commit is contained in:
2024-08-10 19:36:26 +02:00
parent 84002d1856
commit 376201373e
3 changed files with 12 additions and 5 deletions

View File

@@ -1,10 +1,11 @@
<script lang="ts"> <script lang="ts">
import { GenerateColor } from "$lib/utils"; import { GenerateColor, RemoveExistingColors } from "$lib/utils";
import { main } from "$wails/models"; import { main } from "$wails/models";
import EmptyFoodComp from "./EmptyFoodComp.svelte"; import EmptyFoodComp from "./EmptyFoodComp.svelte";
import FoodComp from "./FoodComp.svelte"; import FoodComp from "./FoodComp.svelte";
export let items: main.Food[] = []; export let items: main.Food[] = [];
RemoveExistingColors();
let minCal = 1e5; let minCal = 1e5;
let maxCal = 0; let maxCal = 0;

View File

@@ -1,10 +1,11 @@
<script lang="ts"> <script lang="ts">
import { GenerateColor } from "$lib/utils"; import { GenerateColor, RemoveExistingColors } from "$lib/utils";
import EmptyWeightComp from "$components/Weight/EmptyWeightComp.svelte"; import EmptyWeightComp from "$components/Weight/EmptyWeightComp.svelte";
import WeightComp from "$components/Weight/WeightComp.svelte"; import WeightComp from "$components/Weight/WeightComp.svelte";
import { main } from "$wails/models"; import { main } from "$wails/models";
export let items: main.Weight[] = []; export let items: main.Weight[] = [];
RemoveExistingColors();
const dateColors: Map<string, string> = new Map<string, string>(); const dateColors: Map<string, string> = new Map<string, string>();

View File

@@ -68,8 +68,11 @@ function GenerateRandomHSL(): Color {
const existingColors: Color[] = []; const existingColors: Color[] = [];
function RemoveExistingColors() {
existingColors.length = 0;
}
function GenerateColor(): string { function GenerateColor(): string {
const minDistance = 15; const minDistance = 5;
let newColor: Color; let newColor: Color;
let isDistinct = false; let isDistinct = false;
@@ -89,8 +92,10 @@ function GenerateColor(): string {
break; break;
} }
} }
if (isDistinct) {
existingColors.push(newColor); existingColors.push(newColor);
} }
}
// We can not reach this point without having a color generated // We can not reach this point without having a color generated
// @ts-ignore // @ts-ignore
@@ -104,5 +109,5 @@ function LerpColor(color1: Color, color2: Color, t: number): Color {
return { h, s, l }; return { h, s, l };
} }
export { GenerateColor, LerpColor }; export { GenerateColor, LerpColor, RemoveExistingColors };
export type { Color }; export type { Color };