commit b69ceaece5a351853f9d5728b34dd3213668770e
parent 934256ec9323c4aa9fbe8ede50bbfc6769bbf328
Author: William Casarin <jb55@jb55.com>
Date: Tue, 7 Aug 2018 21:54:05 -0700
dark/light themes
Diffstat:
M | ln.h | | | 17 | +++++++++++------ |
M | main.c | | | 32 | +++++++++++++++++++++++++------- |
M | render.c | | | 36 | ++++++++++++++++++++++++++++++------ |
3 files changed, 66 insertions(+), 19 deletions(-)
diff --git a/ln.h b/ln.h
@@ -7,16 +7,18 @@
#define CELL_MAX_ELEMS 32
+union color {
+ float rgba[4];
+ struct {
+ float r, g, b, a;
+ };
+};
+
struct node {
const char *alias;
const char *id; // pubkey
- union {
- float rgba[4];
- struct {
- float r, g, b, a;
- };
- } color;
+ union color color;
struct cell *cell;
double x, y;
@@ -83,6 +85,9 @@ struct ln {
int window_width;
int window_height;
+ union color clear_color;
+ int dark_theme;
+
int grid_div;
struct cell *grid;
diff --git a/main.c b/main.c
@@ -75,9 +75,26 @@ int main()
srand(time(0));
// ln collision grid subdivision
// cells = grid_div * grid_div
- int grid_div = 20;
+ static const int grid_div = 20;
+ static const int dark_theme = 1;
- struct ln ln;
+ static const union color dark_color = {
+ .rgba = { 0x28 / 255.0, 0x2c / 255.0, 0x34 / 255.0, 1.0f }
+ };
+
+ static const union color light_color = {
+ .rgba = { 1.0, 1.0, 1.0, 1.0f }
+ };
+
+ // clear color
+ struct ln ln = {
+ .dark_theme = dark_theme
+ };
+
+ if (dark_theme)
+ memcpy(&ln.clear_color, &dark_color, sizeof(ln.clear_color));
+ else
+ memcpy(&ln.clear_color, &light_color, sizeof(ln.clear_color));
if (!glfwInit()) {
printf("Failed to init GLFW.");
@@ -132,6 +149,7 @@ int main()
glfwSetTime(0);
prevt = glfwGetTime();
+
glfwSetMouseButtonCallback(window, mouse_click);
glfwSetCursorPosCallback(window, mouse_pos);
@@ -174,11 +192,11 @@ int main()
// Update and render
glViewport(0, 0, fbWidth, fbHeight);
- if (premult)
- glClearColor(0, 0, 0, 0);
- else // base16-onedark bg color ;)
- glClearColor(0x28 / 255.0, 0x2c / 255.0, 0x34 / 255.0, 1.0f);
- /* glClearColor(1.0f, 1.0f, 1.0f, 1.0f); */
+
+ glClearColor(ln.clear_color.r,
+ ln.clear_color.g,
+ ln.clear_color.b,
+ ln.clear_color.a);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT |
GL_STENCIL_BUFFER_BIT);
diff --git a/render.c b/render.c
@@ -1,6 +1,7 @@
#include "render.h"
#include <stdio.h>
+#include <assert.h>
#include "nanovg/nanovg.h"
void draw_channel(NVGcontext *vg, struct channel *channel)
@@ -66,7 +67,7 @@ void draw_grid(NVGcontext *vg, int ww, int wh, int grid_div) {
}
-void draw_node(NVGcontext *vg, struct node *node)
+void draw_node(NVGcontext *vg, struct ln *ln, struct node *node)
{
const float r = node->size;
/* const float pos = 500.0; */
@@ -83,21 +84,44 @@ void draw_node(NVGcontext *vg, struct node *node)
nvgRGBAf(node->color.r, node->color.g, node->color.b,
node->color.a);
+ static const float adj = 0.3f;
+
+ NVGcolor clear =
+ nvgRGBAf(ln->clear_color.r,
+ ln->clear_color.g,
+ ln->clear_color.b,
+ 1.0f);
+
+ NVGcolor clear_adj =
+ nvgRGBAf(ln->clear_color.r * adj,
+ ln->clear_color.g * adj,
+ ln->clear_color.b * adj,
+ 1.0f);
+
NVGcolor blend =
- nvgRGBAf(0, 0, 0, 1.0);
+ nvgRGBAf(node->color.r * adj,
+ node->color.g * adj,
+ node->color.b * adj,
+ 1.0f);
nvgSave(vg);
nvgTranslate(vg, node->x, node->y);
const float light = 2.0f;
- bg = nvgRadialGradient(vg, -light, -light, 0, r+2.0, node_color, blend);
+
+ // TODO: use brightness instead of clear color for white theme
+ if (!ln->dark_theme)
+ bg = nvgRadialGradient(vg, -light, -light, 0, r+2.0, clear, node_color);
+ else
+ bg = nvgRadialGradient(vg, -light, -light, 0, r+2.0, node_color, blend);
+
nvgBeginPath(vg);
nvgCircle(vg, 0, 0, r);
/* nvgPathWinding(vg, NVG_HOLE); */
nvgStrokeWidth(vg, 3.0f);
- nvgStrokeColor(vg, nvgRGBf(0.1, 0.1, 0.1));
- /* nvgStroke(vg); */
+ nvgStrokeColor(vg, ln->dark_theme ? clear_adj : clear);
+ nvgStroke(vg);
nvgFillPaint(vg, bg);
nvgFill(vg);
@@ -125,5 +149,5 @@ void render_ln(struct ln *ln)
draw_channel(vg, &ln->channels[i]);
for (i = 0; i < ln->node_count; i++)
- draw_node(vg, &ln->nodes[i]);
+ draw_node(vg, ln, &ln->nodes[i]);
}