Click here to Skip to main content
15,896,153 members
Articles / Programming Languages / C#

Manipulating colors in .NET - Part 1

Rate me:
Please Sign up or sign in to vote.
4.96/5 (275 votes)
3 Jun 2007CPOL16 min read 547.9K   15.8K   440  
Understand and use color models in .NET
using System;

namespace Devcorp.Controls.Design
{
	/// <summary>
	/// Structure to define CMYK.
	/// </summary>
	public struct CMYK 
	{
		/// <summary>
		/// Gets an empty CMYK structure;
		/// </summary>
		public readonly static CMYK Empty = new CMYK();

		#region Fields
		private double c; 
		private double m; 
		private double y; 
		private double k;
		#endregion

		#region Operators
		public static bool operator ==(CMYK item1, CMYK item2)
		{
			return (
				item1.Cyan == item2.Cyan 
				&& item1.Magenta == item2.Magenta 
				&& item1.Yellow == item2.Yellow
				&& item1.Black == item2.Black
				);
		}

		public static bool operator !=(CMYK item1, CMYK item2)
		{
			return (
				item1.Cyan != item2.Cyan 
				|| item1.Magenta != item2.Magenta 
				|| item1.Yellow != item2.Yellow
				|| item1.Black != item2.Black
				);
		}


		#endregion

		#region Accessors
		public double Cyan
		{ 
			get
			{
				return c;
			} 
			set
			{ 
				c = value; 
				c = (c>1)? 1 : ((c<0)? 0 : c); 
			} 
		} 

		public double Magenta
		{ 
			get
			{
				return m;
			} 
			set
			{ 
				m = value; 
				m = (m>1)? 1 : ((m<0)? 0 : m); 
			} 
		} 

		public double Yellow
		{ 
			get
			{
				return y;
			} 
			set
			{ 
				y = value; 
				y = (y>1)? 1 : ((y<0)? 0 : y); 
			} 
		} 

		public double Black 
		{ 
			get
			{
				return k;
			} 
			set
			{ 
				k = value; 
				k = (k>1)? 1 : ((k<0)? 0 : k); 
			} 
		} 
		#endregion

		/// <summary>
		/// Creates an instance of a CMYK structure.
		/// </summary>
		public CMYK(double c, double m, double y, double k) 
		{
			this.c = c;
			this.m = m;
			this.y = y;
			this.k = k;
		}

		#region Methods
		public override bool Equals(Object obj) 
		{
			if(obj==null || GetType()!=obj.GetType()) return false;

			return (this == (CMYK)obj);
		}

		public override int GetHashCode() 
		{
			return Cyan.GetHashCode() ^ Magenta.GetHashCode() ^ Yellow.GetHashCode() ^ Black.GetHashCode();
		}

		#endregion
	} 
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
France France
IT consultant and Project Manager in Paris, specialized in software engineering/design.

He spends most of his time in meetings Smile | :)
He would love to have more time to develop all those ideas/concepts he has in mind.

Comments and Discussions