You are reading a single comment by @dwallersv and its replies. Click here to read the full conversation.
  • I'm trying to make a "class" called Color that is a 4-element Uint8Array. The point here is compactness -- I'm trying to minimize the memory footprint, which still having my own "object" with lots of goodness in terms of "methods" on an instance of Color (things like accessing the color as an RGB or HSB 8/8/8 bit triple in 3 of the 4 elements -- the 4th (actually first) specifies the format of the three color values).

    What I can't figure out is how to call Uint8Array from my Color constructor to return an array as a Color class object that can be indexed and behave like a regular Uint8Array, while responding to my custom "methods" in the prototype. So it can be used like this:

    c = new Color([0,100,100,100]); //returns a Uint8Array of [0,100,100,100] that is type Color
    /* invert color */
    c[1] = 255 - c[1];
    c[2] = 255 - c[2];
    c[3] = 255 - c[3];
    /* get HSB version of color from method attached to Color proto */
    c_hsb = c.RGBtoHSB();
    

    Here's how this is set up:

    function Color(c) {
        //what do I do here with 'c' to initialize and return a byte array initialized with 'c', that will return 'true' for 'instanceof(Color)'?
    }
    function proto(){};
    proto.prototype = Uint8Array.prototype;
    Color.prototype = new proto();
    
    a = new Color([255,255,255]);
    

    Of course, with nothing in the Color constructor bur with the Uint8Array prototype inherited, returns an empty object for the assignment to 'a' on the last line -- not surprising. If I do the following:

    function Color(c) {
        return new Uint8Array(c);
    }
    

    In the constructor, I get a Uint8Array type object back, not Color type object (which doesn't surprise me). So, none of the "methods" that are a part of the Color prototype are accessible (except through directly calling them as Color.prototype.method()).

    Any help?

About

Avatar for dwallersv @dwallersv started