zsh-workers
 help / color / mirror / code / Atom feed
7ebc753650c5d096022982aaa3a25e71d486544c blob 4307 bytes (raw)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
 
#include "nearcolor.mdh"
#include "nearcolor.pro"

#include <math.h>

struct cielab {
    float L, a, b;
};
typedef struct cielab *Cielab;

static float
deltae(Cielab lab1, Cielab lab2)
{
    /* taking square root unnecessary as we're just comparing values */
    return powf(lab1->L - lab2->L, 2) +
	powf(lab1->a - lab2->a, 2) +
	powf(lab1->b - lab2->b, 2);
}

static void
RGBtoLAB(int red, int green, int blue, Cielab lab)
{
    float R = (float)red / 255.0;
    float G = (float)green / 255.0;
    float B = (float)blue / 255.0;
    R = 100.0 * (R > 0.04045 ? powf((R + 0.055) / 1.055, 2.4) : R / 12.92);
    G = 100.0 * (G > 0.04045 ? powf((G + 0.055) / 1.055, 2.4) : G / 12.92);
    B = 100.0 * (B > 0.04045 ? powf((B + 0.055) / 1.055, 2.4) : B / 12.92);

    /* Observer. = 2 degrees, Illuminant = D65 */
    float X = (R * 0.4124 + G * 0.3576 + B * 0.1805) / 95.047;
    float Y = (R * 0.2126 + G * 0.7152 + B * 0.0722) / 100.0;
    float Z = (R * 0.0193 + G * 0.1192 + B * 0.9505) / 108.883;

    X = (X > 0.008856) ? powf(X, 1.0/3.0) : (7.787 * X) + (16.0 / 116.0);
    Y = (Y > 0.008856) ? powf(Y, 1.0/3.0) : (7.787 * Y) + (16.0 / 116.0);
    Z = (Z > 0.008856) ? powf(Z, 1.0/3.0) : (7.787 * Z) + (16.0 / 116.0);

    lab->L = (116.0 * Y) - 16.0;
    lab->a = 500.0 * (X - Y);
    lab->b = 200.0 * (Y - Z);
}

static int
mapRGBto88(int red, int green, int blue)
{
    int component[] = { 0, 0x8b, 0xcd, 0xff, 0x2e, 0x5c, 0x8b, 0xa2, 0xb9, 0xd0, 0xe7 };
    struct cielab orig, next;
    float nextl, bestl = -1;
    int r, g, b;
    int comp_r = 0, comp_g = 0, comp_b = 0;

    /* Get original value */
    RGBtoLAB(red, green, blue, &orig);

    /* try every one of the 72 colours */
    for (r = 0; r < 11; r++) {
	for (g = 0; g <= 3; g++) {
	    for (b = 0; b <= 3; b++) {
		if (r > 3) g = b = r; /* advance inner loops to the block of greys */
		RGBtoLAB(component[r], component[g], component[b], &next);
		nextl = deltae(&orig, &next);
		if (nextl < bestl || bestl < 0) {
		    bestl = nextl;
		    comp_r = r;
		    comp_g = g;
		    comp_b = b;
		}
	    }
	}
    }

    return (comp_r > 3) ? 77 + comp_r :
        16 + (comp_r * 16) + (comp_g * 4) + comp_b;
}

/*
 * Convert RGB to nearest colour in the 256 colour range
 */
static int
mapRGBto256(int red, int green, int blue)
{
    int component[] = {
	0, 0x5f, 0x87, 0xaf, 0xd7, 0xff,
	0x8, 0x12, 0x1c, 0x26, 0x30, 0x3a, 0x44, 0x4e,
	0x58, 0x62, 0x6c, 0x76, 0x80, 0x8a, 0x94, 0x9e,
	0xa8, 0xb2, 0xbc, 0xc6, 0xd0, 0xda, 0xe4, 0xee
    };
    struct cielab orig, next;
    float nextl, bestl = -1;
    int r, g, b;
    int comp_r = 0, comp_g = 0, comp_b = 0;

    /* Get original value */
    RGBtoLAB(red, green, blue, &orig);

    for (r = 0; r < sizeof(component)/sizeof(*component); r++) {
	for (g = 0; g <= 5; g++) {
	    for (b = 0; b <= 5; b++) {
		if (r > 5) g = b = r; /* advance inner loops to the block of greys */
		RGBtoLAB(component[r], component[g], component[b], &next);
		nextl = deltae(&orig, &next);
		if (nextl < bestl || bestl < 0) {
		    bestl = nextl;
		    comp_r = r;
		    comp_g = g;
		    comp_b = b;
		}
	    }
	}
    }

    return (comp_r > 5) ? 226 + comp_r :
	16 + (comp_r * 36) + (comp_g * 6) + comp_b;
}

static int
getnearestcolor(UNUSED(Hookdef dummy), Color_rgb col)
{
    /* we add 1 to the colours so that colour 0 (black) is
     * distinguished from runhookdef() indicating that no
     * hook function is registered */
    if (tccolours == 256)
	return mapRGBto256(col->red, col->green, col->blue) + 1;
    if (tccolours == 88)
	return mapRGBto88(col->red, col->green, col->blue) + 1;
    return -1;
}

static struct features module_features = {
    NULL, 0,
    NULL, 0,
    NULL, 0,
    NULL, 0,
    0
};

/**/
int
setup_(UNUSED(Module m))
{
    return 0;
}

/**/
int
features_(Module m, char ***features)
{
    *features = featuresarray(m, &module_features);
    return 0;
}

/**/
int
enables_(Module m, int **enables)
{
    return handlefeatures(m, &module_features, enables);
}

/**/
int
boot_(Module m)
{
    addhookfunc("get_color_attr", (Hookfn) getnearestcolor);
    return 0;
}

/**/
int
cleanup_(Module m)
{
    deletehookfunc("get_color_attr", (Hookfn) getnearestcolor);
    return setfeatureenables(m, &module_features, NULL);
}

/**/
int
finish_(UNUSED(Module m))
{
    return 0;
}
debug log:

solving 7ebc75365 ...
found 7ebc75365 in https://inbox.vuxu.org/zsh-workers/2917-1541648904.343056@V1WL.VCjm.HHYZ/
found 128658e20 in https://git.vuxu.org/mirror/zsh/
preparing index
index prepared:
100644 128658e209f921235189c42164f89a1b63e76ca2	Src/Modules/nearcolor.c

applying [1/1] https://inbox.vuxu.org/zsh-workers/2917-1541648904.343056@V1WL.VCjm.HHYZ/
diff --git a/Src/Modules/nearcolor.c b/Src/Modules/nearcolor.c
index 128658e20..7ebc75365 100644

Checking patch Src/Modules/nearcolor.c...
Applied patch Src/Modules/nearcolor.c cleanly.

index at:
100644 7ebc753650c5d096022982aaa3a25e71d486544c	Src/Modules/nearcolor.c

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).