MCPcopy Index your code
hub / github.com/clips/pattern / adjust

Function adjust

pattern/canvas.js:3099–3141  ·  view source on GitHub ↗
(img, options)

Source from the content-addressed store, hash-verified

3097
3098// function adjust(img, {hue:0, saturation:1.0, brightness:1.0, contrast:1.0})
3099function adjust(img, options) {
3100 /* Applies color adjustment filters to the image and returns the adjusted image.
3101 * - hue : the shift in hue (1.0 is 360 degrees on the color wheel).
3102 * - saturation : the intensity of the colors (0.0 is a grayscale image).
3103 * - brightness : the overall lightness or darkness (0.0 is a black image).
3104 * - contrast : the difference in brightness between regions.
3105 */
3106 var pixels = new Pixels(img);
3107 var adjust_hue = function(pixels, m) {
3108 pixels.map(function(p) {
3109 var hsb = _rgb2hsb(p[0]/255, p[1]/255, p[2]/255);
3110 var rgb = _hsb2rgb(Math.clamp((hsb[0] + m) % 1, 0, 1), hsb[1], hsb[2]);
3111 return [rgb[0]*255, rgb[1]*255, rgb[2]*255, p[3]];
3112 });
3113 }
3114 var adjust_saturation = function(pixels, m) {
3115 pixels.map(function(p) {
3116 var i = (0.3*p[0] + 0.59*p[1] + 0.11*p[2]) * (1-m);
3117 return [p[0]*m + i, p[1]*m + i, p[2]*m + i, p[3]];
3118 });
3119 }
3120 var adjust_brightness = function(pixels, m) {
3121 pixels.map(function(p) {
3122 return [p[0] + m, p[1] + m, p[2] + m, p[3]];
3123 });
3124 }
3125 var adjust_contrast = function(pixels, m) {
3126 pixels.map(function(p) {
3127 return [(p[0]-128)*m + 128, (p[1]-128)*m + 128, (p[2]-128)*m + 128, p[3]];
3128 });
3129 }
3130 var o = options || {};
3131 if (o.hue !== undefined)
3132 adjust_hue(pixels, o.hue);
3133 if (o.saturation !== undefined && o.saturation != 1)
3134 adjust_saturation(pixels, o.saturation);
3135 if (o.brightness !== undefined && o.brightness != 1)
3136 adjust_brightness(pixels, 255 * (o.brightness-1));
3137 if (o.contrast !== undefined && o.contrast != 1)
3138 adjust_contrast(pixels, o.contrast);
3139 pixels.update();
3140 return pixels.image();
3141}
3142
3143function desaturate(img) {
3144 /* Returns a grayscale version of the image.

Callers 1

desaturateFunction · 0.85

Calls 5

adjust_hueFunction · 0.85
adjust_saturationFunction · 0.85
adjust_brightnessFunction · 0.85
adjust_contrastFunction · 0.85
updateMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…