Example #1
0
        public override void DrawRect(CGRect dirtyRect)
        {
            NSColorList colors = NSColorList.ColorListNamed("System");
            CGRect      rect   = Bounds;

            rect.Height = 12;
            var style = (NSMutableParagraphStyle)NSParagraphStyle.DefaultParagraphStyle.MutableCopy();

            style.Alignment = NSTextAlignment.Right;
            var attrs = new NSStringAttributes {
                Font            = NSFont.SystemFontOfSize(8),
                ForegroundColor = NSColor.LabelColor,
                ParagraphStyle  = style
            };

            foreach (NSString key in colors.AllKeys())
            {
                if (DrawColors)
                {
                    NSColor color = colors.ColorWithKey(key);
                    color.Set();
                    NSGraphics.RectFill(rect);
                }

                if (DrawTitles)
                {
                    key.DrawString(rect, attrs.Dictionary);
                }

                rect.Y += 12;
            }
        }
Example #2
0
        // Gets the string for the text field from the object passed in.
        public override string StringFor(NSObject value)
        {
            // Not a color?
            if (value.GetType() != typeof(NSColor))
            {
                return(null);
            }

            // Convert to an RGB color space
            NSColor color = ((NSColor)value).UsingColorSpace(NSColorSpace.CalibratedRGB);

            // Get components as floats between 0 and 1
            nfloat red, green, blue, alpha;

            color.GetRgba(out red, out green, out blue, out alpha);

            // Initialize the distance to something large
            double minDistance = 3.0f;
            string closestKey  = "";

            // Find the closest color
            foreach (string key in colorList.AllKeys())
            {
                NSColor c = colorList.ColorWithKey(key);
                nfloat  r, g, b, a;
                c.GetRgba(out r, out g, out b, out a);

                // How far apart are color and c?
                double distance = (Math.Pow(red - r, 2) + Math.Pow(green - g, 2) + Math.Pow(blue - b, 2));
                // Is this the closest yet?
                if (distance < minDistance)
                {
                    minDistance = distance;
                    closestKey  = key;
                }
            }
            return(closestKey);
        }