Skip to content

Commit c451d2c

Browse files
committed
Refactor battery color calculation logic into BatteryIcon::ColorFromPercentage().
1 parent 8d87a9e commit c451d2c

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

src/displayapp/screens/BatteryIcon.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "displayapp/screens/Symbols.h"
44
#include "displayapp/icons/battery/batteryicon.c"
55
#include "displayapp/InfiniTimeTheme.h"
6+
#include <lvgl/src/lv_misc/lv_color.h>
67

78
using namespace Pinetime::Applications::Screens;
89

@@ -51,3 +52,12 @@ const char* BatteryIcon::GetPlugIcon(bool isCharging) {
5152
else
5253
return "";
5354
}
55+
56+
lv_color_t BatteryIcon::ColorFromPercentage(int batteryPercent) {
57+
// HSV color model has red at 0° and green at 120°.
58+
// We lock saturation and brightness at 100% and traverse the cylinder
59+
// between red and green, thus avoiding the darker RGB on medium battery
60+
// charges and giving us a much nicer color range.
61+
const uint8_t hue = batteryPercent * 120 / 100;
62+
return lv_color_hsv_to_rgb(hue, 100, 100);
63+
}

src/displayapp/screens/BatteryIcon.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace Pinetime {
1616

1717
static const char* GetUnknownIcon();
1818
static const char* GetPlugIcon(bool isCharging);
19+
static lv_color_t ColorFromPercentage(int batteryPercent);
1920

2021
private:
2122
lv_obj_t* batteryImg;

src/displayapp/screens/WatchFaceTerminal.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,10 @@ void WatchFaceTerminal::Refresh() {
123123
powerPresent = batteryController.IsPowerPresent();
124124
batteryPercentRemaining = batteryController.PercentRemaining();
125125
if (batteryPercentRemaining.IsUpdated() || powerPresent.IsUpdated()) {
126-
// HSV color model has red at 0° and green at 120°.
127-
// We lock satuation and brightness at 100% and traverse the cilinder
128-
// between red and green, thus avoiding the darker RGB on medium battery
129-
// charges and giving us a much nicer color range.
130-
uint8_t hue = batteryPercentRemaining.Get() * 120 / 100;
131-
lv_obj_set_style_local_text_color(batteryValue, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, lv_color_hsv_to_rgb(hue, 100, 100));
126+
lv_obj_set_style_local_text_color(batteryValue,
127+
LV_LABEL_PART_MAIN,
128+
LV_STATE_DEFAULT,
129+
BatteryIcon::ColorFromPercentage(batteryPercentRemaining.Get()));
132130
lv_label_set_text_fmt(batteryValue, "#ffffff [BATT]# %d%%", batteryPercentRemaining.Get());
133131
if (batteryController.IsCharging()) {
134132
lv_label_ins_text(batteryValue, LV_LABEL_POS_LAST, " Charging");

0 commit comments

Comments
 (0)