Skip to content
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

[FIX] Use individual relative stat percentage for stat color #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
27 changes: 15 additions & 12 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,17 @@ function renderPokemonIndex(pokemons: Array<Pokemon>): string {
<td class="value abilities-text">${ability.description}</td>
</tr>`;
})
.join('\n');
const getStatBar = (value: number, maxValue: number) => {
const percentage = Math.round((value / maxValue) * 100);
const greenThreshold = 35;
const yellowThreshold = 20;
.join('\n');
const getStatBar = (value: number, sumValues: number, maxValue: number) => {
const absolute_percentage = Math.round((value / maxValue) * 100);
const relative_percentage = Math.round((value / sumValues) * 100);
const greenThreshold = 100 / (6 - 1); // 6: because there are 6 stats
const yellowThreshold = 100 / (6 + 1); // +-1: to move a bit more/less from 6

let backgroundColor;
if (percentage >= greenThreshold) {
if (relative_percentage >= greenThreshold) {
backgroundColor = 'limegreen';
} else if (percentage >= yellowThreshold) {
} else if (relative_percentage >= yellowThreshold) {
backgroundColor = 'gold';
} else {
backgroundColor = 'tomato';
Expand All @@ -142,20 +143,22 @@ function renderPokemonIndex(pokemons: Array<Pokemon>): string {
return `
<div class="stat-bar-container">
<div class="stat-bar">
<div class="stat-value" style="width: ${percentage}%; background-color: ${backgroundColor};"></div>
<div class="stat-value" style="width: ${absolute_percentage}%; background-color: ${backgroundColor};"></div>
</div>
</div>
`;
};

const statsTableRows = pokemon.stats
.map(stat => `
.map(stat => {
const sum_stats = pokemon.stats.reduce((a, b) => a.value + b.value);
return `
<tr>
<td class="attribute abilities-text">${(stat.name === 'hp') ? 'HP': stat.name.charAt(0).toUpperCase() + stat.name.slice(1)}:</td>
<td class="value abilities-text">${stat.value}</td>
<td class="value abilities-bar">${getStatBar(stat.value, 255)}</td>
</tr>`)
.join('\n');
<td class="value abilities-bar">${getStatBar(stat.value, sum_stats, 255)}</td>
</tr>`;
}).join('\n');

const descriptionsSelect = pokemon.pokedexDescriptions
.map(description => `
Expand Down