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: 1,982 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
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 This line is not quite a noop because of floating point roundoff, but it doesn't do anything useful...
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 🔴