ssw1
(Ssw)
September 8, 2021, 9:41am
1
Hi All,
I was just wondering if there was a plug in where you pass in RGB’s and it returns a Hex Colour code you can then copy into Adobe programs (ie. photoshop etc). Just to save copying in 3 different values.
Example of the interface on Adobe colour which allows you to turn RGB to Hex.
https://color.adobe.com/create/color-wheel
maje90
(Riccardo Majewski)
September 8, 2021, 10:11am
2
A single line of c# can do that.
using System.Drawing;
private void RunScript(System.Drawing.Color Color, ref object Hex)
{
Hex = System.Drawing.ColorTranslator.ToHtml(Color);
}
c#_color-to-hex.gh (6.9 KB)
1 Like
ssw1
(Ssw)
September 13, 2021, 7:28pm
4
Amazing @maje90
Is there a way of doing the opposite, where I pass in a Hex value and it spits out the RGB?
Thankyou so much! Sam
I’m using the attached definition to convert hex to rgb.
hex_rgb.gh (10.0 KB)
If you also want the alpha value, I use the following:
Color to Hex using Regex
private void RunScript(System.Drawing.Color color, ref object hex)
{
hex = String.Format("#{0:X2}{1:X2}{2:X2}{3:X2}", color.R, color.G, color.B, color.A);
}
Hex to Color
(from stackoverflow )
using System.Drawing;
private void RunScript(string hex, ref object color)
{
hex = hex.TrimStart('#');
if (hex.Length == 6)
color = Color.FromArgb(255, // hardcoded opaque
int.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber),
int.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.HexNumber),
int.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber));
else
color = Color.FromArgb(
int.Parse(hex.Substring(6, 2), System.Globalization.NumberStyles.HexNumber),
int.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber),
int.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.HexNumber),
int.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber));
}
I’ve also have both of these in the Heron add-in.
-Brian
1 Like