You are not logged in.

#1 2018-11-11 14:29:36

From: Germany
Registered: 2018-07-23
Posts: 1

lerpc - linear interpolation of HTML colors [i3wm][i3blocks]

Hi everyone,

I created a small program to interpolate HTML colors for my i3blocks battery indicator config. It basically takes two HTML colors and two numeric values (current and max) and outputs a new HTML color which is a blend of the two colors based on the current value. This can be used to make battery, cpu, load, whatever indicators in your i3blocks or other pango markup compatible application fade from one color to another. I know that this is a rather small and simple program and it takes less than a day to write, but I thought it'd be worth sharing and maybe someone even has some suggestions for additional features etc.

Lerpc can be found on the AUR and Github


#2 2018-11-11 15:19:47

Inspector Parrot
Registered: 2011-11-29
Posts: 20,424

Re: lerpc - linear interpolation of HTML colors [i3wm][i3blocks]

Note that a vast majority of your code can be replaced by a proper use of sscanf:

#include <stdlib.h>
#include <stdio.h>

#define col(a, b)   (int) (a < b ? a + (b - a) * mult : a - (a - b) * mult)

int main(int argc, const char *argv[]) {
   int r1, r2, g1, g2, b1, b2;
   if (  argc < 5 ||
         3 != sscanf(argv[1], "#%02x%02x%02x", &r1, &g1, &b1) ||
         3 != sscanf(argv[2], "#%02x%02x%02x", &r2, &g2, &b2))
      return 1; // print error and usage here
   float mult = atof(argv[3]) / atof(argv[4]);
   printf("#%02x%02x%02x\n", col(r1, r2), col(g1, g2), col(b1, b2));
   return 0;

Last edited by Trilby (2018-11-11 15:22:21)

"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman


Board footer

Powered by FluxBB