Root/ubb-vga/ccube.c

1/*
2 * ccube.c - Color mapper based on color cube model
3 *
4 * Written 2011 by Werner Almesberger
5 * Copyright 2011 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdint.h>
15
16#include "ubb-vga.h"
17
18
19#define W_RGB (256*0.5)
20#define W_Y (256*0.25)
21#define W_0 (256*0.125)
22
23
24static struct col {
25    uint8_t r, g, b;
26} col[16];
27
28
29uint8_t ccube_map(uint8_t r, uint8_t g, uint8_t b)
30{
31    unsigned best = 0;
32    int best_i = 0;
33    unsigned d;
34    int i;
35
36    for (i = 0; i != 16; i++) {
37        d = (r-col[i].r)*(r-col[i].r)+
38            (g-col[i].g)*(g-col[i].g)+
39            (b-col[i].b)*(b-col[i].b);
40        if (!i || d < best) {
41            best = d;
42            best_i = i;
43        }
44    }
45    return best_i;
46}
47
48
49void ccube_init(void)
50{
51    int i;
52
53    for (i = 0; i != 16; i++) {
54        col[i].r = W_0+(i & R_VAL ? W_RGB : 0)+(i & Y_VAL ? W_Y : 0);
55        col[i].g = W_0+(i & G_VAL ? W_RGB : 0)+(i & Y_VAL ? W_Y : 0);
56        col[i].b = W_0+(i & B_VAL ? W_RGB : 0)+(i & Y_VAL ? W_Y : 0);
57    }
58}
59

Archive Download this file

Branches:
master



interactive