Example usage for java.lang Math min

List of usage examples for java.lang Math min

Introduction

In this page you can find the example usage for java.lang Math min.

Prototype

@HotSpotIntrinsicCandidate
public static double min(double a, double b) 

Source Link

Document

Returns the smaller of two double values.

Usage

From source file:Main.java

/**
 * Convert CYMK color to RGB color.//from  w  ww . j a v a 2s. c  o  m
 * This method doesn't check f cmyk is not null or have 4 elements in array.
 *
 * @param cmyk target CYMK color. Each value should be between 0.0f to 1.0f,
 *             and should be set in this order: cyan, magenta, yellow, black.
 * @return ARGB color. Alpha is fixed value (255).
 */
public static int rgbFromCmyk(float[] cmyk) {
    float cyan = cmyk[0];
    float magenta = cmyk[1];
    float yellow = cmyk[2];
    float black = cmyk[3];
    int red = (int) ((1.0f - Math.min(1.0f, cyan * (1.0f - black) + black)) * 255);
    int green = (int) ((1.0f - Math.min(1.0f, magenta * (1.0f - black) + black)) * 255);
    int blue = (int) ((1.0f - Math.min(1.0f, yellow * (1.0f - black) + black)) * 255);
    return ((0xff & red) << 16) + ((0xff & green) << 8) + (0xff & blue);
}

From source file:Main.java

/**
 * Creates an animation to fade the dialog opacity from 0 to 1.
 *
 * @param dialog the dialog to fade in//from  w  w  w  .  jav a 2s .  c o m
 * @param delay the delay in ms before starting and between each change
 * @param incrementSize the increment size
 */
public static void fadeIn(final JDialog dialog, int delay, final float incrementSize) {
    final Timer timer = new Timer(delay, null);
    timer.setRepeats(true);
    timer.addActionListener(new ActionListener() {
        private float opacity = 0;

        @Override
        public void actionPerformed(ActionEvent e) {
            opacity += incrementSize;
            dialog.setOpacity(Math.min(opacity, 1)); // requires java 1.7
            if (opacity >= 1) {
                timer.stop();
            }
        }
    });

    dialog.setOpacity(0); // requires java 1.7
    timer.start();
    dialog.setVisible(true);
}

From source file:Main.java

public static Size getOptimalPreviewSize(Activity currentActivity, List<Size> sizes, double targetRatio) {
    // Use a very small tolerance because we want an exact match.
    final double ASPECT_TOLERANCE = 0.001;
    if (sizes == null)
        return null;
    Size optimalSize = null;//from  w w  w.  j a va 2s.  c  om
    double minDiff = Double.MAX_VALUE;
    // Because of bugs of overlay and layout, we sometimes will try to
    // layout the viewfinder in the portrait orientation and thus get the
    // wrong size of preview surface. When we change the preview size, the
    // new overlay will be created before the old one closed, which causes
    // an exception. For now, just get the screen size.
    Point point = getDefaultDisplaySize(currentActivity, new Point());
    int targetHeight = Math.min(point.x, point.y);
    // Try to find an size match aspect ratio and size
    for (Size size : sizes) {
        double ratio = (double) size.width / size.height;
        if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
            continue;
        if (Math.abs(size.height - targetHeight) < minDiff) {
            optimalSize = size;
            minDiff = Math.abs(size.height - targetHeight);
        }
    }
    // Cannot find the one match the aspect ratio. This should not happen.
    // Ignore the requirement.
    if (optimalSize == null) {
        Log.w(TAG, "No preview size match the aspect ratio");
        minDiff = Double.MAX_VALUE;
        for (Size size : sizes) {
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }
    }
    return optimalSize;
}

From source file:Main.java

/**Method to get round shaped bitmap. Mostly used for profile pictures
 *
 *     @param scaleBitmapImage/*from  w  ww .j av  a  2  s .c om*/
 *                      The source bitmap which has to converted to round shape
 *     @param context
 *                      The context
 *     @param targetWidthPixels
 *                      The width required for the target or returned  bitmap
 *     @param targetHeightPixels
 *                      The height required for the target or returned  bitmap
 *     @return
 *            round shaped bitmap
 */
public static Bitmap getRoundedShape(Bitmap scaleBitmapImage, Context context, int targetWidthPixels,
        int targetHeightPixels) {

    Bitmap targetBitmap = Bitmap.createBitmap(targetWidthPixels, targetHeightPixels, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(targetBitmap);
    Path path = new Path();
    path.addCircle(((float) targetWidthPixels - 1) / 2, ((float) targetHeightPixels - 1) / 2,
            (Math.min(((float) targetWidthPixels), ((float) targetHeightPixels)) / 2), Path.Direction.CCW);

    canvas.clipPath(path);
    Bitmap sourceBitmap = scaleBitmapImage;
    canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight()),
            new Rect(0, 0, targetWidthPixels, targetHeightPixels), null);
    return targetBitmap;
}

From source file:Main.java

/**
 * Return a float value within the range.
 * This is just a wrapper for Math.min() and Math.max().
 * This may be useful if you feel it confusing ("Which is min and which is max?").
 *
 * @param value    the target value//from   w  ww  .  ja va  2 s. c o m
 * @param minValue minimum value. If value is less than this, minValue will be returned
 * @param maxValue maximum value. If value is greater than this, maxValue will be returned
 * @return float value limited to the range
 */
public static float getFloat(final float value, final float minValue, final float maxValue) {
    return Math.min(maxValue, Math.max(minValue, value));
}

From source file:Main.java

/**
 * For devices with Jellybean or later, darkens the given color to ensure that white text is
 * clearly visible on top of it.  For devices prior to Jellybean, does nothing, as the
 * sync adapter handles the color change.
 *
 * @param color/*from   w ww  .j a  va2s.  c o m*/
 */
public static int getDisplayColorFromColor(int color) {
    float[] hsv = new float[3];
    Color.colorToHSV(color, hsv);
    hsv[1] = Math.min(hsv[1] * SATURATION_ADJUST, 1.0f);
    hsv[2] = hsv[2] * INTENSITY_ADJUST;
    return Color.HSVToColor(hsv);
}

From source file:Main.java

/**
 * Helper function that dump an array of bytes in hex form
 * /*  ww  w  .  jav  a  2  s . co m*/
 * @param buffer
 *            The bytes array to dump
 * @return A string representation of the array of bytes
 */
public static final String dumpBytes(byte[] buffer, int amountToDump) {
    if (buffer == null) {
        return "";
    }

    StringBuffer sb = new StringBuffer();

    amountToDump = Math.min(amountToDump, buffer.length);

    for (int i = 0; i < amountToDump; i++) {
        sb.append("0x").append((char) (HEX_CHAR[(buffer[i] & 0x00F0) >> 4]))
                .append((char) (HEX_CHAR[buffer[i] & 0x000F])).append(" ");
    }

    return sb.toString();
}

From source file:com.aqnote.app.wifianalyzer.vendor.model.VendorNameUtils.java

static String cleanVendorName(String name) {
    if (StringUtils.isBlank(name) || name.contains("<") || name.contains(">")) {
        return StringUtils.EMPTY;
    }//from w  ww  .j  a va  2  s.  c  om
    String result = name.replaceAll("[^a-zA-Z0-9]", " ").replaceAll(" +", " ").trim().toUpperCase();

    return result.substring(0, Math.min(result.length(), VENDOR_NAME_MAX));
}

From source file:Main.java

public static Bitmap resizeAndCropCenterExt(Bitmap bitmap, int size, boolean recycle) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    if (w == size && h == size)
        return bitmap;

    // scale the image so that the shorter side equals to the target;
    // the longer side will be center-cropped.
    float scale = (float) size / Math.min(w, h);

    int width = Math.round(scale * bitmap.getWidth());
    int height = Math.round(scale * bitmap.getHeight());
    if (width > height) {
        int largeSize = (int) (width <= size * 1.5 ? width : size * 1.5);
        Bitmap target = Bitmap.createBitmap(largeSize, size, getConfig(bitmap));
        Canvas canvas = new Canvas(target);
        canvas.translate((largeSize - width) / 2f, (size - height) / 2f);
        canvas.scale(scale, scale);//from w  ww . ja v a 2 s  .  co  m
        Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
        canvas.drawBitmap(bitmap, 0, 0, paint);
        if (recycle)
            bitmap.recycle();
        return target;
    } else {
        int largeSize = (int) (height <= size * 1.5 ? height : size * 1.5);
        Bitmap target = Bitmap.createBitmap(size, largeSize, getConfig(bitmap));
        Canvas canvas = new Canvas(target);
        canvas.translate((size - width) / 2f, (largeSize - height) / 2f);
        canvas.scale(scale, scale);
        Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
        canvas.drawBitmap(bitmap, 0, 0, paint);
        if (recycle)
            bitmap.recycle();
        return target;
    }
}

From source file:CompTest.java

public static Comparator stringComparator() {
    return new Comparator() {

        public int compare(Object o1, Object o2) {
            String s1 = (String) o1;
            String s2 = (String) o2;
            int len1 = s1.length();
            int len2 = s2.length();
            int n = Math.min(len1, len2);
            char v1[] = s1.toCharArray();
            char v2[] = s2.toCharArray();
            int pos = 0;

            while (n-- != 0) {
                char c1 = v1[pos];
                char c2 = v2[pos];
                if (c1 != c2) {
                    return c1 - c2;
                }/*from   www  .  j  av  a 2 s. c o  m*/
                pos++;
            }
            return len1 - len2;
        }
    };
}