paperjs / paper.js

The Swiss Army Knife of Vector Graphics Scripting – Scriptographer ported to JavaScript and the browser, using HTML5 Canvas. Created by @lehni & @puckey
http://paperjs.org
Other
14.45k stars 1.22k forks source link

Implement math operators on Color for simple color arithmetic. #183

Closed lehni closed 11 years ago

lehni commented 11 years ago

Allow things like mixing, averaging, etc. in a very simple way.

frederickk commented 11 years ago

i've added a number of simple calculation methods, perhaps if you let me know what you have in mind i can add additional calculation methods for colors it to my fork

lehni commented 11 years ago

Hi Frederick,

I just found it, looking good!

https://github.com/frederickk/paper.js/blob/master/src/style/Color.js

I wasn't aware of all your extensions to Paper.js. Maybe we should discuss these a bit and see which ones could make it into core?

And in order for you to avoid having to merge all the time when we do refactoring (of which a lot has happened in the past), you could write these as extensions rather than a fork:

paper.Color.inject({
    add: function(color) {
        return new Color(
            this.red + point.red,
            this.green + point.green,
            this.blue + point.blue,
            this.alpha + point.alpha
        );
    }
});

Just a thought!

I will move the ones over that I think are good, and will perhaps change the code a bit. I shall also add you to the contributors list then.

lehni commented 11 years ago

Just realizing, the code will need more work though, since this only works for the rgb type. We should work with components arrays instead. Will look into that.

frederickk commented 11 years ago

i wanted to test my additions before submitting a pull request, but maybe i can just submit it and together we can possibly work on changes. let me know what you think is best.

the "library" (https://github.com/frederickk/frederickkPaper) i built was a series of extensions exactly as you've described, here... would that a better way of proposing additions for you to implement?

so a component version would look like (pseudo code):

    add: function(color) {
        for( var i=0; i<this.comps.length; i++ ) {
            this.comps[i] + color.comps[i];
        }
        return this;
    }
lehni commented 11 years ago

Implemented, with tests: https://github.com/paperjs/paper.js/blob/d369984196b20b72f3582d6e4bccd98fc804e0a5/src/style/Color.js#L1047

lehni commented 11 years ago

Oops, I didn't see you posted in the meantime. The code is more abstract, as it can produce the math operators in a loop. It also performs some extra magic so that color.multiply(4) actually works, and 4 is not clamped down to 1 before it's multiplied...