static void color_reverse(const GdkColor *a, GdkColor *b) { gdouble red; gdouble green; gdouble blue; gdouble h; gdouble s; gdouble v; red = (gdouble) a->red / 65535.0; green = (gdouble) a->green / 65535.0; blue = (gdouble) a->blue / 65535.0; gtk_rgb_to_hsv (red, green, blue, &h, &s, &v); /* pivot brightness around the center */ v = 0.5 + (0.5 - v); if (v > 1.0) v = 1.0; else if (v < 0.0) v = 0.0; /* reduce saturation by 50% */ s *= 0.5; gtk_hsv_to_rgb (h, s, v, &red, &green, &blue); b->red = red * 65535.0; b->green = green * 65535.0; b->blue = blue * 65535.0; }
static void color_reverse (const GdkColor *a, GdkColor *b) { gdouble red; gdouble green; gdouble blue; gdouble h; gdouble s; gdouble v; red = (gdouble) a->red / 65535.0; green = (gdouble) a->green / 65535.0; blue = (gdouble) a->blue / 65535.0; gtk_rgb_to_hsv (red, green, blue, &h, &s, &v); v = 0.5 + (0.5 - v); if (v > 1.0) v = 1.0; else if (v < 0.0) v = 0.0; gtk_hsv_to_rgb (h, s, v, &red, &green, &blue); b->red = red * 65535.0; b->green = green * 65535.0; b->blue = blue * 65535.0; }
static void color_reverse (GdkRGBA *a) { gdouble red; gdouble green; gdouble blue; gdouble h; gdouble s; gdouble v; red = a->red; green = a->green; blue = a->blue; gtk_rgb_to_hsv (red, green, blue, &h, &s, &v); v = 0.5 + (0.5 - v); if (v > 1.0) v = 1.0; else if (v < 0.0) v = 0.0; gtk_hsv_to_rgb (h, s, v, &red, &green, &blue); a->red = red; a->green = green; a->blue = blue; }
static void make_palette (MateRRLabeler *labeler) { /* The idea is that we go around an hue color wheel. We want to start * at red, go around to green/etc. and stop at blue --- because magenta * is evil. Eeeeek, no magenta, please! * * Purple would be nice, though. Remember that we are watered down * (i.e. low saturation), so that would be like Like berries with cream. * Mmmmm, berries. */ double start_hue; double end_hue; int i; g_assert (labeler->priv->num_outputs > 0); #if GTK_CHECK_VERSION (3, 0, 0) labeler->priv->palette = g_new (GdkRGBA, labeler->priv->num_outputs); #else labeler->priv->palette = g_new (GdkColor, labeler->priv->num_outputs); #endif start_hue = 0.0; /* red */ end_hue = 2.0/3; /* blue */ for (i = 0; i < labeler->priv->num_outputs; i++) { double h, s, v; double r, g, b; h = start_hue + (end_hue - start_hue) / labeler->priv->num_outputs * i; s = 1.0 / 3; v = 1.0; gtk_hsv_to_rgb (h, s, v, &r, &g, &b); #if GTK_CHECK_VERSION (3, 0, 0) labeler->priv->palette[i].red = r; labeler->priv->palette[i].green = g; labeler->priv->palette[i].blue = b; labeler->priv->palette[i].alpha = 1.0; #else labeler->priv->palette[i].red = (int) (65535 * r + 0.5); labeler->priv->palette[i].green = (int) (65535 * g + 0.5); labeler->priv->palette[i].blue = (int) (65535 * b + 0.5); #endif } }
static void hsv_changed (GtkColorEditor *editor) { GdkRGBA color; gdouble h, s, v, a; h = gtk_adjustment_get_value (editor->priv->h_adj); s = gtk_adjustment_get_value (editor->priv->s_adj); v = gtk_adjustment_get_value (editor->priv->v_adj); a = gtk_adjustment_get_value (editor->priv->a_adj); gtk_hsv_to_rgb (h, s, v, &color.red, &color.green, &color.blue); color.alpha = a; gtk_color_swatch_set_rgba (GTK_COLOR_SWATCH (editor->priv->swatch), &color); gtk_color_scale_set_rgba (GTK_COLOR_SCALE (editor->priv->a_slider), &color); entry_set_rgba (editor, &color); g_object_notify (G_OBJECT (editor), "rgba"); }
int main(void) { gdouble h,s,v,r,g,b; uint16_t H,S,V,R,G,B; TColor RGB; TColor HSV; int Hi,Si,Vi,Ri,Gi,Bi; int T; int Errors = 0; int Error; int TotalError = 0; #ifdef TEST_HSV2RGB // Test HSV2RGB for (Vi = 0; Vi <= 100; Vi++) { v = Vi*1.0/100.0; for (Si = 0; Si <= 100; Si++) { s = Si*1.0/100.0; for (Hi = 0; Hi < 360; Hi++) { h = Hi*1.0/360.0; gtk_hsv_to_rgb(h,s,v,&r,&g,&b); H = round(h*65535.0); S = round(s*65535.0); V = round(v*65535.0); R = round(r*65535.0); G = round(g*65535.0); B = round(b*65535.0); HSV.HSV.H = H; HSV.HSV.S = S; HSV.HSV.V = V; HSV2RGB(&HSV,&RGB); Error = abs((int)RGB.RGB.R-(int)R) + abs((int)RGB.RGB.G-(int)G) + abs((int)RGB.RGB.B-(int)B); if (Error > MAXDIFF) { printf("HSV2RGB %3d %3d %3d: %5d %5d %5d -> %5d %5d %5d", Hi,Si,Vi, H,S,V, RGB.RGB.R,RGB.RGB.G,RGB.RGB.B); printf(" (should be %5d %5d %5d, Error = %d)",R,G,B,Error); printf("\n"); TotalError += Error; Errors++; } } } } #endif // TEST_HSV2RGB #ifdef TEST_RGB2HSV // Test RGB2HSV for (Bi = 0; Bi <= 100; Bi++) { b = Bi*1.0/100.0; for (Gi = 0; Gi <= 100; Gi++) { g = Gi*1.0/100.0; for (Ri = 0; Ri <= 100; Ri++) { r = Ri*1.0/100.0; gtk_rgb_to_hsv(r,g,b,&h,&s,&v); R = round(r*65535.0); G = round(g*65535.0); B = round(b*65535.0); H = round(h*65535.0); S = round(s*65535.0); V = round(v*65535.0); RGB.RGB.R = R; RGB.RGB.G = G; RGB.RGB.B = B; RGB2HSV(&RGB,&HSV); Error = abs((int)HSV.HSV.H-(int)H) + abs((int)HSV.HSV.S-(int)S) + abs((int)HSV.HSV.V-(int)V); if (Error > MAXDIFF) { printf("RGB2HSV %3d %3d %3d: %5d %5d %5d -> %5d %5d %5d", Ri,Gi,Bi, R,G,B, HSV.HSV.H,HSV.HSV.S,HSV.HSV.V); printf(" (should be %5d %5d %5d, Error = %d)",H,S,V,Error); printf("\n"); TotalError += Error; Errors++; } } } } #endif // TEST_RGB2HSV #ifdef TEST_WHITE2RGB printf("<pre>\n"); for (T = 1000; T <= 40000; T+=100) { White2RGB(T,&RGB); printf("<span style=\"background:#%02x%02x%02x\"> %5d K -> %5d %5d %5d - #%02x%02x%02x</span>\n", RGB.RGB.R >> 8,RGB.RGB.G >> 8,RGB.RGB.B >> 8, T, RGB.RGB.R,RGB.RGB.G,RGB.RGB.B, RGB.RGB.R >> 8,RGB.RGB.G >> 8,RGB.RGB.B >> 8); } printf("</pre>\n"); #endif printf("%d errors found, total deviation is %d.\n",Errors,TotalError); return (Errors == 0 ? 0 : 1); }