Java Byte Clamp CLAMP(byte x, byte min, byte max)

Here you can find the source of CLAMP(byte x, byte min, byte max)

Description

CLAMP

License

Open Source License

Declaration

public static byte CLAMP(byte x, byte min, byte max) 

Method Source Code

//package com.java2s;
/*// www.ja  va  2 s.  c om
 * @(#)gl_util.java 0.3 06/11/20
 *
 * jGL 3-D graphics library for Java
 * Copyright (c) 1999-2006 Robin Bing-Yu Chen (robin@ntu.edu.tw)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version. the GNU Lesser
 * General Public License should be included with this distribution
 * in the file LICENSE.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

public class Main {
    public static byte CLAMP(byte x, byte min, byte max) {
        if (x < min)
            return min;
        if (x > max)
            return max;
        return x;
    }

    public static short CLAMP(short x, short min, short max) {
        if (x < min)
            return min;
        if (x > max)
            return max;
        return x;
    }

    public static int CLAMP(int x, int min, int max) {
        if (x < min)
            return min;
        if (x > max)
            return max;
        return x;
    }

    public static float CLAMP(float x, float min, float max) {
        if (x < min)
            return min;
        if (x > max)
            return max;
        return x;
    }

    public static float CLAMP(float x, double min, double max) {
        if (x < min)
            return (float) min;
        if (x > max)
            return (float) max;
        return x;
    }

    public static float CLAMP(double x, double min, double max) {
        // the CLAMP for double will return by float
        if (x < min)
            return (float) min;
        if (x > max)
            return (float) max;
        return (float) x;
    }
}

Related

  1. clamp(byte value, byte lower, byte upper)