Java Integer Clamp clampRGB(int val)

Here you can find the source of clampRGB(int val)

Description

Clamps an integer value between 0 and 255

License

BSD License

Parameter

Parameter Description
val The value to clamp

Return

The clamped integer

Declaration

public static int clampRGB(int val) 

Method Source Code

//package com.java2s;
/**/*from w ww.j a v a 2  s  . c  o m*/
 * CamanJ - Java Image Manipulation
 * Ported from the CamanJS Javascript library
 *
 * Copyright 2011, Ryan LeFevre
 * Licensed under the new BSD License
 * See LICENSE for more info.
 * 
 * Project Home: http://github.com/meltingice/CamanJ
 */

public class Main {
    /**
     * Clamps an integer value between 0 and 255
     * 
     * @param val
     *            The value to clamp
     * @return The clamped integer
     */
    public static int clampRGB(int val) {
        if (val < 0) {
            return 0;
        } else if (val > 255) {
            return 255;
        }

        return val;
    }

    /**
     * Clamps an array of integers between 0 and 255. Useful when returning an
     * integer array from {@link CamanFilter#process(int[])}
     * 
     * @param rgb
     *            The array of integers to clamp
     * @return The array of clamped integers
     */
    public static int[] clampRGB(int[] rgb) {
        rgb[0] = clampRGB(rgb[0]);
        rgb[1] = clampRGB(rgb[1]);
        rgb[2] = clampRGB(rgb[2]);
        //rgb[3] = clampRGB(rgb[3]);

        return rgb;
    }

    /**
     * Clamps an array of doubles and returns an array of integers. The reason
     * it doesn't return an array of doubles is because this is designed to be
     * returned from {@link CamanFilter#process(int[])}, and the return must be
     * an int array. Doubles are converted to ints by simple casting, so the
     * decimal portion of the double is dropped without rounding.
     * 
     * @param drgb
     *            The array of doubles to clamp
     * @return The array of clamped integers
     */
    public static int[] clampRGB(double[] drgb) {
        int[] rgb = new int[4];
        rgb[0] = clampRGB((int) drgb[0]);
        rgb[1] = clampRGB((int) drgb[1]);
        rgb[2] = clampRGB((int) drgb[2]);
        //rgb[3] = clampRGB((int) drgb[3]);

        return rgb;
    }
}

Related

  1. CLAMPIS(int a, int b, int c)
  2. clampLoop(int v, int min, int max)
  3. clampMono(int value)
  4. clampNonNegative(int i, int a, int b)
  5. clampPower(int num)
  6. clampString(String string, int limit)
  7. clampTo_0_255(int i)
  8. clampToByte(int c)
  9. clampToByteSize(int value)