mirror of
https://github.com/portapack-mayhem/mayhem-firmware.git
synced 2026-07-26 10:38:52 +00:00
245a36459f
* Fix BLE RX RSSI and Channel scale * Properly reset gradient index 0 * Use move constructor in MenuView::add_item * Avoid erasing during iteration * Fix gradient rounding. Extend to end of table * Add comment to add_item
103 lines
2.8 KiB
C++
103 lines
2.8 KiB
C++
/*
|
|
* Copyright (C) 2025 Belousov Oleg
|
|
*
|
|
* This file is part of PortaPack.
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
* any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; see the file COPYING. If not, write to
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
* Boston, MA 02110-1301, USA.
|
|
*/
|
|
|
|
#include "gradient.hpp"
|
|
|
|
#include "convert.hpp"
|
|
#include "file_reader.hpp"
|
|
namespace fs = std::filesystem;
|
|
|
|
const std::filesystem::path default_gradient_file = u"waterfall.txt";
|
|
|
|
Gradient::Gradient() {
|
|
step(0, 0, 0, 0);
|
|
}
|
|
|
|
void Gradient::set_default() {
|
|
step(0, 0, 0, 0);
|
|
step(86, 0, 0, 255);
|
|
step(171, 0, 255, 0);
|
|
step(255, 255, 0, 0);
|
|
}
|
|
|
|
bool Gradient::load_file(const std::filesystem::path& file_path) {
|
|
File gradient_file;
|
|
auto error = gradient_file.open(file_path.string());
|
|
|
|
if (error)
|
|
return false;
|
|
|
|
// make sure the table starts at 0
|
|
step(0, 0, 0, 0);
|
|
|
|
auto reader = FileLineReader(gradient_file);
|
|
for (const auto& line : reader) {
|
|
if (line.length() == 0 || line[0] == '#')
|
|
continue; // Empty or comment line.
|
|
|
|
auto cols = split_string(line, ',');
|
|
|
|
if (cols.size() == 4) {
|
|
int16_t index, r, g, b;
|
|
|
|
if (!parse_int(cols[0], index) || index < 0 || index > 255)
|
|
continue;
|
|
|
|
if (!parse_int(cols[1], r) || r < 0 || r > 255)
|
|
continue;
|
|
|
|
if (!parse_int(cols[2], g) || g < 0 || g > 255)
|
|
continue;
|
|
|
|
if (!parse_int(cols[3], b) || b < 0 || b > 255)
|
|
continue;
|
|
|
|
step(index, r, g, b);
|
|
}
|
|
}
|
|
|
|
// extend the gradient from the current index to the end of the table
|
|
step(255, prev_r, prev_g, prev_b);
|
|
|
|
return true;
|
|
}
|
|
|
|
void Gradient::step(int16_t index, int16_t r, int16_t g, int16_t b) {
|
|
// prevent division by 0 if index == prev_index
|
|
int16_t range = index - prev_index;
|
|
if (range == 0) range = 1;
|
|
|
|
for (int16_t i = prev_index; i <= index; i++) {
|
|
int16_t t = i - prev_index;
|
|
|
|
int16_t new_r = prev_r + (t * (r - prev_r) + range / 2) / range;
|
|
int16_t new_g = prev_g + (t * (g - prev_g) + range / 2) / range;
|
|
int16_t new_b = prev_b + (t * (b - prev_b) + range / 2) / range;
|
|
|
|
if (i <= 255) lut[i] = ui::Color(new_r, new_g, new_b);
|
|
}
|
|
|
|
prev_index = index;
|
|
prev_r = r;
|
|
prev_g = g;
|
|
prev_b = b;
|
|
}
|