Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. 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.

Options

hexToRGB()

MichaelPascoeMichaelPascoe Member Posts: 1,724 PRO
edited October 2023 in FeatureScript

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!   cadsharp.com/featurescripts 💎

Comments

  • Options
    S1monS1mon Member Posts: 2,388 PRO
    edited October 2023
    I'm curious what sort of prompts you used. In the past when I've tried to coax FeatureScript out of ChatGPT 4, it's hallucinated a bunch of stuff. With the recent updates, hopefully it knows more FeatureScript.

    Also, I would point you to this request to include more color management stuff in the standard libraries for FeatureScript. Color.js seems like a good starting point.

  • Options
    MichaelPascoeMichaelPascoe Member Posts: 1,724 PRO

    Learn more about the Gospel of Christ  ( Here )

    CADSharp  -  We make custom features and integrated Onshape apps!   cadsharp.com/featurescripts 💎
  • Options
    ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,178
    I find the equations
    var r = (rr / 255) * 255;
    
    a bit suspicious :)  This line is not quite a noop because of floating point roundoff, but it doesn't do anything useful...
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • Options
    MichaelPascoeMichaelPascoe Member Posts: 1,724 PRO

    True, but some clean up was to be expected when asking a magic calculator for a function :D


    Learn more about the Gospel of Christ  ( Here )

    CADSharp  -  We make custom features and integrated Onshape apps!   cadsharp.com/featurescripts 💎
  • Options
    Evan_ReeseEvan_Reese Member Posts: 2,066 PRO
    Nice! here's the one I wrote for the Part Color feature a while back, and I feel kind of reassured that it's so similar, because I just kinda made it up:

    /**
     * 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;
    }

    Evan Reese / Principal and Industrial Designer with Ovyl
    Website: ovyl.io
Sign In or Register to comment.