Java Byte Clamp clamp(byte value, byte lower, byte upper)

Here you can find the source of clamp(byte value, byte lower, byte upper)

Description

Clamp a value to an interval.

License

Open Source License

Parameter

Parameter Description
value the input parameter.
lower the lower clamp threshold.
upper the upper clamp threshold.

Return

the clamped value.

Declaration

public static byte clamp(byte value, byte lower, byte upper) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from  w w  w.j  ava  2s . co m
     * Clamp a value to an interval.
     *
     * @param value
     *    the input parameter.
     * @param lower
     *    the lower clamp threshold.
     * @param upper
     *    the upper clamp threshold.
     * @return
     *  the clamped value.
     */
    public static byte clamp(byte value, byte lower, byte upper) {
        return (value < lower ? lower : (value > upper ? upper : value));
    }

    /**
     * Clamp a value to an interval.
     *
     * @param value
     *    the input parameter.
     * @param lower
     *    the lower clamp threshold.
     * @param upper
     *    the upper clamp threshold.
     * @return
     *  the clamped value.
     */
    public static short clamp(short value, short lower, short upper) {
        return (value < lower ? lower : (value > upper ? upper : value));
    }

    /**
     * Clamp a value to an interval.
     *
     * @param value
     *    the input parameter.
     * @param lower
     *    the lower clamp threshold.
     * @param upper
     *    the upper clamp threshold.
     * @return
     *  the clamped value.
     */
    public static int clamp(int value, int lower, int upper) {
        return (value < lower ? lower : (value > upper ? upper : value));
    }

    /**
     * Clamp a value to an interval.
     *
     * @param value
     *    the input parameter.
     * @param lower
     *    the lower clamp threshold.
     * @param upper
     *    the upper clamp threshold.
     * @return
     *  the clamped value.
     */
    public static long clamp(long value, long lower, long upper) {
        return (value < lower ? lower : (value > upper ? upper : value));
    }

    /**
     * Clamp a value to an interval.
     *
     * @param value
     *    the input parameter.
     * @param lower
     *    the lower clamp threshold.
     * @param upper
     *    the upper clamp threshold.
     * @return
     *  the clamped value.
     */
    public static float clamp(float value, float lower, float upper) {
        return (value < lower ? lower : (value > upper ? upper : value));
    }

    /**
     * Clamp a value to an interval.
     *
     * @param value
     *    the input parameter.
     * @param lower
     *    the lower clamp threshold.
     * @param upper
     *    the upper clamp threshold.
     * @return
     *  the clamped value.
     */
    public static double clamp(double value, double lower, double upper) {
        return (value < lower ? lower : (value > upper ? upper : value));
    }

    /**
     * Clamp a value to the interval <code>[0, 255]</code>.
     *
     * @param value
     *    the input parameter.
     * @return
     *  the clamped value.
     */
    public static byte clamp(byte value) {
        return (value < 0 ? 0 : (value > 255 ? (byte) 255 : value));
    }

    /**
     * Clamp a value to the interval <code>[0, 255]</code>.
     *
     * @param value
     *    the input parameter.
     * @return
     *  the clamped value.
     */
    public static short clamp(short value) {
        return (value < 0 ? 0 : (value > 255 ? (short) 255 : value));
    }

    /**
     * Clamp a value to the interval <code>[0, 255]</code>.
     *
     * @param value
     *    the input parameter.
     * @return
     *  the clamped value.
     */
    public static int clamp(int value) {
        return (value < 0 ? 0 : (value > 255 ? 255 : value));
    }

    /**
     * Clamp a value to the interval <code>[0, 255]</code>.
     *
     * @param value
     *    the input parameter.
     * @return
     *  the clamped value.
     */
    public static long clamp(long value) {
        return (value < 0 ? 0 : (value > 255 ? 255 : value));
    }

    /**
     * Clamp a value to the interval <code>[0, 255]</code>.
     *
     * @param value
     *    the input parameter.
     * @return
     *  the clamped value.
     */
    public static float clamp(float value) {
        return (value < 0 ? 0 : (value > 255 ? 255 : value));
    }

    /**
     * Clamp a value to the interval <code>[0, 255]</code>.
     *
     * @param value
     *    the input parameter.
     * @return
     *  the clamped value.
     */
    public static double clamp(double value) {
        return (value < 0 ? 0 : (value > 255 ? 255 : value));
    }
}

Related

  1. CLAMP(byte x, byte min, byte max)