Processing Ajax...

Title
Close Dialog

Message

Confirm
Close Dialog

Confirm
Close Dialog

Confirm
Close Dialog

Convert HTML Colour to RGBA

Description
This macro will convert an HTML colour code to RGBA.
Language
C#.net
Minimum Version
Created By
jerone
Contributors
-
Date Created
Oct 6, 2017
Date Last Modified
Oct 6, 2017

Macro Code

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;

// The 'text' parameter will contain the text from the:
//   - Current Clipboard when run by HotKey
//   - History Item when run from the History Menu
// The returned string will be:
//   - Placed directly on the Clipboard
//   - Ignored by ClipboardFusion if it is 'null'
public static class ClipboardFusionHelper
{
	public static string ProcessText(string text)
	{
        Color c = ParseColor(text);
        String htmlColor = "rgba(" + c.R + ", " + c.G + ", " + c.B + (c.A < 255 ? (c.A / 255.0).ToString("', '#.##;', '#.##;', '0", CultureInfo.InvariantCulture) : "") + ")";
		return htmlColor;
	}
	
    public static Color ParseColor(string htmlColor)
    {
        htmlColor = htmlColor.Trim();

        if (htmlColor.StartsWith("rgb")) // rgb or rgba
        {
            int left = htmlColor.IndexOf('(');
            int right = htmlColor.IndexOf(')');

            if (left < 0 || right < 0)
                throw new FormatException("rgba format error");
			
            string noBrackets = htmlColor.Substring(left + 1, right - left - 1);

            string[] parts = noBrackets.Split(',');

            int r = int.Parse(parts[0], CultureInfo.InvariantCulture);
            int g = int.Parse(parts[1], CultureInfo.InvariantCulture);
            int b = int.Parse(parts[2], CultureInfo.InvariantCulture);

            if (parts.Length == 3)
            {
                return Color.FromArgb(r, g, b);
            }
            else if (parts.Length == 4)
            {
                float a = float.Parse(parts[3], CultureInfo.InvariantCulture);
                return Color.FromArgb((int)Math.Round(a * 255), r, g, b);
            }
        }
		else if ((htmlColor[0] == '#') &&
                ((htmlColor.Length == 9) || (htmlColor.Length == 5)))
		{
 			if (htmlColor.Length == 9) 
			{
				string r = htmlColor.Substring(1, 2);
				string g = htmlColor.Substring(3, 2);
				string b = htmlColor.Substring(5, 2);
				string a = htmlColor.Substring(7, 2);
				
				return Color.FromArgb(Convert.ToInt32(a, 16),
									  Convert.ToInt32(r, 16),
									  Convert.ToInt32(g, 16),
									  Convert.ToInt32(b, 16));
			}
			else 
			{
				string r = Char.ToString(htmlColor[1]);
				string g = Char.ToString(htmlColor[2]);
				string b = Char.ToString(htmlColor[3]);
				string a = Char.ToString(htmlColor[4]);
				
				return Color.FromArgb(Convert.ToInt32(a + a, 16),
									  Convert.ToInt32(r + r, 16),
									  Convert.ToInt32(g + g, 16),
									  Convert.ToInt32(b + b, 16));
			}
		}

		return ColorTranslator.FromHtml(htmlColor);
    }
}