Skip to content

Commit

Permalink
Cleaned up code a bit, and finally added licensing information. Also …
Browse files Browse the repository at this point in the history
…added a minified version, only 12.7KB!
  • Loading branch information
meltingice committed Jan 12, 2011
1 parent 973e344 commit 7f88b03
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 29 deletions.
10 changes: 10 additions & 0 deletions LICENSE
@@ -0,0 +1,10 @@
Copyright (c) 2010, Ryan LeFevre
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of Ryan LeFevre nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
73 changes: 44 additions & 29 deletions caman.js
@@ -1,3 +1,12 @@
/*!
* CamanJS - Image Manipulation Library
* http://camanjs.com/
*
* Copyright 2011, Ryan LeFevre
* Licensed under the new BSD License.
* See LICENSE for more info.
*/

(function () { (function () {


var forEach = Array.prototype.forEach, var forEach = Array.prototype.forEach,
Expand Down Expand Up @@ -766,22 +775,25 @@ Caman.manip.processNext = function (finishedFn) {
this.executeFilter(next.adjust, next.processFn); this.executeFilter(next.adjust, next.processFn);
}; };


// Basic library of effects/filters that is always loaded // Expose Caman to the world!
window.Caman = Caman;

/****************************************************************************
* Below are a basic library of filters that are always loaded with CamanJS *
****************************************************************************/
(function(Caman) { (function(Caman) {


Caman.manip.brightness = function(adjust) { Caman.manip.brightness = function(adjust) {


adjust = Math.floor(255 * (adjust / 100)); adjust = Math.floor(255 * (adjust / 100));


// Note that process has 2 args now
return this.process( adjust, function brightness(adjust, rgba) { return this.process( adjust, function brightness(adjust, rgba) {
// also pass the adjustment to the process callback rgba.r += adjust;
rgba.r += adjust; rgba.g += adjust;
rgba.g += adjust; rgba.b += adjust;
rgba.b += adjust;

return rgba;
return rgba; });
});
}; };


Caman.manip.saturation = function(adjust) { Caman.manip.saturation = function(adjust) {
Expand All @@ -794,11 +806,11 @@ Caman.manip.processNext = function (finishedFn) {
for (chan in rgba) { for (chan in rgba) {
if (rgba.hasOwnProperty(chan)) { if (rgba.hasOwnProperty(chan)) {
if (rgba[chan] === max || chan === "a") { if (rgba[chan] === max || chan === "a") {
continue; continue;
} }


diff = max - rgba[chan]; diff = max - rgba[chan];
rgba[chan] += Math.ceil(diff * (adjust / 100)); rgba[chan] += Math.ceil(diff * (adjust / 100));
} }
} }


Expand Down Expand Up @@ -833,13 +845,18 @@ Caman.manip.processNext = function (finishedFn) {
var chan; var chan;
for (chan in rgba) { for (chan in rgba) {
if (rgba.hasOwnProperty(chan)) { if (rgba.hasOwnProperty(chan)) {
// skip the alpha channel
if (chan === 'a') { continue; } if (chan === 'a') { continue; }

rgba[chan] /= 255; rgba[chan] /= 255;
rgba[chan] -= 0.5; rgba[chan] -= 0.5;
rgba[chan] *= adjust; rgba[chan] *= adjust;
rgba[chan] += 0.5; rgba[chan] += 0.5;
rgba[chan] *= 255; rgba[chan] *= 255;


// While uglier, I found that using if statements are
// faster than calling Math.max() and Math.min() to bound
// the numbers.
if (rgba[chan] > 255) { if (rgba[chan] > 255) {
rgba[chan] = 255; rgba[chan] = 255;
} else if (rgba[chan] < 0) { } else if (rgba[chan] < 0) {
Expand All @@ -853,18 +870,19 @@ Caman.manip.processNext = function (finishedFn) {
}; };


Caman.manip.hue = function(adjust) { Caman.manip.hue = function(adjust) {
var hsv, h; var hsv, h;

return this.process( adjust, function hue(adjust, rgba) { return this.process( adjust, function hue(adjust, rgba) {
hsv = Caman.rgb_to_hsv(rgba.r, rgba.g, rgba.b); hsv = Caman.rgb_to_hsv(rgba.r, rgba.g, rgba.b);
h = hsv.h * 100; h = hsv.h * 100;
h += Math.abs(adjust); h += Math.abs(adjust);
h = h % 100; h = h % 100;
h /= 100; h /= 100;
hsv.h = h; hsv.h = h;

rgb = Caman.hsv_to_rgb(hsv.h, hsv.s, hsv.v); rgb = Caman.hsv_to_rgb(hsv.h, hsv.s, hsv.v);

return {r: rgb.r, g: rgb.g, b: rgb.b, a: rgba.a}; return {r: rgb.r, g: rgb.g, b: rgb.b, a: rgba.a};
}); });
}; };


Expand All @@ -873,20 +891,19 @@ Caman.manip.processNext = function (finishedFn) {


if (arguments.length === 2) { if (arguments.length === 2) {
rgb = Caman.hex_to_rgb(arguments[0]); rgb = Caman.hex_to_rgb(arguments[0]);

level = arguments[1]; level = arguments[1];
} else if (arguments.length === 4) { } else if (arguments.length === 4) {
rgb = { rgb = {
r: arguments[0], r: arguments[0],
g: arguments[1], g: arguments[1],
b: arguments[2] b: arguments[2]
} };


level = arguments[3]; level = arguments[3];
} }


return this.process( [ level, rgb ], function colorize( adjust, rgba) { return this.process( [ level, rgb ], function colorize( adjust, rgba) {

// adjust[0] == level; adjust[1] == rgb;
rgba.r -= (rgba.r - adjust[1].r) * (adjust[0] / 100); rgba.r -= (rgba.r - adjust[1].r) * (adjust[0] / 100);
rgba.g -= (rgba.g - adjust[1].g) * (adjust[0] / 100); rgba.g -= (rgba.g - adjust[1].g) * (adjust[0] / 100);
rgba.b -= (rgba.b - adjust[1].b) * (adjust[0] / 100); rgba.b -= (rgba.b - adjust[1].b) * (adjust[0] / 100);
Expand Down Expand Up @@ -1045,6 +1062,4 @@ Caman.manip.processNext = function (finishedFn) {


}(Caman)); }(Caman));


window.Caman = Caman;

}()); }());
69 changes: 69 additions & 0 deletions caman.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7f88b03

Please sign in to comment.