Click here to Skip to main content
15,883,733 members
Articles / Desktop Programming / WPF

Writing Your Own RTF Converter

Rate me:
Please Sign up or sign in to vote.
4.95/5 (234 votes)
1 Aug 2013CPOL14 min read 2.4M   40.4K   632  
An article on how to write a custom RTF parser and converter.
// -- FILE ------------------------------------------------------------------
// name       : RtfColor.cs
// project    : RTF Framelet
// created    : Leon Poyyayil - 2008.05.21
// language   : c#
// environment: .NET 2.0
// copyright  : (c) 2004-2010 by Itenso GmbH, Switzerland
// --------------------------------------------------------------------------
using System.Drawing;
using Itenso.Sys;

namespace Itenso.Rtf.Model
{

	// ------------------------------------------------------------------------
	public sealed class RtfColor : IRtfColor
	{

		// ----------------------------------------------------------------------
		public static readonly IRtfColor Black = new RtfColor( 0, 0, 0 );
		public static readonly IRtfColor White = new RtfColor( 255, 255, 255 );

		// ----------------------------------------------------------------------
		public RtfColor( int red, int green, int blue )
		{
			if ( red < 0 || red > 255 )
			{
				throw new RtfColorException( Strings.InvalidColor( red ) );
			}
			if ( green < 0 || green > 255 )
			{
				throw new RtfColorException( Strings.InvalidColor( green ) );
			}
			if ( blue < 0 || blue > 255 )
			{
				throw new RtfColorException( Strings.InvalidColor( blue ) );
			}
			this.red = red;
			this.green = green;
			this.blue = blue;
			drawingColor = Color.FromArgb( red, green, blue );
		} // RtfColor

		// ----------------------------------------------------------------------
		public int Red
		{
			get { return red; }
		} // Red

		// ----------------------------------------------------------------------
		public int Green
		{
			get { return green; }
		} // Green

		// ----------------------------------------------------------------------
		public int Blue
		{
			get { return blue; }
		} // Blue

		// ----------------------------------------------------------------------
		public Color AsDrawingColor
		{
			get { return drawingColor; }
		} // AsDrawingColor

		// ----------------------------------------------------------------------
		public override bool Equals( object obj )
		{
			if ( obj == this )
			{
				return true;
			}
			
			if ( obj == null || GetType() != obj.GetType() )
			{
				return false;
			}

			return IsEqual( obj );
		} // Equals

		// ----------------------------------------------------------------------
		public override int GetHashCode()
		{
			return HashTool.AddHashCode( GetType().GetHashCode(), ComputeHashCode() );
		} // GetHashCode

		// ----------------------------------------------------------------------
		public override string ToString()
		{
			return "Color{" + red + "," + green + "," + blue + "}";
		} // ToString

		// ----------------------------------------------------------------------
		private bool IsEqual( object obj )
		{
			RtfColor compare = obj as RtfColor; // guaranteed to be non-null
			return compare != null && red == compare.red &&
				green == compare.green &&
				blue == compare.blue;
		} // IsEqual

		// ----------------------------------------------------------------------
		private int ComputeHashCode()
		{
			int hash = red;
			hash = HashTool.AddHashCode( hash, green );
			hash = HashTool.AddHashCode( hash, blue );
			return hash;
		} // ComputeHashCode

		// ----------------------------------------------------------------------
		// members
		private readonly int red;
		private readonly int green;
		private readonly int blue;
		private readonly Color drawingColor;

	} // class RtfColor

} // namespace Itenso.Rtf.Model
// -- EOF -------------------------------------------------------------------

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
Software Developer (Senior)
Switzerland Switzerland
👨 Senior .NET Software Engineer

🚀 My Open Source Projects
- Time Period Library 👉 GitHub
- Payroll Engine 👉 GitHub

Feedback and contributions are welcome.



Comments and Discussions