colors: store as doubles, rather than uint32_t

Since cairo uses doubles, we don't want to have to convert a uin32_t
to double values every time we render a cell.
This commit is contained in:
Daniel Eklöf 2019-06-26 20:33:32 +02:00
parent 54403738bb
commit 1dbddd7155
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
3 changed files with 73 additions and 55 deletions

83
csi.c
View file

@ -16,29 +16,29 @@
#define min(x, y) ((x) < (y) ? (x) : (y))
static const uint32_t colors_regular[] = {
0x000000ff,
0xcc9393ff,
0x7f9f7fff,
0xd0bf8fff,
0x6ca0a3ff,
0xdc8cc3ff,
0x93e0e3ff,
0xdcdcccff,
static const struct rgba colors_regular[] = {
{0.000000, 0.000000, 0.000000, 1.000000}, /* 0x000000ff */
{0.800000, 0.576471, 0.576471, 1.000000}, /* 0xcc9393ff */
{0.498039, 0.623529, 0.498039, 1.000000}, /* 0x7f9f7fff */
{0.815686, 0.749020, 0.560784, 1.000000}, /* 0xd0bf8fff */
{0.423529, 0.627451, 0.639216, 1.000000}, /* 0x6ca0a3ff */
{0.862745, 0.549020, 0.764706, 1.000000}, /* 0xdc8cc3ff */
{0.576471, 0.878431, 0.890196, 1.000000}, /* 0x93e0e3ff */
{0.862745, 0.862745, 0.800000, 1.000000}, /* 0xdcdcccff */
};
static const uint32_t colors_bright[] = {
0x000000ff,
0xdca3a3ff,
0xbfebbfff,
0xf0dfafff,
0x8cd0d3ff,
0xdc8cc3ff,
0x93e0e3ff,
0xffffffff,
static const struct rgba colors_bright[] = {
{0.000000, 0.000000, 0.000000, 1.000000}, /* 0x000000ff */
{0.862745, 0.639216, 0.639216, 1.000000}, /* 0xdca3a3ff */
{0.749020, 0.921569, 0.749020, 1.000000}, /* 0xbfebbfff */
{0.941176, 0.874510, 0.686275, 1.000000}, /* 0xf0dfafff */
{0.549020, 0.815686, 0.827451, 1.000000}, /* 0x8cd0d3ff */
{0.862745, 0.549020, 0.764706, 1.000000}, /* 0xdc8cc3ff */
{0.576471, 0.878431, 0.890196, 1.000000}, /* 0x93e0e3ff */
{1.000000, 1.000000, 1.000000, 1.000000}, /* 0xffffffff */
};
static uint32_t colors256[256];
static struct rgba colors256[256];
static void __attribute__((constructor))
initialize_colors256(void)
@ -51,14 +51,29 @@ initialize_colors256(void)
for (size_t r = 0; r < 6; r++) {
for (size_t g = 0; g < 6; g++) {
for (size_t b = 0; b < 6; b++) {
colors256[16 + r * 6 * 6 + g * 6 + b] = (struct rgba) {
r * 51 / 255.0,
g * 51 / 255.0,
b * 51 / 255.0,
1.0,
};
#if 0
colors256[16 + r * 6 * 6 + g * 6 + b] =
(51 * r) << 24 | (51 * g) << 16 | (51 * b) << 8 | 0xff;
#endif
}
}
}
for (size_t i = 0; i < 24; i++)
colors256[232 + i] = (11 * i) << 24 | (11 * i) << 16 | (11 * i) << 8 | 0xff;
for (size_t i = 0; i < 24; i++){
colors256[232 + i] = (struct rgba) {
i * 11 / 255.0,
i * 11 / 255.0,
i * 11 / 255.0,
1.0
};
//(11 * i) << 24 | (11 * i) << 16 | (11 * i) << 8 | 0xff;
}
}
static int
@ -139,10 +154,15 @@ csi_sgr(struct terminal *term)
} else if (term->vt.params.idx - i - 1 == 4 &&
term->vt.params.v[i + 1].value == 2)
{
uint32_t r = term->vt.params.v[i + 2].value;
uint32_t g = term->vt.params.v[i + 3].value;
uint32_t b = term->vt.params.v[i + 4].value;
term->vt.attrs.foreground = r << 24 | g << 16 | b << 8 | 0xff;
uint8_t r = term->vt.params.v[i + 2].value;
uint8_t g = term->vt.params.v[i + 3].value;
uint8_t b = term->vt.params.v[i + 4].value;
term->vt.attrs.foreground = (struct rgba) {
r / 255.0,
g / 255.0,
b / 255.0,
1.0,
};
term->vt.attrs.have_foreground = true;
i += 4;
} else {
@ -180,10 +200,15 @@ csi_sgr(struct terminal *term)
} else if (term->vt.params.idx - i - 1 == 4 &&
term->vt.params.v[i + 1].value == 2)
{
uint32_t r = term->vt.params.v[i + 2].value;
uint32_t g = term->vt.params.v[i + 3].value;
uint32_t b = term->vt.params.v[i + 4].value;
term->vt.attrs.background = r << 24 | g << 16 | b << 8 | 0xff;
uint8_t r = term->vt.params.v[i + 2].value;
uint8_t g = term->vt.params.v[i + 3].value;
uint8_t b = term->vt.params.v[i + 4].value;
term->vt.attrs.background = (struct rgba) {
r / 255.0,
g / 255.0,
b / 255.0,
1.0
};
term->vt.attrs.have_background = true;
i += 4;
} else {

35
main.c
View file

@ -29,8 +29,8 @@
#define min(x, y) ((x) < (y) ? (x) : (y))
#define max(x, y) ((x) > (y) ? (x) : (y))
static const uint32_t default_foreground = 0xffffffff;
static const uint32_t default_background = 0x000000ff;
static const struct rgba default_foreground = {1.0, 1.0, 1.0, 1.0};
static const struct rgba default_background = {0.0, 0.0, 0.0, 1.0};
struct wayland {
struct wl_display *display;
@ -103,19 +103,19 @@ grid_render_update(struct context *c, struct buffer *buf, const struct damage *d
int width = c->term.grid.cell_width;
int height = c->term.grid.cell_height;
uint32_t foreground = cell->attrs.have_foreground
struct rgba foreground = cell->attrs.have_foreground
? cell->attrs.foreground : c->term.grid.foreground;
uint32_t background = cell->attrs.have_background
struct rgba background = cell->attrs.have_background
? cell->attrs.background : c->term.grid.background;
if (has_cursor) {
uint32_t swap = foreground;
struct rgba swap = foreground;
foreground = background;
background = swap;
}
if (cell->attrs.reverse) {
uint32_t swap = foreground;
struct rgba swap = foreground;
foreground = background;
background = swap;
}
@ -123,19 +123,10 @@ grid_render_update(struct context *c, struct buffer *buf, const struct damage *d
//LOG_DBG("cell %dx%d dirty: c=0x%02x (%c)",
// row, col, cell->c[0], cell->c[0]);
double br = (double)((background >> 24) & 0xff) / 255.0;
double bg = (double)((background >> 16) & 0xff) / 255.0;
double bb = (double)((background >> 8) & 0xff) / 255.0;
double ba = (double)((background >> 0) & 0xff) / 255.0;
double fr = (double)((foreground >> 24) & 0xff) / 255.0;
double fg = (double)((foreground >> 16) & 0xff) / 255.0;
double fb = (double)((foreground >> 8) & 0xff) / 255.0;
double fa = (double)((foreground >> 0) & 0xff) / 255.0;
cairo_scaled_font_t *font = attrs_to_font(c, &cell->attrs);
cairo_set_scaled_font(buf->cairo, font);
cairo_set_source_rgba(buf->cairo, br, bg, bb, ba);
cairo_set_source_rgba(
buf->cairo, background.r, background.g, background.b, background.a);
/* Background */
cairo_rectangle(buf->cairo, x, y, width, height);
@ -158,7 +149,8 @@ grid_render_update(struct context *c, struct buffer *buf, const struct damage *d
continue;
}
cairo_set_source_rgba(buf->cairo, fr, fg, fb, fa);
cairo_set_source_rgba(
buf->cairo, foreground.r, foreground.g, foreground.b, foreground.a);
cairo_show_glyphs(buf->cairo, glyphs, num_glyphs);
cairo_glyph_free(glyphs);
}
@ -175,10 +167,9 @@ grid_render_erase(struct context *c, struct buffer *buf, const struct damage *dm
LOG_DBG("damage: ERASE: %d -> %d",
dmg->range.start, dmg->range.start + dmg->range.length);
double br = (double)((default_background >> 24) & 0xff) / 255.0;
double bg = (double)((default_background >> 16) & 0xff) / 255.0;
double bb = (double)((default_background >> 8) & 0xff) / 255.0;
cairo_set_source_rgba(buf->cairo, br, bg, bb, 1.0);
cairo_set_source_rgba(
buf->cairo, default_background.r, default_background.g,
default_background.b, default_background.a);
const int cols = c->term.grid.cols;

View file

@ -11,6 +11,8 @@
#include "tllist.h"
struct rgba { double r, g, b, a; };
struct attributes {
bool bold;
bool italic;
@ -21,8 +23,8 @@ struct attributes {
bool reverse;
bool have_foreground;
bool have_background;
uint32_t foreground; /* Only valid when have_foreground == true */
uint32_t background; /* Only valid when have_background == true */
struct rgba foreground; /* Only valid when have_foreground == true */
struct rgba background; /* Only valid when have_background == true */
};
struct cell {
@ -76,8 +78,8 @@ struct grid {
int col;
} alt_saved_cursor;
uint32_t foreground;
uint32_t background;
struct rgba foreground;
struct rgba background;
tll(struct damage) damage;
tll(struct damage) scroll_damage;