Click here to Skip to main content
15,896,606 members
Articles / Multimedia / GDI+

Drawing Library

Rate me:
Please Sign up or sign in to vote.
4.78/5 (63 votes)
10 Dec 2007CPOL3 min read 294.5K   12.9K   211  
A library for creating shapes and developing tools.
using System;
using System.Collections.Generic;
using System.Text;

using System.Drawing;

namespace Globe.Core.Converters
{
    /// <summary>
    /// Manages some conversions of a color.
    /// </summary>
    public class ColorConverter
    {
        #region Constructors

        /// <summary>
        /// Default constructor.
        /// </summary>
        public ColorConverter()
        {
        }

        #endregion

        #region Public Functions

        /// <summary>
        /// Gets a color from string.
        /// </summary>
        /// <param name="argb">String to convert.</param>
        /// <param name="separator">Char separator for a, r, g, b.</param>
        /// <returns>Color.</returns>
        public static Color ColorFromString(string argb, char separator)
        {
            string[] components = argb.Split(new char[] {separator});

            Color color = Color.White;

            try
            {
                color = Color.FromArgb(
                    int.Parse(components[0]),
                    int.Parse(components[1]),
                    int.Parse(components[2]),
                    int.Parse(components[3]));
            }
            catch
            {
                throw new ApplicationException();
            }

            return color;
        }

        /// <summary>
        /// Gets a string from a color.
        /// </summary>
        /// <param name="color">Color to convert.</param>
        /// <param name="separator">Char separator for a, r, g, b.</param>
        /// <returns>String that rapresents the color (format: a'separator'r'separator'g'separator'b).</returns>
        public static string StringFromColor(Color color, char separator)
        {
            string argb = color.A.ToString() + separator + color.R.ToString() + separator + color.G.ToString() + separator + color.B.ToString();

            return argb;
        }

        #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
Software Developer
Italy Italy
I am a biomedical engineer. I work in Genoa as software developer. I developed MFC ActiveX controls for industrial automation for 2 years and packages for Visual Studio 2005 for 1 year. Currently I'm working in .NET 3.5 in biomedical area.

Comments and Discussions