hexToRGB()

MichaelPascoe
MichaelPascoe Member Posts: 2,960 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];
}


RENDERCAD - Photorealistic product rendering.
CADSharp - Professional CAD + software consulting.

▚▞▚▞▚▞▚▞▚

Comments

  • S1mon
    S1mon Member Posts: 4,618 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.

    Simon Gatrall | Product Development, Engineering, Design, Onshape | Ex- IDEO, PCH, Unagi, Carbon | LinkedIn

  • MichaelPascoe
    MichaelPascoe Member Posts: 2,960 PRO

    RENDERCAD - Photorealistic product rendering.
    CADSharp - Professional CAD + software consulting.

    ▚▞▚▞▚▞▚▞▚
  • ilya_baran
    ilya_baran Onshape Employees, Developers, HDM Posts: 1,276 image
    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
  • MichaelPascoe
    MichaelPascoe Member Posts: 2,960 PRO

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


    RENDERCAD - Photorealistic product rendering.
    CADSharp - Professional CAD + software consulting.

    ▚▞▚▞▚▞▚▞▚
  • EvanReese
    EvanReese Member, Mentor Posts: 3,095 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
    The Onsherpa | Reach peak Onshape productivity
    www.theonsherpa.com