Skip to content

Instantly share code, notes, and snippets.

@saivert
Created March 10, 2021 20:00
Show Gist options
  • Select an option

  • Save saivert/73b24602832244349ee09df007d13bb2 to your computer and use it in GitHub Desktop.

Select an option

Save saivert/73b24602832244349ee09df007d13bb2 to your computer and use it in GitHub Desktop.
PangoAttrList *
convert_escapetext_to_pango_attrlist (char *text, float *fg, float *bg, float *highlight) {
const int maxTintStops = 100;
tint_stop_t tintStops[maxTintStops];
int numTintStops;
char *plainString;
numTintStops = calculate_tint_stops_from_string (text, tintStops, maxTintStops, &plainString);
guint strLength = strlen(plainString);
strcpy (text, plainString);
PangoAttrList *lst = pango_attr_list_new ();
PangoAttribute *attr = NULL;
// add attributes
for (guint i = 0; i < numTintStops; i++) {
int index0 = tintStops[i].index;
guint len = strLength - index0;
GdkColor finalColor = { .red = fg[0]*65535, .green = fg[1]*65535, .blue = fg[2]*65535};
if (tintStops[i].has_rgb) {
finalColor.red = tintStops[i].r * 65535/255.;
finalColor.green = tintStops[i].g * 65535/255.;
finalColor.blue = tintStops[i].b * 65535/255.;
}
int tint = tintStops[i].tint;
if (tint >= 1 && tint <= 3) {
const float blend[] = {.50f, .25f, 0};
finalColor.red = CHANNEL_BLENDR(highlight[0], finalColor.red/65535., blend[tint-1]) * 65535;
finalColor.green = CHANNEL_BLENDR(highlight[1], finalColor.green/65535., blend[tint-1]) * 65535;
finalColor.blue = CHANNEL_BLENDR(highlight[2], finalColor.blue/65535., blend[tint-1]) * 65535;
} else if (tint >= -3 && tint <= -1) {
const float blend[] = {.30f, .60f, .80f};
finalColor.red = CHANNEL_BLENDR(finalColor.red/65535., bg[0], blend[-tint-1]) * 65535;
finalColor.green = CHANNEL_BLENDR(finalColor.green/65535., bg[1], blend[-tint-1]) * 65535;
finalColor.blue = CHANNEL_BLENDR(finalColor.blue/65535., bg[2], blend[-tint-1]) * 65535;
}
attr = pango_attr_foreground_new (finalColor.red, finalColor.green, finalColor.blue);
attr->start_index = index0;
attr->end_index = index0 + len;
pango_attr_list_insert (lst, attr);
}
free (plainString);
return lst;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment