Color to String : Color « 2D Graphics « C# / C Sharp






Color to String

  

// This file is part of Code4Public (http://code4public.codeplex.com)
// Copyright 2011 Sina Iravanian - <sina@sinairv.com>
//
// This source file(s) may be redistributed, altered and customized
// by any means PROVIDING the authors name and all copyright
// notices remain intact.
// THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED. USE IT AT YOUR OWN RISK. THE AUTHOR ACCEPTS NO
// LIABILITY FOR ANY DATA DAMAGE/LOSS THAT THIS PRODUCT MAY CAUSE.
// ------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Code4Public.Utils
{
    public class ColorUtils
    {
        public static string ColorTo8CharString(Color color)
        {
            string str = String.Format("{0:X}", color.ToArgb());

            StringBuilder sb = new StringBuilder();
            for(int i = 0; i < 8 - str.Length; ++i)
            {
                sb.Append("0");
            }

            return sb.ToString() + str;
        }

        public static string ColorTo6CharString(Color color)
        {
            return ColorTo8CharString(color).Substring(2);
        }

        public static string ColorTo6CharHtmlString(Color color)
        {
            return "#" + ColorTo6CharString(color);
        }

        public static string ColorToRTFTableEntry(Color color)
        {
            return String.Format(@"\red{0}\green{1}\blue{2}", color.R, color.G, color.B);
        }

        public static bool TryParseColor(string strColor, out Color color)
        {
            color = Color.White;

            strColor = strColor.Trim();
            if (strColor.StartsWith("#")) // remove leading # if any
                strColor = strColor.Substring(1);
            
            int n;
            if (Int32.TryParse(strColor, System.Globalization.NumberStyles.HexNumber, null, out n))
            {
                color = Color.FromArgb(n);
                // sets the alpha value to 255
                color = Color.FromArgb(255, color.R, color.G, color.B);
                return true;
            }
            return false;
        }
    }
}

   
    
  








Related examples in the same category

1.Transparent colorTransparent color
2.List all known color in a systemList all known color in a system
3.Draw each of 100 cells with randomly chosen colorsDraw each of 100 cells with randomly chosen colors
4.Filled with the semi transparent and transparent colorFilled with the semi transparent and transparent color
5.All the colors that are supported in C# according
6.Color ChangerColor Changer
7.Known ColorsKnown Colors
8.Five yellow squares with different alpha values(Transparensy)
9.Create two color instances with different alpha components
10.Color.Chocolate
11.Use Color.FromArgb to create Color
12.Get all known color
13.Color representation in r,g,b with values [0.0 - 1.0].
14.Color representation in h,s,v with h = [0 - 360], s,v = [0.0 - 1.0].
15.Return a randomly-generated color
16.Color to RGB value
17.Get color outof String
18.Parse Color
19.Color To Rgb
20.Rgb To Color
21.Returns an HTML #XXXXXX format for a color.
22.Convert color name to Color object
23.Hex Color Util
24.Helper method to get a color based on it's string value
25.Converts a color string to a hex value string
26.Color to Hue,Lightness,Saturation value
27.Get Color From String
28.Convert String value To Color
29.Masks an image and returns a two color bitmap.
30.Convert Hex To Color
31.Create Color From HLS value
32.Parse Color with Color.FromArgb
33.Create Color Object from RGB value
34.Get Rainbow Colors
35.Convert RGB to HSL
36.HSL to RGB conversion.
37.Hsv To Rgb
38.Rgb Linear Interpolate
39.Get Luminance
40.To Grey scale
41.Heat Map background colour calculations