Skip to content

Commit ee69191

Browse files
authored
Merge pull request #145 from Leogendra/develop
Develop v1.8.5 with bug fixes and improvements
2 parents de097f8 + c618f3b commit ee69191

File tree

11 files changed

+97
-34
lines changed

11 files changed

+97
-34
lines changed

assets/pixels/score_1.svg

Lines changed: 4 additions & 4 deletions
Loading

assets/pixels/score_2.svg

Lines changed: 4 additions & 4 deletions
Loading

assets/pixels/score_3.svg

Lines changed: 4 additions & 4 deletions
Loading

assets/pixels/score_4.svg

Lines changed: 4 additions & 4 deletions
Loading

assets/pixels/score_5.svg

Lines changed: 4 additions & 4 deletions
Loading

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ <h3>Map Custom Moods to Scores</h3>
540540
<img src="assets/logos/kofi.png" alt="Ko-fi">
541541
</a>
542542
</div>
543-
<div class="version">Version: v1.8.4</div>
543+
<div class="version">Version: v1.8.5</div>
544544
</footer>
545545

546546
<!-- Charts.js for graphs -->

scripts/card.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ async function load_colored_score_SVG(score) {
2222
const text = await res.text();
2323
const doc = new DOMParser().parseFromString(text, "image/svg+xml");
2424
const svg = doc.querySelector("svg");
25-
svg.style.color = png_settings.colors[score];
25+
const scoreColor = png_settings.colors[score];
26+
svg.classList.add("color-icon");
27+
svg.style.color = scoreColor;
28+
29+
const bgColor = get_contrasting_text_color(scoreColor, highTreshold = 225);
30+
svg.style.setProperty('--backgroundColor', bgColor);
31+
2632
return svg;
2733
}
2834

scripts/png.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,14 @@ async function update_svg_color(score, color) {
9696
const svg = document.querySelector(`#color${score}`).parentElement.querySelector("svg");
9797
if (svg) {
9898
svg.style.color = color;
99+
const bgColor = get_contrasting_text_color(color, highTreshold = 225);
100+
svg.style.setProperty('--backgroundColor', bgColor);
99101
}
100102
png_settings.colors[score] = color;
101103
}
102104

103105

104106
async function setup_palette_settings() {
105-
const colors = png_settings.colors;
106-
107107
for (let score = 1; score <= 5; score++) {
108108
const cell = document.querySelector(`#color${score}`).parentElement;
109109
const input = document.getElementById(`color${score}`);
@@ -113,11 +113,10 @@ async function setup_palette_settings() {
113113
let old_svg = cell.querySelector("svg");
114114
if (!old_svg) {
115115
const svg = await load_colored_score_SVG(score);
116-
svg.classList.add("color-icon");
117-
svg.style.color = colors[score];
118-
119116
input.addEventListener("input", () => {
120117
update_svg_color(score, input.value);
118+
btn_save_palette_settings.style.display = "flex";
119+
btn_reset_palette_settings.style.display = "flex";
121120
});
122121

123122
cell.appendChild(svg);
@@ -328,7 +327,7 @@ async function generate_pixels_PNG() {
328327
const direction = layout.includes("vertical") ? "col" : "row";
329328
const isWeek = layout.includes("weeks");
330329
const textColor = get_contrasting_text_color(colors.empty);
331-
const lightTextColor = get_contrasting_text_color(colors.empty, less = true);
330+
const lightTextColor = get_contrasting_text_color(colors.empty, highTreshold = 186, less = true);
332331

333332
// create a map of pixels by date
334333
const pixel_map = new Map();
@@ -1140,11 +1139,14 @@ btn_open_dialog_settings.addEventListener("click", () => {
11401139

11411140
btn_reset_palette_settings.addEventListener("click", () => {
11421141
png_settings.colors = { ...png_default_settings.colors };
1142+
btn_save_palette_settings.style.display = "none";
1143+
btn_reset_palette_settings.style.display = "none";
11431144
close_dialog_settings();
11441145
});
11451146

11461147
btn_save_palette_settings.addEventListener("click", () => {
11471148
close_dialog_settings(save = true);
1149+
btn_save_palette_settings.style.display = "none";
11481150
show_popup_message("Palette settings saved", "success", 3000);
11491151
});
11501152

scripts/statistics.js

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,53 @@ function calculate_streaks() {
4646
}
4747

4848

49+
function calculate_best_day_of_year() {
50+
const dayStats = {}; // { "MM-DD": { total: X, count: Y } }
51+
52+
current_data.forEach(entry => {
53+
const avgScore = average(entry.scores);
54+
if (!avgScore || !entry.date) { return; }
55+
56+
const date = new Date(entry.date);
57+
const month = String(date.getMonth());
58+
const day = String(date.getDate());
59+
const dayKey = `${month}-${day}`;
60+
61+
if (!dayStats[dayKey]) {
62+
dayStats[dayKey] = { total: 0, count: 0 };
63+
}
64+
dayStats[dayKey].total += avgScore;
65+
dayStats[dayKey].count += 1;
66+
});
67+
68+
let bestDay = null;
69+
let bestAvg = 0;
70+
71+
Object.entries(dayStats).forEach(([dayKey, stats]) => {
72+
const avg = stats.total / stats.count;
73+
if (avg > bestAvg) {
74+
bestAvg = avg;
75+
bestDay = dayKey;
76+
}
77+
});
78+
79+
if (!bestDay) {
80+
return { day: "N/A", score: 0 };
81+
}
82+
83+
const [month, day] = bestDay.split('-').map(Number);
84+
const monthName = new Date(0, month).toLocaleString(userLocale, { month: 'long' });
85+
const formattedDay = `${day} ${monthName}`;
86+
87+
return { day: formattedDay, score: bestAvg };
88+
}
89+
90+
4991
function calculate_and_display_stats() {
5092
const allScores = current_data.flatMap(entry => entry.scores);
5193
const streaks = calculate_streaks();
5294
const moodCounts = {};
95+
const bestDayInfos = calculate_best_day_of_year();
5396
averageScore = average(allScores);
5497
nbTotalDays = current_data.filter(entry => entry.scores.length > 0).length;
5598

@@ -58,10 +101,10 @@ function calculate_and_display_stats() {
58101
});
59102

60103
stats_array = [
61-
{ title: "Number of Pixels", value: `<p>${nbTotalDays}</p>` },
62-
{ title: "Average score", value: `<p>${averageScore.toFixed(2)}</p>` },
104+
{ title: "Number of Pixels", value: `<p>${nbTotalDays} | Avg score: ${averageScore.toFixed(2)}</p>` },
63105
{ title: "Streaks", value: `<p>Last: ${streaks.currentStreak} | Best: ${streaks.bestStreak}</p>` },
64-
{ title: "Score distribution", value: `<canvas title="Click to enlarge" id="scoresPieChart" class="pie-chart" width="100" height="100"></canvas>` },
106+
{ title: "Best day of the year", value: `<p>${bestDayInfos.day}, score: ${bestDayInfos.score.toFixed(2)}</p>` },
107+
{ title: "Score distribution (click)", value: `<canvas title="Click to enlarge" id="scoresPieChart" class="pie-chart" width="100" height="100"></canvas>` },
65108
];
66109

67110
stats_container.innerHTML = stats_array.map(({ title, value }, index) => `

scripts/utils.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,17 +214,17 @@ function RGB_to_hex({ r, g, b }) {
214214
}
215215

216216

217-
function get_contrasting_text_color(bgColor, less = false) {
217+
function get_contrasting_text_color(bgColor, highTreshold = 186, less = false) {
218218
const hex = bgColor.replace("#", "");
219219
const r = parseInt(hex.substr(0, 2), 16);
220220
const g = parseInt(hex.substr(2, 2), 16);
221221
const b = parseInt(hex.substr(4, 2), 16);
222222

223223
const luminance = (0.299 * r + 0.587 * g + 0.114 * b);
224224
if (less) {
225-
return luminance > 186 ? "#808080" : "#d3d3d3";
225+
return luminance > highTreshold ? "#808080" : "#d3d3d3";
226226
}
227-
return luminance > 186 ? "#000000" : "#ffffff";
227+
return luminance > highTreshold ? "#000000" : "#ffffff";
228228
}
229229

230230

0 commit comments

Comments
 (0)