Linear Gradients
This commit is contained in:
parent
14c7ba87f7
commit
9180bc84de
@ -133,23 +133,42 @@ WatchFaceTerminal::~WatchFaceTerminal() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::tuple<float, float, float> hexToFloat(int rgb) {
|
std::tuple<float, float, float> hexToFloat(int rgb) {
|
||||||
float r = ((rgb >> 16) & 0xFF) / 255;
|
float r = ((rgb >> 16) & 0xFF);
|
||||||
float g = ((rgb >> 8) & 0xFF) / 255;
|
float g = ((rgb >> 8) & 0xFF);
|
||||||
float b = (rgb & 0xFF) / 255;
|
float b = (rgb & 0xFF);
|
||||||
return std::tuple<float, float, float>(r, g, b);
|
return std::tuple<float, float, float>(r, g, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float normalize(float value) {
|
||||||
|
if (value < 0.0f) {
|
||||||
|
return 0.0f;
|
||||||
|
} else if (value > 1.0f) {
|
||||||
|
return 1.0f;
|
||||||
|
} else {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// reference: https://dev.to/ndesmic/linear-color-gradients-from-scratch-1a0e
|
// reference: https://dev.to/ndesmic/linear-color-gradients-from-scratch-1a0e
|
||||||
|
|
||||||
std::tuple<float, float, float> lerp(std::tuple<float, float, float> pointA, std::tuple<float, float, float> pointB, float normalValue) {
|
std::tuple<float, float, float> lerp(std::tuple<float, float, float> pointA, std::tuple<float, float, float> pointB, float normalValue) {
|
||||||
return std::tuple<float, float, float>(
|
NRF_LOG_INFO("Normal value: %f", normalValue);
|
||||||
|
auto lerpOutput = std::tuple<float, float, float>(
|
||||||
get<0>(pointA) + (get<0>(pointB) - get<0>(pointA)) * normalValue,
|
get<0>(pointA) + (get<0>(pointB) - get<0>(pointA)) * normalValue,
|
||||||
get<1>(pointA) + (get<1>(pointB) - get<1>(pointA)) * normalValue,
|
get<1>(pointA) + (get<1>(pointB) - get<1>(pointA)) * normalValue,
|
||||||
get<2>(pointA) + (get<2>(pointB) - get<2>(pointA)) * normalValue
|
get<2>(pointA) + (get<2>(pointB) - get<2>(pointA)) * normalValue
|
||||||
|
//std::lerp(get<0>(pointA), get<0>(pointB), normalValue),
|
||||||
|
//std::lerp(get<1>(pointA), get<1>(pointB), normalValue),
|
||||||
|
//std::lerp(get<2>(pointA), get<2>(pointB), normalValue)
|
||||||
);
|
);
|
||||||
|
NRF_LOG_INFO("pointA: %f, %f, %f", get<0>(pointA), get<1>(pointA), get<2>(pointA));
|
||||||
|
NRF_LOG_INFO("pointB: %f, %f, %f", get<0>(pointB), get<1>(pointB), get<2>(pointB));
|
||||||
|
NRF_LOG_INFO("lerp: %f, %f, %f", get<0>(lerpOutput), get<1>(lerpOutput), get<2>(lerpOutput));
|
||||||
|
return lerpOutput;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* TemperatureColor(int16_t temperature) {
|
const char* TemperatureColor(int16_t temperature) {
|
||||||
const std::vector<int> colors = {0x0000ff, 0xff9b00, 0xff0000};
|
const std::vector<int> colors = {0x5555ff, 0xff9b00, 0xff0000};
|
||||||
std::vector<std::tuple<float, float, float>> stops;
|
std::vector<std::tuple<float, float, float>> stops;
|
||||||
for (auto colorVal: colors) {
|
for (auto colorVal: colors) {
|
||||||
stops.emplace_back(hexToFloat(colorVal));
|
stops.emplace_back(hexToFloat(colorVal));
|
||||||
@ -158,11 +177,15 @@ WatchFaceTerminal::~WatchFaceTerminal() {
|
|||||||
if (tempRounded < 0) {
|
if (tempRounded < 0) {
|
||||||
tempRounded = 1;
|
tempRounded = 1;
|
||||||
}
|
}
|
||||||
// convert temperature to range between 0 and 1
|
// convert temperature to range between newMin and newMax
|
||||||
float oldRange = (56 - 0);
|
float oldMax = 100;
|
||||||
float newRange = (1 - 0);
|
float oldMin = 0;
|
||||||
float newValue = (((tempRounded - 0) * newRange) / oldRange) + 1;
|
float newMax = 1;
|
||||||
|
float newMin = 0;
|
||||||
|
float oldRange = (oldMax - oldMin);
|
||||||
|
float newRange = (newMax - newMin);
|
||||||
|
float newValue = (((tempRounded - oldMin) * newRange) / oldRange) + newMin;
|
||||||
|
newValue = normalize(newValue);
|
||||||
if (newValue <= .5f) {
|
if (newValue <= .5f) {
|
||||||
return floatToRgbHex(lerp(stops[0], stops[1], newValue));
|
return floatToRgbHex(lerp(stops[0], stops[1], newValue));
|
||||||
} else {
|
} else {
|
||||||
@ -222,7 +245,7 @@ void WatchFaceTerminal::Refresh() {
|
|||||||
temp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(temp);
|
temp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(temp);
|
||||||
tempUnit = 'F';
|
tempUnit = 'F';
|
||||||
}
|
}
|
||||||
auto color = "ff9b00";
|
auto color = TemperatureColor(temp);
|
||||||
//TemperatureColor(temp);
|
//TemperatureColor(temp);
|
||||||
NRF_LOG_INFO("Color hex: %s", color);
|
NRF_LOG_INFO("Color hex: %s", color);
|
||||||
lv_label_set_text_fmt(weatherStatus, "[WTHR]#%s %d#°%c %s", color, RoundTemperature(temp), tempUnit, WeatherString(weatherId));
|
lv_label_set_text_fmt(weatherStatus, "[WTHR]#%s %d#°%c %s", color, RoundTemperature(temp), tempUnit, WeatherString(weatherId));
|
||||||
|
Loading…
Reference in New Issue
Block a user