Example #1
0
        public static CGColor ToCG(this NSColor color)
        {
            if (color == null)
            {
                return(null);
            }
            CGColor cgColor;

            // try getting the CGColor and ensure it isn't null..
            if (MacVersion.IsAtLeast(10, 8) && (cgColor = color.CGColor) != null)
            {
                return(cgColor);
            }

            // try to convert to RGB colorspace so we can convert it to CGColor
            var converted = color.UsingColorSpaceFast(NSColorSpace.DeviceRGB);

            if (converted == null)
            {
                return(new CGColor(0, 0, 0, 1f));
            }

            // try getting the CGColor again..
            if (MacVersion.IsAtLeast(10, 8) && (cgColor = converted.CGColor) != null)
            {
                return(cgColor);
            }

            converted.GetComponents(out var components);
            return(new CGColor(converted.ColorSpace.ColorSpace, components));
        }
Example #2
0
        public static Color ToEto(this NSColor color, bool calibrated = false)
        {
            if (color == null)
            {
                return(Colors.Transparent);
            }

            var colorspace = calibrated ? NSColorSpace.CalibratedRGB : NSColorSpace.DeviceRGB;
            var converted  = color.UsingColorSpaceFast(colorspace);

            if (converted == null)
            {
                // Convert named (e.g. system) colors to RGB using its CGColor
                converted = color.ToCG().ToNS().UsingColorSpaceFast(colorspace);

                if (converted == null)
                {
                    return(new Color(color, 0, 0, 0, 1f));
                }
            }
            nfloat red, green, blue, alpha;

            converted.GetRgba(out red, out green, out blue, out alpha);
            return(new Color(color, (float)red, (float)green, (float)blue, (float)alpha));
        }
Example #3
0
        public static CGColor ToCG(this NSColor color)
        {
            if (color == null)
            {
                return(null);
            }
            if (MacVersion.IsAtLeast(10, 8))
            {
                return(color.CGColor);
            }

            var converted = color.UsingColorSpaceFast(NSColorSpace.DeviceRGB);

            if (converted == null)
            {
                return(new CGColor(0, 0, 0, 1f));
            }

            converted.GetComponents(out var components);
            return(new CGColor(converted.ColorSpace.ColorSpace, components));
        }