Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.
First time visiting? Here are some places to start:- Looking for a certain topic? Check out the categories filter or use Search (upper right).
- Need support? Ask a question to our Community Support category.
- Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
- Be respectful, on topic and if you see a problem, Flag it.
If you would like to contact our Community Manager personally, feel free to send a private message or an email.
hexToRGB()
MichaelPascoe
Member Posts: 2,604 PRO
Had GPT whip up a hexToRGB function since Onshape doesn't seem to have one. It needed some direction on which functions Onshape has like splitIntoCharacters() but over all it did great:
export function hexToRGB(hex)
{
// Remove the '#' symbol if present
// var hex = hexColor.replace("#", "");
// Split the hex color string into an array of characters
var hexArray = splitIntoCharacters(hex);
// Ensure that the array has 6 characters (RRGGBB)
if (size(hexArray) != 6)
{
// Handle invalid input here
return [0, 0, 0]; // Default to black or another appropriate value
}
// Define a map to convert hex characters to decimal values
var hexMap = {
'0' : 0, '1' : 1, '2' : 2, '3' : 3, '4' : 4, '5' : 5, '6' : 6, '7' : 7,
'8' : 8, '9' : 9, 'A' : 10, 'B' : 11, 'C' : 12, 'D' : 13, 'E' : 14, 'F' : 15,
'a' : 10, 'b' : 11, 'c' : 12, 'd' : 13, 'e' : 14, 'f' : 15
};
// Convert each character to its corresponding decimal value
var rr = 16 * hexMap[hexArray[0]] + hexMap[hexArray[1]];
var gg = 16 * hexMap[hexArray[2]] + hexMap[hexArray[3]];
var bb = 16 * hexMap[hexArray[4]] + hexMap[hexArray[5]];
// Normalize the values to the [0, 255] range
var r = (rr / 255) * 255;
var g = (gg / 255) * 255;
var b = (bb / 255) * 255;
return [r, g, b];
}Learn more about the Gospel of Christ ( Here )
CADSharp - We make custom features and integrated Onshape apps! Learn How to FeatureScript Here 🔴
Tagged:
1
Comments
Simon Gatrall | Product Development Specialist | Open For Work
Here is a link to the chat: https://chat.openai.com/share/b4dc2925-7eec-4fbf-bf4c-0601694c938f
Learn more about the Gospel of Christ ( Here )
CADSharp - We make custom features and integrated Onshape apps! Learn How to FeatureScript Here 🔴
a bit suspicious
True, but some clean up was to be expected when asking a magic calculator for a function
Learn more about the Gospel of Christ ( Here )
CADSharp - We make custom features and integrated Onshape apps! Learn How to FeatureScript Here 🔴
/** * Takes a string of 6 letters or numbers and returns the color specified by the hex code. */ export function hexToColor(hexCode is string) returns Color { var hexMap = { "0" : 0, "1" : 1, "2" : 2, "3" : 3, "4" : 4, "5" : 5, "6" : 6, "7" : 7, "8" : 8, "9" : 9, "a" : 10, "b" : 11, "c" : 12, "d" : 13, "e" : 14, "f" : 15, "A" : 10, "B" : 11, "C" : 12, "D" : 13, "E" : 14, "F" : 15 }; var splitString = splitIntoCharacters(hexCode); var hex1 = hexMap[splitString[0]]; var hex2 = hexMap[splitString[1]]; var hex3 = hexMap[splitString[2]]; var hex4 = hexMap[splitString[3]]; var hex5 = hexMap[splitString[4]]; var hex6 = hexMap[splitString[5]]; var red = (1 / 256 * (hex1 * 16 + hex2)); var green = (1 / 256 * (hex3 * 16 + hex4)); var blue = (1 / 256 * (hex5 * 16 + hex6)); var color = color(red, green, blue); return color; }The Onsherpa | Reach peak Onshape productivity
www.theonsherpa.com