simplify color logic

This commit is contained in:
smayzy 2026-04-06 13:49:47 +02:00
parent f4f540e532
commit 8f20eb454c

View File

@ -1,5 +1,6 @@
#include <ctype.h> #include <ctype.h>
#include <ncurses.h> #include <ncurses.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -24,6 +25,7 @@ typedef struct{
int ldata; int ldata;
int hdata; int hdata;
int digits; int digits;
bool high;
WINDOW *lwin; WINDOW *lwin;
} StInt; } StInt;
typedef struct{ typedef struct{
@ -31,6 +33,7 @@ typedef struct{
float ldata; float ldata;
float hdata; float hdata;
int digits; int digits;
bool high;
WINDOW *lwin; WINDOW *lwin;
} StFlt; } StFlt;
typedef struct{ typedef struct{
@ -80,7 +83,39 @@ int smaller_of(int a, int b) {
return (a < b) ? a : b; return (a < b) ? a : b;
} }
void win_int(StInt st, int use_color, int color) { int color_high(float data, float ldata, float hdata) {
int percent = (int)((100.0f * (data - ldata)) / (hdata - ldata) + 0.5f);
if (percent >= 90) return 1;
else if (percent >= 75) return 2;
else return 3;
}
int color_low(float data, float ldata, float hdata) {
int percent = 100 - (int)((100.0f * (data - ldata)) / (hdata - ldata) + 0.5f);
if (percent >= 90) return 1;
else if (percent >= 75) return 2;
else return 3;
}
void bar_mark(WINDOW *lwin) {
int lwiny, lwinx;
int first_mark, last_mark;
getmaxyx(lwin, lwiny, lwinx);
first_mark = (75 * (lwinx - 4)) / 100;
last_mark = (90 * (lwinx - 4)) / 100;
mvwaddch(lwin, 0, first_mark + 1, '|');
mvwaddch(lwin, lwiny - 1, first_mark + 1, '|');
mvwaddch(lwin, 0, last_mark + 1, '|');
mvwaddch(lwin, lwiny - 1, last_mark + 1, '|');
}
void win_int(StInt st, bool use_color) {
int color;
if (st.high) {
color = color_high(st.data, st.ldata, st.hdata);
} else {
color = color_low(st.data, st.ldata, st.hdata);
};
int lwiny, lwinx, len; int lwiny, lwinx, len;
char buf[16]; char buf[16];
getmaxyx(st.lwin, lwiny, lwinx); getmaxyx(st.lwin, lwiny, lwinx);
@ -124,7 +159,13 @@ void win_int(StInt st, int use_color, int color) {
wrefresh(st.lwin); wrefresh(st.lwin);
} }
void win_float(StFlt st, int use_color, int color) { void win_float(StFlt st, bool use_color) {
int color;
if (st.high) {
color = color_high(st.data, st.ldata, st.hdata);
} else {
color = color_low(st.data, st.ldata, st.hdata);
};
int lwiny, lwinx, len; int lwiny, lwinx, len;
char buf[16]; char buf[16];
@ -183,7 +224,7 @@ void win_float(StFlt st, int use_color, int color) {
wrefresh(st.lwin); wrefresh(st.lwin);
} }
void win_bar(StInt st, int use_color) { void win_bar(StInt st, bool use_color) {
int lwiny, lwinx; int lwiny, lwinx;
getmaxyx(st.lwin, lwiny, lwinx); getmaxyx(st.lwin, lwiny, lwinx);
@ -259,32 +300,6 @@ void win_bar(StInt st, int use_color) {
} }
} }
void bar_mark(WINDOW *lwin) {
int lwiny, lwinx;
int first_mark, last_mark;
getmaxyx(lwin, lwiny, lwinx);
first_mark = (75 * (lwinx - 4)) / 100;
last_mark = (90 * (lwinx - 4)) / 100;
mvwaddch(lwin, 0, first_mark + 1, '|');
mvwaddch(lwin, lwiny - 1, first_mark + 1, '|');
mvwaddch(lwin, 0, last_mark + 1, '|');
mvwaddch(lwin, lwiny - 1, last_mark + 1, '|');
}
int color_high(float data, float ldata, float hdata) {
int percent = (int)((100.0f * (data - ldata)) / (hdata - ldata) + 0.5f);
if (percent >= 90) return 1;
else if (percent >= 75) return 2;
else return 3;
}
short color_low(float data, float ldata, float hdata) {
int percent = 100 - (int)((100.0f * (data - ldata)) / (hdata - ldata) + 0.5f);
if (percent >= 90) return 1;
else if (percent >= 75) return 2;
else return 3;
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
int option; int option;
int delay = 1; int delay = 1;
@ -322,7 +337,7 @@ int main(int argc, char **argv) {
return 1; return 1;
} }
int use_color = 0; bool use_color = 0;
if (has_colors()) { if (has_colors()) {
use_color = 1; use_color = 1;
start_color(); start_color();
@ -335,15 +350,15 @@ int main(int argc, char **argv) {
WINDOW *win[10]; WINDOW *win[10];
Tel tel; Tel tel;
tel.speed = (StInt){0, 0, 200, 3, NULL}; tel.speed = (StInt){0, 0, 200, 3, 1, NULL};
tel.rpm = (StInt){0, 0, 6000, 4, NULL}; tel.rpm = (StInt){0, 0, 6000, 4, 1, NULL};
tel.tq = (StFlt){0.0f, 1.8f, 3.5f, 3, NULL}; tel.tq = (StFlt){0.0f, 1.8f, 3.5f, 3, 1, NULL};
tel.power = (StInt){0, 0, 1300, 4, NULL}; tel.power = (StInt){0, 0, 1300, 4, 1, NULL};
tel.eff = (StFlt){0.0f, 0.0f, 300.0f, 5, NULL}; tel.eff = (StFlt){0.0f, 0.0f, 300.0f, 5, 1, NULL};
tel.bat = (StInt){0, 0, 100, 3, NULL}; tel.bat = (StInt){0, 0, 100, 3, 0, NULL};
tel.bat_temp = (StFlt){0.0f, 0.0f, 150.0f, 5, NULL}; tel.bat_temp = (StFlt){0.0f, 0.0f, 150.0f, 5, 1, NULL};
tel.var_temp = (StFlt){0.0f, 0.0f, 150.0f, 5, NULL}; tel.var_temp = (StFlt){0.0f, 0.0f, 150.0f, 5, 1, NULL};
tel.mot_temp = (StFlt){0.0f, 0.0f, 150.0f, 5, NULL}; tel.mot_temp = (StFlt){0.0f, 0.0f, 150.0f, 5, 1, NULL};
tel.message = (StStr){NULL}; tel.message = (StStr){NULL};
int x3 = x / 3; int x3 = x / 3;
@ -413,18 +428,18 @@ int main(int argc, char **argv) {
} }
long now = now_ms(); long now = now_ms();
if (now - t100 >= 100 * delay) { if (now - t100 >= 100 * delay) {
win_int(tel.speed, use_color, color_high(tel.speed.data, tel.speed.ldata, tel.speed.hdata)); win_int(tel.speed, use_color);
win_int(tel.rpm, use_color, color_high(tel.rpm.data, tel.rpm.ldata, tel.rpm.hdata)); win_int(tel.rpm, use_color);
win_float(tel.tq, use_color, color_high(tel.tq.data, tel.tq.ldata, tel.tq.hdata)); win_float(tel.tq, use_color);
win_bar(tel.power, use_color); win_bar(tel.power, use_color);
win_float(tel.eff, use_color, color_high(tel.eff.data, tel.eff.ldata, tel.eff.hdata)); win_float(tel.eff, use_color);
t100 = now; t100 = now;
} }
if (now - t1000 >= 1000 * delay) { if (now - t1000 >= 1000 * delay) {
win_int(tel.bat, use_color, color_low(tel.bat.data, tel.bat.ldata, tel.bat.hdata)); win_int(tel.bat, use_color);
win_float(tel.bat_temp, use_color, color_high(tel.bat_temp.data, tel.bat_temp.ldata, tel.bat_temp.hdata)); win_float(tel.bat_temp, use_color);
win_float(tel.var_temp, use_color, color_high(tel.var_temp.data, tel.var_temp.ldata, tel.var_temp.hdata)); win_float(tel.var_temp, use_color);
win_float(tel.mot_temp, use_color, color_high(tel.mot_temp.data, tel.mot_temp.ldata, tel.mot_temp.hdata)); win_float(tel.mot_temp, use_color);
t1000 = now; t1000 = now;
} }
//win[9]; //win[9];