private void PrintColor(NSColor color)
 {
     color = color.UsingColorSpace(NSColorSpace.GenericRGBColorSpace);
     TextFieldR.IntValue = (int)(color.RedComponent * 255);
     TextFieldG.IntValue = (int)(color.GreenComponent * 255);
     TextFieldB.IntValue = (int)(color.BlueComponent * 255);
 }
Beispiel #2
0
        public static Color AsColor(this NSColor color)
        {
            var convertedColorspace = color.UsingColorSpace(NSColorSpace.GenericRGBColorSpace);

            convertedColorspace.GetRgba(out var red, out var green, out var blue, out var alpha);
            return(new Color((float)red, (float)green, (float)blue, (float)alpha));
        }
Beispiel #3
0
 public static Color ToColor(this NSColor clr)
 {
     if (clr == null)
     {
         return(Color.Transparent);
     }
     clr = clr.UsingColorSpace(NSColorSpace.CalibratedRGB);
     return(Color.FromArgb((int)clr.AlphaComponent, (int)clr.RedComponent, (int)clr.GreenComponent, (int)clr.BlueComponent));
 }
Beispiel #4
0
        public static Color ToColor(this NSColor color)
        {
            if (color == null)
            {
                return(null);
            }

            color = color.UsingColorSpace(NSColorSpace.DeviceRGB);
            return(new Color((float)color.RedComponent, (float)color.GreenComponent, (float)color.BlueComponent, (float)color.AlphaComponent));
        }
Beispiel #5
0
        public static CGColor ToCG(this NSColor color)
        {
            var cs = NSColorSpace.DeviceRGBColorSpace;

            var devColor = color.UsingColorSpace(cs);

            float[] components;
            devColor.GetComponents(out components);
            return(new CGColor(cs.ColorSpace, components));
        }
Beispiel #6
0
        void OnColorChange(string name, NSColor color)
        {
            mainView.WantsLayer = true;

            // Prevent crashes when asking for components
            color = color.UsingColorSpace(NSColorSpace.CalibratedRGB);

            mainView.Layer.BackgroundColor = color.CGColor;

            Console.WriteLine("Color changed on {0} ({1}, {2}, {3}, {4})", name, color.RedComponent, color.GreenComponent, color.BlueComponent, color.AlphaComponent);
        }
Beispiel #7
0
        public static Color ToColor(this NSColor value)
        {
            if (value == null)
            {
                return(new Color(0, 0, 0, 1));
            }

            var calibratedColor = value.UsingColorSpace(NSColorSpace.DeviceRGBColorSpace);

            return(new Color((float)calibratedColor.RedComponent, (float)calibratedColor.GreenComponent, (float)calibratedColor.BlueComponent, (float)calibratedColor.AlphaComponent));
        }
Beispiel #8
0
        public static Color ToXwtColor(this NSColor col)
        {
            var calibrated = col.UsingColorSpace(DeviceRGBString);

            if (calibrated != null)
            {
                return(new Color(calibrated.RedComponent, calibrated.GreenComponent, calibrated.BlueComponent, calibrated.AlphaComponent));
            }
            // some system colors can not be calibrated and UsingColorSpace returns null.
            // Use CGColor in this case, which should match the device already.
            return(col.CGColor.ToXwtColor());
        }
Beispiel #9
0
        public static Color ToEto(this NSColor color)
        {
            if (color == null)
            {
                return(Colors.Black);
            }
            //if (color.ColorSpace.ColorSpaceModel != NSColorSpaceModel.RGB)
            color = color.UsingColorSpace(NSColorSpace.CalibratedRGB);
            nfloat red, green, blue, alpha;

            color.GetRgba(out red, out green, out blue, out alpha);
            return(new Color((float)red, (float)green, (float)blue, (float)alpha));
        }
        public static Color ToSystemColor(this NSColor color)
        {
            if (color == null)
            {
                throw new ArgumentNullException(nameof(color));
            }

            // make sure the colorspace is valid for RGBA
            // we can't check as the check will throw if it is invalid
            color = color.UsingColorSpace(NSColorSpace.SRGBColorSpace);

            color.GetRgba(out var red, out var green, out var blue, out var alpha);
            return(Color.FromArgb((int)(alpha * 255), (int)(red * 255), (int)(green * 255), (int)(blue * 255)));
        }
Beispiel #11
0
        public static Color ToEto(this NSColor color)
        {
            if (color == null)
            {
                return(Colors.Black);
            }
            var converted = color.UsingColorSpace(NSColorSpace.CalibratedRGB);

            if (converted == null)
            {
                // Convert named (e.g. system) colors to RGB using its CGColor
                converted = color.CGColor.ToNS().UsingColorSpace(NSColorSpace.CalibratedRGB);
                if (converted == null)
                {
                    throw new ArgumentOutOfRangeException("color", "Color cannot be converted to an RGB colorspace");
                }
            }
            nfloat red, green, blue, alpha;

            converted.GetRgba(out red, out green, out blue, out alpha);
            return(new Color((float)red, (float)green, (float)blue, (float)alpha));
        }
Beispiel #12
0
        public static bool ToArgb(this NSColor color, out float a, out float r, out float g, out float b)
        {
            NSColor rgba = color.UsingColorSpace(GenericRgbColorSpace());

            if (rgba != null)
            {
                rgba.GetRgba(out nfloat nr, out nfloat ng, out nfloat nb, out nfloat na);
                a = (float)na;
                r = (float)nr;
                g = (float)ng;
                b = (float)nb;
                return(true);
            }

            var cgc = color.CGColor;             // 10.8+

            if (cgc != null)
            {
                if (cgc.NumberOfComponents == 4)
                {
                    a = (float)cgc.Components[3];
                    r = (float)cgc.Components[0];
                    g = (float)cgc.Components[1];
                    b = (float)cgc.Components[2];
                    return(true);
                }

                if (cgc.NumberOfComponents == 2)
                {
                    a = (float)cgc.Components[1];
                    r = g = b = (float)cgc.Components[0];
                    return(true);
                }
            }

            a = r = g = b = 0;
            return(false);
        }
Beispiel #13
0
        public static Color ToSDColor(this NSColor color)
        {
            var convertedColor = color.UsingColorSpace(NSColorSpace.GenericRGBColorSpace);

            if (convertedColor != null)
            {
                nfloat r, g, b, a;
                convertedColor.GetRgba(out r, out g, out b, out a);
                return(Color.FromArgb((int)(a * 255), (int)(r * 255), (int)(g * 255), (int)(b * 255)));
            }

            var cgColor = color.CGColor;             // 10.8+

            if (cgColor != null)
            {
                if (cgColor.NumberOfComponents == 4)
                {
                    return(Color.FromArgb(
                               (int)(cgColor.Components[3] * 255),
                               (int)(cgColor.Components[0] * 255),
                               (int)(cgColor.Components[1] * 255),
                               (int)(cgColor.Components[2] * 255)));
                }

                if (cgColor.NumberOfComponents == 2)
                {
                    return(Color.FromArgb(
                               (int)(cgColor.Components[1] * 255),
                               (int)(cgColor.Components[0] * 255),
                               (int)(cgColor.Components[0] * 255),
                               (int)(cgColor.Components[0] * 255)));
                }
            }

            return(Color.Transparent);
        }
        public static bool ToArgb(this NSColor color, out float a, out float r, out float g, out float b)
        {
            var cgc = color.CGColor;             // 10.8+

            if (cgc != null)
            {
                if (cgc.NumberOfComponents == 4 && cgc.ColorSpace.Name == CGColorSpaceNames.SRGB)
                {
                    r = (float)cgc.Components[0];
                    g = (float)cgc.Components[1];
                    b = (float)cgc.Components[2];
                    a = (float)cgc.Components[3];
                    return(true);
                }

                if (cgc.NumberOfComponents == 2 && cgc.ColorSpace.Name == CGColorSpaceNames.LinearGray)
                {
                    a = (float)cgc.Components[1];
                    r = g = b = (float)cgc.Components[0];
                    return(true);
                }
            }

            if (color.UsingColorSpace(SRGBColorSpace()) is NSColor rgba)
            {
                rgba.GetRgba(out nfloat nr, out nfloat ng, out nfloat nb, out nfloat na);
                a = (float)na;
                r = (float)nr;
                g = (float)ng;
                b = (float)nb;
                return(true);
            }

            a = r = g = b = 0;
            return(false);
        }
Beispiel #15
0
		public static SCNNode SCBoxNode (string title, CGRect frame, NSColor color, float cornerRadius, bool centered)
		{
			NSMutableDictionary titleAttributes = null;
			NSMutableDictionary centeredTitleAttributes = null;

			// create and extrude a bezier path to build the box
			var path = NSBezierPath.FromRoundedRect (frame, cornerRadius, cornerRadius);
			path.Flatness = 0.05f;

			var shape = SCNShape.Create (path, 20);
			shape.ChamferRadius = 0.0f;

			var node = SCNNode.Create ();
			node.Geometry = shape;

			// create an image and fill with the color and text
			var textureSize = new CGSize ();
			textureSize.Width = NMath.Ceiling (frame.Size.Width * 1.5f);
			textureSize.Height = NMath.Ceiling (frame.Size.Height * 1.5f);

			var texture = new NSImage (textureSize);
			texture.LockFocus ();

			var drawFrame = new CGRect (0, 0, textureSize.Width, textureSize.Height);

			nfloat hue, saturation, brightness, alpha;

			(color.UsingColorSpace (NSColorSpace.DeviceRGBColorSpace)).GetHsba (out hue, out saturation, out brightness, out alpha);
			var lightColor = NSColor.FromDeviceHsba (hue, saturation - 0.2f, brightness + 0.3f, alpha);
			lightColor.Set ();

			NSGraphics.RectFill (drawFrame);

			NSBezierPath fillpath = null;

			if (cornerRadius == 0 && centered == false) {
				//special case for the "labs" slide
				drawFrame.Offset (0, -2);
				fillpath = NSBezierPath.FromRoundedRect (drawFrame, cornerRadius, cornerRadius);
			} else {
				drawFrame.Inflate (-3, -3);
				fillpath = NSBezierPath.FromRoundedRect (drawFrame, cornerRadius, cornerRadius);
			}

			color.Set ();
			fillpath.Fill ();

			// draw the title if any
			if (title != null) {
				if (titleAttributes == null) {
					var paraphStyle = new NSMutableParagraphStyle ();
					paraphStyle.LineBreakMode = NSLineBreakMode.ByWordWrapping;
					paraphStyle.Alignment = NSTextAlignment.Center;
					paraphStyle.MinimumLineHeight = 38;
					paraphStyle.MaximumLineHeight = 38;

					var font = NSFont.FromFontName ("Myriad Set Semibold", 34) != null ? NSFont.FromFontName ("Myriad Set Semibold", 34) : NSFont.FromFontName ("Avenir Medium", 34);

					var shadow = new NSShadow ();
					shadow.ShadowOffset = new CGSize (0, -2);
					shadow.ShadowBlurRadius = 4;
					shadow.ShadowColor = NSColor.FromDeviceWhite (0.0f, 0.5f);

					titleAttributes = new NSMutableDictionary ();
					titleAttributes.SetValueForKey (font, NSAttributedString.FontAttributeName);
					titleAttributes.SetValueForKey (NSColor.White, NSAttributedString.ForegroundColorAttributeName);
					titleAttributes.SetValueForKey (shadow, NSAttributedString.ShadowAttributeName);
					titleAttributes.SetValueForKey (paraphStyle, NSAttributedString.ParagraphStyleAttributeName);

					var centeredParaphStyle = (NSMutableParagraphStyle)paraphStyle.MutableCopy ();
					centeredParaphStyle.Alignment = NSTextAlignment.Center;

					centeredTitleAttributes = new NSMutableDictionary ();
					centeredTitleAttributes.SetValueForKey (font, NSAttributedString.FontAttributeName);
					centeredTitleAttributes.SetValueForKey (NSColor.White, NSAttributedString.ForegroundColorAttributeName);
					centeredTitleAttributes.SetValueForKey (shadow, NSAttributedString.ShadowAttributeName);
					centeredTitleAttributes.SetValueForKey (paraphStyle, NSAttributedString.ParagraphStyleAttributeName);
				}

				var attrString = new NSAttributedString (title, centered ? centeredTitleAttributes : titleAttributes);
				var textSize = attrString.Size;

				//check if we need two lines to draw the text
				var twoLines = title.Contains ("\n");
				if (!twoLines)
					twoLines = textSize.Width > frame.Size.Width && title.Contains (" ");

				//if so, we need to adjust the size to center vertically
				if (twoLines)
					textSize.Height += 38;

				if (!centered)
					drawFrame.Inflate (-15, 0);

				//center vertically
				var dy = (drawFrame.Size.Height - textSize.Height) * 0.5f;
				var drawFrameHeight = drawFrame.Size.Height;
				drawFrame.Size = new CGSize (drawFrame.Size.Width, drawFrame.Size.Height - dy);
				attrString.DrawString (drawFrame);
			}

			texture.UnlockFocus ();

			//set the created image as the diffuse texture of our 3D box
			var front = SCNMaterial.Create ();
			front.Diffuse.Contents = texture;
			front.LocksAmbientWithDiffuse = true;

			//use a lighter color for the chamfer and sides
			var sides = SCNMaterial.Create ();
			sides.Diffuse.Contents = lightColor;
			node.Geometry.Materials = new SCNMaterial[] {
				front,
				sides,
				sides,
				sides,
				sides
			};

			return node;
		}
Beispiel #16
0
        public static SCNNode SCBoxNode(string title, CGRect frame, NSColor color, float cornerRadius, bool centered)
        {
            NSMutableDictionary titleAttributes         = null;
            NSMutableDictionary centeredTitleAttributes = null;

            // create and extrude a bezier path to build the box
            var path = NSBezierPath.FromRoundedRect(frame, cornerRadius, cornerRadius);

            path.Flatness = 0.05f;

            var shape = SCNShape.Create(path, 20);

            shape.ChamferRadius = 0.0f;

            var node = SCNNode.Create();

            node.Geometry = shape;

            // create an image and fill with the color and text
            var textureSize = new CGSize();

            textureSize.Width  = (float)Math.Ceiling((double)frame.Size.Width * 1.5);
            textureSize.Height = (float)Math.Ceiling((double)frame.Size.Height * 1.5);

            var texture = new NSImage(textureSize);

            texture.LockFocus();

            var drawFrame = new CGRect(0, 0, textureSize.Width, textureSize.Height);

            nfloat hue, saturation, brightness, alpha;

            (color.UsingColorSpace(NSColorSpace.DeviceRGBColorSpace)).GetHsba(out hue, out saturation, out brightness, out alpha);
            var lightColor = NSColor.FromDeviceHsba(hue, saturation - 0.2f, brightness + 0.3f, alpha);

            lightColor.Set();

            NSGraphics.RectFill(drawFrame);

            NSBezierPath fillpath = null;

            if (cornerRadius == 0 && centered == false)
            {
                //special case for the "labs" slide
                drawFrame.Offset(0, -2);
                fillpath = NSBezierPath.FromRoundedRect(drawFrame, cornerRadius, cornerRadius);
            }
            else
            {
                drawFrame.Inflate(-3, -3);
                fillpath = NSBezierPath.FromRoundedRect(drawFrame, cornerRadius, cornerRadius);
            }

            color.Set();
            fillpath.Fill();

            // draw the title if any
            if (title != null)
            {
                if (titleAttributes == null)
                {
                    var paraphStyle = new NSMutableParagraphStyle();
                    paraphStyle.LineBreakMode     = NSLineBreakMode.ByWordWrapping;
                    paraphStyle.Alignment         = NSTextAlignment.Center;
                    paraphStyle.MinimumLineHeight = 38;
                    paraphStyle.MaximumLineHeight = 38;

                    var font = NSFont.FromFontName("Myriad Set Semibold", 34) != null?NSFont.FromFontName("Myriad Set Semibold", 34) : NSFont.FromFontName("Avenir Medium", 34);

                    var shadow = new NSShadow();
                    shadow.ShadowOffset     = new CGSize(0, -2);
                    shadow.ShadowBlurRadius = 4;
                    shadow.ShadowColor      = NSColor.FromDeviceWhite(0.0f, 0.5f);

                    titleAttributes = new NSMutableDictionary();
                    titleAttributes.SetValueForKey(font, NSAttributedString.FontAttributeName);
                    titleAttributes.SetValueForKey(NSColor.White, NSAttributedString.ForegroundColorAttributeName);
                    titleAttributes.SetValueForKey(shadow, NSAttributedString.ShadowAttributeName);
                    titleAttributes.SetValueForKey(paraphStyle, NSAttributedString.ParagraphStyleAttributeName);

                    var centeredParaphStyle = (NSMutableParagraphStyle)paraphStyle.MutableCopy();
                    centeredParaphStyle.Alignment = NSTextAlignment.Center;

                    centeredTitleAttributes = new NSMutableDictionary();
                    centeredTitleAttributes.SetValueForKey(font, NSAttributedString.FontAttributeName);
                    centeredTitleAttributes.SetValueForKey(NSColor.White, NSAttributedString.ForegroundColorAttributeName);
                    centeredTitleAttributes.SetValueForKey(shadow, NSAttributedString.ShadowAttributeName);
                    centeredTitleAttributes.SetValueForKey(paraphStyle, NSAttributedString.ParagraphStyleAttributeName);
                }

                var attrString = new NSAttributedString(title, centered ? centeredTitleAttributes : titleAttributes);
                var textSize   = attrString.Size;

                //check if we need two lines to draw the text
                var twoLines = title.Contains("\n");
                if (!twoLines)
                {
                    twoLines = textSize.Width > frame.Size.Width && title.Contains(" ");
                }

                //if so, we need to adjust the size to center vertically
                if (twoLines)
                {
                    textSize.Height += 38;
                }

                if (!centered)
                {
                    drawFrame.Inflate(-15, 0);
                }

                //center vertically
                var dy = (drawFrame.Size.Height - textSize.Height) * 0.5f;
                var drawFrameHeight = drawFrame.Size.Height;
                drawFrame.Size = new CGSize(drawFrame.Size.Width, drawFrame.Size.Height - dy);
                attrString.DrawString(drawFrame);
            }

            texture.UnlockFocus();

            //set the created image as the diffuse texture of our 3D box
            var front = SCNMaterial.Create();

            front.Diffuse.Contents        = texture;
            front.LocksAmbientWithDiffuse = true;

            //use a lighter color for the chamfer and sides
            var sides = SCNMaterial.Create();

            sides.Diffuse.Contents  = lightColor;
            node.Geometry.Materials = new SCNMaterial[] {
                front,
                sides,
                sides,
                sides,
                sides
            };

            return(node);
        }
 static void OnColorChange(string name, NSColor color)
 {
     // Prevent crashes when asking for components
     color = color.UsingColorSpace(NSColorSpace.CalibratedRGB);
     Console.WriteLine("Color changed on {0} ({1}, {2}, {3}, {4})", name, color.RedComponent, color.GreenComponent, color.BlueComponent, color.AlphaComponent);
 }
Beispiel #18
0
 public static Color ToXwtColor(this NSColor col)
 {
     col = col.UsingColorSpace(DeviceRGBString);
     return(new Color(col.RedComponent, col.GreenComponent, col.BlueComponent, col.AlphaComponent));
 }