Esempio n. 1
0
        // Convert an RGB color value into a pixel using this colormap.
        // This assumes that standard colors have already been expanded.
        internal XPixel RGBToPixel(Color color)
        {
            try
            {
                // Acquire the display lock.
                IntPtr display = dpy.Lock();

                // Do we already have a cached pixel value for this color?
                Object cached = cachedPixels[color];
                if (cached != null)
                {
                    return((XPixel)(cached));
                }

                // Try to allocate the color from the X display server.
                XColor xcolor;
                int    red   = color.Red;
                int    green = color.Green;
                int    blue  = color.Blue;
                xcolor.pixel = XPixel.Zero;
                xcolor.red   = (ushort)(red << 8);
                xcolor.green = (ushort)(green << 8);
                xcolor.blue  = (ushort)(blue << 8);
                xcolor.flags =
                    (sbyte)(XColor.DoRed | XColor.DoGreen | XColor.DoBlue);
                xcolor.pad = (sbyte)0;
                if (Xlib.XAllocColor(display, colormap, ref xcolor) != 0)
                {
                    // Cache the value for next time.
                    cachedPixels[color] = xcolor.pixel;
                    return(xcolor.pixel);
                }

                // TODO: do a closest color match for the color.

                // Last ditch: return black or white depending upon
                // the intensity of the color.
                if (((color.Red * 54 + color.Green * 183 +
                      color.Blue * 19) / 256) < 128)
                {
                    return(Xlib.XBlackPixelOfScreen(screen.screen));
                }
                else
                {
                    return(Xlib.XWhitePixelOfScreen(screen.screen));
                }
            }
            finally
            {
                dpy.Unlock();
            }
        }
Esempio n. 2
0
        private ulong GetPixelByName(string name)
        {
            var    screen = Xlib.XDefaultScreen(this.display);
            XColor color  = new XColor();

            if (0 == Xlib.XParseColor(this.display, Xlib.XDefaultColormap(this.display, screen), name, ref color))
            {
                Log.Error($"Invalid Color {name}");
            }

            if (0 == Xlib.XAllocColor(this.display, Xlib.XDefaultColormap(this.display, screen), ref color))
            {
                Log.Error($"Failed to allocate color {name}");
            }

            return(color.pixel);
        }