Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
c# FromClarionColor (also now with ToClarionColor...)
// Update from skype discussion
// First, check to see if your input is a hex string or not
private static int GetClarionColorFromString(string line)
{
if (line.Contains("h") || line.Contains("H"))
{
char[] chars = { 'h', 'H' };
line = line.TrimEnd(chars);
return Int32.Parse(line, System.Globalization.NumberStyles.HexNumber);
}
else
{
return Int32.Parse(line);
}
}
// Then switch from GRB to RGB
public static int ConvertFromGBRToRGB(int clarionColor)
{
byte[] bytes = BitConverter.GetBytes(clarionColor);
Array.Reverse(bytes, 0, 3);
return BitConverter.ToInt32(bytes, 0);
}
// Older examples
//comp.lang.clarion question - "Converting from Clarion LONG color codes to HTML colors (e.g. from 32896 to #808000)"
//
private String ToClarionColor(Color color)
{
return string.Format("00{0:X2}{1:X2}{2:X2}h", color.B, color.G, color.R);
}
// Oh, obviously this is assuming that the clarion color is hex. Not an Int like in the subject but you get the idea right?
private Color FromClarionColor(string clarionColor)
{
if (clarionColor.Length == 7)
clarionColor = clarionColor.Substring(1, 6); // trim off the leading "00" or "0"
if (clarionColor.Length == 8)
clarionColor = clarionColor.Substring(2, 6); // trim off the leading "00" or "0"
if (clarionColor.Length == 6)
{
string clarionColorInHtml = clarionColor.Substring(4, 2) +
clarionColor.Substring(2, 2) +
clarionColor.Substring(0, 2);
return RgbColor.FromColor(ColorTranslator.FromHtml(@"#" + clarionColorInHtml)).ToColor();
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.