feat: use the foreground color for cleaner look

This commit is contained in:
Ricardo Markiewicz
2026-04-08 23:36:47 +02:00
parent 4d6354af48
commit dfc26364e1
3 changed files with 16 additions and 18 deletions
-1
View File
@@ -24,7 +24,6 @@ class AGraph : public AModule {
Gtk::DrawingArea graph_; Gtk::DrawingArea graph_;
std::deque<int> values_; std::deque<int> values_;
uint16_t datapoints_ = 20; uint16_t datapoints_ = 20;
uint16_t y_offset_ = 0;
GraphType graph_type_ = GraphType::LINE; GraphType graph_type_ = GraphType::LINE;
void addValue(const int n); void addValue(const int n);
+1 -6
View File
@@ -19,10 +19,6 @@ The *cpu graph* module displays a line graph with the CPU utilization.
typeof: integer ++ typeof: integer ++
The length in pixels the module should display. The length in pixels the module should display.
*y_offset*: ++
typeof: integer ++
The margin in pixels at the bottom of the module.
*datapoints*: ++ *datapoints*: ++
typeof: integer ++ typeof: integer ++
How many data points to show. How many data points to show.
@@ -72,8 +68,7 @@ Basic configuration:
``` ```
"cpu_graph": { "cpu_graph": {
"interval": 2, "interval": 2,
"width": 10, "width": 10
"y_offset": 4
} }
``` ```
+15 -11
View File
@@ -39,10 +39,6 @@ AGraph::AGraph(const Json::Value& config, const std::string& name, const std::st
datapoints_ = config_["datapoints_"].asUInt(); datapoints_ = config_["datapoints_"].asUInt();
} }
if (config_["y_offset"].isUInt()) {
y_offset_ = config_["y_offset"].asUInt();
}
if (config_["graph_type"].isString()) { if (config_["graph_type"].isString()) {
std::string type = config_["graph_type"].asString(); std::string type = config_["graph_type"].asString();
if (type == "line") { if (type == "line") {
@@ -122,7 +118,7 @@ void AGraph::addValue(const int n) {
bool AGraph::onDraw(const Cairo::RefPtr<Cairo::Context>& cr) { bool AGraph::onDraw(const Cairo::RefPtr<Cairo::Context>& cr) {
const int width = graph_.get_allocated_width(); const int width = graph_.get_allocated_width();
const int height = graph_.get_allocated_height() - 1 - y_offset_; const int height = graph_.get_allocated_height() - 1;
if (values_.empty() || width <= 0 || height <= 0) { if (values_.empty() || width <= 0 || height <= 0) {
return false; return false;
@@ -220,20 +216,23 @@ void AGraph::drawBars(const Cairo::RefPtr<Cairo::Context>& cr,
current_value = std::min(100, std::max(0, current_value)); current_value = std::min(100, std::max(0, current_value));
double green_height = height * (std::min(current_value, 40) / 100.0); double green_height = height * (std::min(current_value, 40) / 100.0);
cr->set_source_rgba(0.0, 1.0, 0.0, 1.0); cr->set_source_rgba(fg_color.get_red(), fg_color.get_green(), fg_color.get_blue(),
0.5);
cr->rectangle(0, height - green_height, width, green_height); cr->rectangle(0, height - green_height, width, green_height);
cr->fill(); cr->fill();
if (current_value > 40) { if (current_value > 40) {
double yellow_height = height * (std::min(current_value, 75) - 40) / 100.0; double yellow_height = height * (std::min(current_value, 75) - 40) / 100.0;
cr->set_source_rgba(1.0, 1.0, 0.0, 1.0); cr->set_source_rgba(fg_color.get_red(), fg_color.get_green(), fg_color.get_blue(),
0.7);
cr->rectangle(0, height - green_height - yellow_height, width, yellow_height); cr->rectangle(0, height - green_height - yellow_height, width, yellow_height);
cr->fill(); cr->fill();
} }
if (current_value > 75) { if (current_value > 75) {
double orange_height = height * (std::min(current_value, 85) - 75) / 100.0; double orange_height = height * (std::min(current_value, 85) - 75) / 100.0;
cr->set_source_rgba(1.0, 0.5, 0.0, 1.0); cr->set_source_rgba(fg_color.get_red(), fg_color.get_green(), fg_color.get_blue(),
0.85);
double yellow_height = height * (std::min(current_value, 75) - 40) / 100.0; double yellow_height = height * (std::min(current_value, 75) - 40) / 100.0;
cr->rectangle(0, height - green_height - yellow_height - orange_height, width, cr->rectangle(0, height - green_height - yellow_height - orange_height, width,
orange_height); orange_height);
@@ -242,7 +241,8 @@ void AGraph::drawBars(const Cairo::RefPtr<Cairo::Context>& cr,
if (current_value > 85) { if (current_value > 85) {
double red_height = height * (current_value - 85) / 100.0; double red_height = height * (current_value - 85) / 100.0;
cr->set_source_rgba(1.0, 0.0, 0.0, 1.0); cr->set_source_rgba(fg_color.get_red(), fg_color.get_green(), fg_color.get_blue(),
1.0);
double yellow_height = height * (std::min(current_value, 75) - 40) / 100.0; double yellow_height = height * (std::min(current_value, 75) - 40) / 100.0;
double orange_height = height * (std::min(current_value, 85) - 75) / 100.0; double orange_height = height * (std::min(current_value, 85) - 75) / 100.0;
cr->rectangle(0, height - green_height - yellow_height - orange_height - red_height, width, cr->rectangle(0, height - green_height - yellow_height - orange_height - red_height, width,
@@ -268,14 +268,16 @@ void AGraph::drawGauge(const Cairo::RefPtr<Cairo::Context>& cr, double width, do
double angle2 = angle1 + 0.3 * angle1; double angle2 = angle1 + 0.3 * angle1;
// Green section (0-33%) // Green section (0-33%)
cr->set_source_rgba(0.0, 1.0, 0.0, 1.0); cr->set_source_rgba(fg_color.get_red(), fg_color.get_green(), fg_color.get_blue(),
0.5);
cr->arc(center_x, center_y, radius, angle1, angle2); cr->arc(center_x, center_y, radius, angle1, angle2);
cr->stroke(); cr->stroke();
// Yellow section (33-66%) // Yellow section (33-66%)
angle1 = angle2; angle1 = angle2;
angle2 = angle1 + 0.3 * angle1; angle2 = angle1 + 0.3 * angle1;
cr->set_source_rgba(1.0, 1.0, 0.0, 1.0); cr->set_source_rgba(fg_color.get_red(), fg_color.get_green(), fg_color.get_blue(),
0.75);
cr->arc(center_x, center_y, radius, angle1, angle2); cr->arc(center_x, center_y, radius, angle1, angle2);
cr->stroke(); cr->stroke();
@@ -283,6 +285,8 @@ void AGraph::drawGauge(const Cairo::RefPtr<Cairo::Context>& cr, double width, do
angle1 = angle2; angle1 = angle2;
angle2 = 0.0; angle2 = 0.0;
cr->set_source_rgba(1.0, 0.0, 0.0, 0.8); cr->set_source_rgba(1.0, 0.0, 0.0, 0.8);
cr->set_source_rgba(fg_color.get_red(), fg_color.get_green(), fg_color.get_blue(),
1.0);
cr->arc(center_x, center_y, radius, angle1, angle2); cr->arc(center_x, center_y, radius, angle1, angle2);
cr->stroke(); cr->stroke();