📄 gdkcolor-x11.c
字号:
for (i = 0; i < size; i++) { palette[i].pixel = i << shift; palette[i].red = colormap->colors[i].red; palette[i].flags = DoRed; } XStoreColors (xdisplay, private->xcolormap, palette, size); shift = visual->green_shift; max_colors = 1 << visual->green_prec; size = (ncolors < max_colors) ? (ncolors) : (max_colors); for (i = 0; i < size; i++) { palette[i].pixel = i << shift; palette[i].green = colormap->colors[i].green; palette[i].flags = DoGreen; } XStoreColors (xdisplay, private->xcolormap, palette, size); shift = visual->blue_shift; max_colors = 1 << visual->blue_prec; size = (ncolors < max_colors) ? (ncolors) : (max_colors); for (i = 0; i < size; i++) { palette[i].pixel = i << shift; palette[i].blue = colormap->colors[i].blue; palette[i].flags = DoBlue; } XStoreColors (xdisplay, private->xcolormap, palette, size); break; default: break; } g_free (palette);}/** * gdk_colors_alloc: * @colormap: a #GdkColormap. * @contiguous: if %TRUE, the colors should be allocated * in contiguous color cells. * @planes: an array in which to store the plane masks. * @nplanes: the number of planes to allocate. (Or zero, * to indicate that the color allocation should not be planar.) * @pixels: an array into which to store allocated pixel values. * @npixels: the number of pixels in each plane to allocate. * * Allocates colors from a colormap. This function * is obsolete. See gdk_colormap_alloc_colors(). * For full documentation of the fields, see * the Xlib documentation for <function>XAllocColorCells()</function>. * * Return value: **/gbooleangdk_colors_alloc (GdkColormap *colormap, gboolean contiguous, gulong *planes, gint nplanes, gulong *pixels, gint npixels){ GdkColormapPrivateX11 *private; gint return_val; gint i; g_return_val_if_fail (GDK_IS_COLORMAP (colormap), FALSE); private = GDK_COLORMAP_PRIVATE_DATA (colormap); if (private->screen->closed) return FALSE; return_val = XAllocColorCells (GDK_SCREEN_XDISPLAY (private->screen), private->xcolormap,contiguous, planes, nplanes, pixels, npixels); if (return_val) { for (i = 0; i < npixels; i++) { private->info[pixels[i]].ref_count++; private->info[pixels[i]].flags |= GDK_COLOR_WRITEABLE; } } return return_val != 0;}/* This is almost identical to gdk_colormap_free_colors. * Keep them in sync! *//** * gdk_colors_free: * @colormap: a #GdkColormap. * @pixels: the pixel values of the colors to free. * @npixels: the number of values in @pixels. * @planes: the plane masks for all planes to free, OR'd together. * * Frees colors allocated with gdk_colors_alloc(). This * function is obsolete. See gdk_colormap_free_colors(). **/voidgdk_colors_free (GdkColormap *colormap, gulong *pixels, gint npixels, gulong planes){ GdkColormapPrivateX11 *private; gulong *pixels_to_free; gint npixels_to_free = 0; gint i; g_return_if_fail (GDK_IS_COLORMAP (colormap)); g_return_if_fail (pixels != NULL); private = GDK_COLORMAP_PRIVATE_DATA (colormap); if ((colormap->visual->type != GDK_VISUAL_PSEUDO_COLOR) && (colormap->visual->type != GDK_VISUAL_GRAYSCALE)) return; pixels_to_free = g_new (gulong, npixels); for (i = 0; i < npixels; i++) { gulong pixel = pixels[i]; if (private->info[pixel].ref_count) { private->info[pixel].ref_count--; if (private->info[pixel].ref_count == 0) { pixels_to_free[npixels_to_free++] = pixel; if (!(private->info[pixel].flags & GDK_COLOR_WRITEABLE)) g_hash_table_remove (private->hash, &colormap->colors[pixel]); private->info[pixel].flags = 0; } } } if (npixels_to_free && !private->private_val && !private->screen->closed) XFreeColors (GDK_SCREEN_XDISPLAY (private->screen), private->xcolormap, pixels_to_free, npixels_to_free, planes); g_free (pixels_to_free);}/* This is almost identical to gdk_colors_free. * Keep them in sync! *//** * gdk_colormap_free_colors: * @colormap: a #GdkColormap. * @colors: the colors to free. * @ncolors: the number of colors in @colors. * * Frees previously allocated colors. **/voidgdk_colormap_free_colors (GdkColormap *colormap, GdkColor *colors, gint ncolors){ GdkColormapPrivateX11 *private; gulong *pixels; gint npixels = 0; gint i; g_return_if_fail (GDK_IS_COLORMAP (colormap)); g_return_if_fail (colors != NULL); private = GDK_COLORMAP_PRIVATE_DATA (colormap); if ((colormap->visual->type != GDK_VISUAL_PSEUDO_COLOR) && (colormap->visual->type != GDK_VISUAL_GRAYSCALE)) return; pixels = g_new (gulong, ncolors); for (i = 0; i < ncolors; i++) { gulong pixel = colors[i].pixel; if (private->info[pixel].ref_count) { private->info[pixel].ref_count--; if (private->info[pixel].ref_count == 0) { pixels[npixels++] = pixel; if (!(private->info[pixel].flags & GDK_COLOR_WRITEABLE)) g_hash_table_remove (private->hash, &colormap->colors[pixel]); private->info[pixel].flags = 0; } } } if (npixels && !private->private_val && !private->screen->closed) XFreeColors (GDK_SCREEN_XDISPLAY (private->screen), private->xcolormap, pixels, npixels, 0); g_free (pixels);}/******************** * Color allocation * ********************//* Try to allocate a single color using XAllocColor. If it succeeds, * cache the result in our colormap, and store in ret. */static gboolean gdk_colormap_alloc1 (GdkColormap *colormap, GdkColor *color, GdkColor *ret){ GdkColormapPrivateX11 *private; XColor xcolor; private = GDK_COLORMAP_PRIVATE_DATA (colormap); xcolor.red = color->red; xcolor.green = color->green; xcolor.blue = color->blue; xcolor.pixel = color->pixel; xcolor.flags = DoRed | DoGreen | DoBlue; if (XAllocColor (GDK_SCREEN_XDISPLAY (private->screen), private->xcolormap, &xcolor)) { ret->pixel = xcolor.pixel; ret->red = xcolor.red; ret->green = xcolor.green; ret->blue = xcolor.blue; if (ret->pixel < colormap->size) { if (private->info[ret->pixel].ref_count) /* got a duplicate */ { XFreeColors (GDK_SCREEN_XDISPLAY (private->screen), private->xcolormap, &xcolor.pixel, 1, 0); private->info[ret->pixel].ref_count++; } else { colormap->colors[ret->pixel] = *color; colormap->colors[ret->pixel].pixel = ret->pixel; private->info[ret->pixel].ref_count = 1; g_hash_table_insert (private->hash, &colormap->colors[ret->pixel], &colormap->colors[ret->pixel]); } } return TRUE; } else { return FALSE; }}static gintgdk_colormap_alloc_colors_writeable (GdkColormap *colormap, GdkColor *colors, gint ncolors, gboolean writeable, gboolean best_match, gboolean *success){ GdkColormapPrivateX11 *private; gulong *pixels; Status status; gint i, index; private = GDK_COLORMAP_PRIVATE_DATA (colormap); if (private->private_val) { index = 0; for (i = 0; i < ncolors; i++) { while ((index < colormap->size) && (private->info[index].ref_count != 0)) index++; if (index < colormap->size) { colors[i].pixel = index; success[i] = TRUE; private->info[index].ref_count++; private->info[i].flags |= GDK_COLOR_WRITEABLE; } else break; } return ncolors - i; } else { pixels = g_new (gulong, ncolors); /* Allocation of a writeable color cells */ status = XAllocColorCells (GDK_SCREEN_XDISPLAY (private->screen), private->xcolormap, FALSE, NULL, 0, pixels, ncolors); if (status) { for (i = 0; i < ncolors; i++) { colors[i].pixel = pixels[i]; success[i] = TRUE; private->info[pixels[i]].ref_count++; private->info[pixels[i]].flags |= GDK_COLOR_WRITEABLE; } } g_free (pixels); return status ? 0 : ncolors; }}static gintgdk_colormap_alloc_colors_private (GdkColormap *colormap, GdkColor *colors, gint ncolors, gboolean writeable, gboolean best_match, gboolean *success){ GdkColormapPrivateX11 *private; gint i, index; XColor *store = g_new (XColor, ncolors); gint nstore = 0; gint nremaining = 0; private = GDK_COLORMAP_PRIVATE_DATA (colormap); /* First, store the colors we have room for */ index = 0; for (i = 0; i < ncolors; i++) { if (!success[i]) { while ((index < colormap->size) && (private->info[index].ref_count != 0)) index++; if (index < colormap->size) { store[nstore].red = colors[i].red; store[nstore].blue = colors[i].blue; store[nstore].green = colors[i].green; store[nstore].pixel = index; store[nstore].flags = DoRed | DoGreen | DoBlue; nstore++; success[i] = TRUE; colors[i].pixel = index; colormap->colors[index] = colors[i]; private->info[index].ref_count++; g_hash_table_insert (private->hash, &colormap->colors[index], &colormap->colors[index]); } else nremaining++; } } XStoreColors (GDK_SCREEN_XDISPLAY (private->screen), private->xcolormap, store, nstore); g_free (store); if (nremaining > 0 && best_match) { /* Get best matches for remaining colors */ gchar *available = g_new (gchar, colormap->size); for (i = 0; i < colormap->size; i++) available[i] = !(private->info[i].flags & GDK_COLOR_WRITEABLE); for (i = 0; i < ncolors; i++) { if (!success[i]) { index = gdk_colormap_match_color (colormap, &colors[i], available); if (index != -1) { colors[i] = colormap->colors[index]; private->info[index].ref_count++; success[i] = TRUE; nremaining--; } } } g_free (available); } return nremaining;}static gintgdk_colormap_alloc_colors_shared (GdkColormap *colormap, GdkColor *colors, gint ncolors, gboolean writeable, gboolean best_match, gboolean *success){ GdkColormapPrivateX11 *private; gint i, index; gint nremaining = 0; gint nfailed = 0; private = GDK_COLORMAP_PRIVATE_DATA (colormap); for (i = 0; i < ncolors; i++) { if (!success[i]) { if (gdk_colormap_alloc1 (colormap, &colors[i], &colors[i])) success[i] = TRUE; else nremaining++; } } if (nremaining > 0 && best_match) { gchar *available = g_new (gchar, colormap->size); for (i = 0; i < colormap->size; i++) available[i] = ((private->info[i].ref_count == 0) || !(private->info[i].flags & GDK_COLOR_WRITEABLE)); gdk_colormap_sync (colormap, FALSE); while (nremaining > 0) { for (i = 0; i < ncolors; i++) { if (!success[i]) { index = gdk_colormap_match_color (colormap, &colors[i], available); if (index != -1) { if (private->info[index].ref_count) { private->info[index].ref_count++; colors[i] = colormap->colors[index]; success[i] = TRUE; nremaining--; } else { if (gdk_colormap_alloc1 (colormap, &colormap->colors[index], &colors[i])) { success[i] = TRUE; nremaining--; break; } else { available[index] = FALSE; } } } else { nfailed++; nremaining--; success[i] = 2; /* flag as permanent failure */ } } } } g_free (available); } /* Change back the values we flagged as permanent failures */ if (nfailed > 0) { for (i = 0; i < ncolors; i++) if (success[i] == 2) success[i] = FALSE; nremaining = nfailed; } return nremaining;}static gintgdk_colormap_alloc_colors_pseudocolor (GdkColormap *colormap, GdkColor *colors, gint ncolors,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -