Example usage for java.lang Math max

List of usage examples for java.lang Math max

Introduction

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

Prototype

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

Source Link

Document

Returns the greater of two double values.

Usage

From source file:Main.java

public static Bitmap transform(Matrix scaler, Bitmap source, int targetWidth, int targetHeight, boolean scaleUp,
        boolean fitInScreen) {
    if (fitInScreen) {
        source = scaleTo(scaler, source, targetWidth, targetHeight, true);
    }/*www  .ja v a  2  s .  c  o  m*/
    int deltaX = source.getWidth() - targetWidth;
    int deltaY = source.getHeight() - targetHeight;
    if ((!scaleUp || fitInScreen) && (deltaX < 0 || deltaY < 0)) {
        Bitmap b2 = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b2);

        int deltaXHalf = Math.max(0, deltaX / 2);
        int deltaYHalf = Math.max(0, deltaY / 2);
        Rect src = new Rect(deltaXHalf, deltaYHalf, deltaXHalf + Math.min(targetWidth, source.getWidth()),
                deltaYHalf + Math.min(targetHeight, source.getHeight()));
        int dstX = (targetWidth - src.width()) / 2;
        int dstY = (targetHeight - src.height()) / 2;
        Rect dst = new Rect(dstX, dstY, targetWidth - dstX, targetHeight - dstY);
        c.drawBitmap(source, src, dst, null);
        source.recycle();
        return b2;
    }

    Bitmap b1 = scaleTo(scaler, source, targetWidth, targetHeight, false);

    int dx1 = Math.max(0, b1.getWidth() - targetWidth);
    int dy1 = Math.max(0, b1.getHeight() - targetHeight);

    Bitmap b2 = Bitmap.createBitmap(b1, dx1 / 2, dy1 / 2, targetWidth, targetHeight);
    b1.recycle();

    return b2;
}

From source file:Main.java

/**
 * Creates a new <code>Color</code> that is a darker version of this
 * <code>Color</code>. This method is the same implementation
 * java.awt.Color#darker is usind except it has a configurable factor.
 * The java.awt.Color default facotr is 0.7
 * @return  a new <code>Color</code> object that is 
 *                    a darker version of this <code>Color</code>.
 * @see        java.awt.Color#brighter/* w ww.  j  a  v a  2  s  . c om*/
 */
public static Color darkerColor(Color color, double factor) {
    return new Color(Math.max((int) (color.getRed() * factor), 0),
            Math.max((int) (color.getGreen() * factor), 0), Math.max((int) (color.getBlue() * factor), 0));
}

From source file:FloatCmp.java

/** Compare two doubles, using default epsilon */
public static boolean equals(double a, double b) {
    if (a == b)//from  w  w  w. j  av a 2s  .co  m
        return true;
    // If the difference is less than epsilon, treat as equal.
    return Math.abs(a - b) < EPSILON * Math.max(Math.abs(a), Math.abs(b));
}

From source file:Main.java

private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    int sampleSize = 1;
    int height = options.outHeight;
    int width = options.outWidth;
    if (height > reqHeight || width > reqWidth) {
        final int heightRatio;
        final int widthRatio;
        if (reqHeight == 0) {
            sampleSize = (int) Math.floor((float) width / (float) reqWidth);
        } else if (reqWidth == 0) {
            sampleSize = (int) Math.floor((float) height / (float) reqHeight);
        } else {//from   w ww . ja  v  a 2  s. com
            heightRatio = (int) Math.floor((float) height / (float) reqHeight);
            widthRatio = (int) Math.floor((float) width / (float) reqWidth);
            sampleSize = Math.max(heightRatio, widthRatio);
        }
    }
    return sampleSize;
}

From source file:Main.java

public static void setBestExposure(Camera.Parameters parameters, boolean lightOn) {
    int minExposure = parameters.getMinExposureCompensation();
    int maxExposure = parameters.getMaxExposureCompensation();
    float step = parameters.getExposureCompensationStep();
    if ((minExposure != 0 || maxExposure != 0) && step > 0.0f) {
        // Set low when light is on
        float targetCompensation = lightOn ? MIN_EXPOSURE_COMPENSATION : MAX_EXPOSURE_COMPENSATION;
        int compensationSteps = Math.round(targetCompensation / step);
        float actualCompensation = step * compensationSteps;
        // Clamp value:
        compensationSteps = Math.max(Math.min(compensationSteps, maxExposure), minExposure);
        if (parameters.getExposureCompensation() == compensationSteps) {
            Log.i(TAG,//  w ww.  jav a 2  s  . c o m
                    "Exposure compensation already set to " + compensationSteps + " / " + actualCompensation);
        } else {
            Log.i(TAG, "Setting exposure compensation to " + compensationSteps + " / " + actualCompensation);
            parameters.setExposureCompensation(compensationSteps);
        }
    } else {
        Log.i(TAG, "Camera does not support exposure compensation");
    }
}

From source file:Main.java

private static float calculateScaleRatio(int width, int height) {
    float widthRatio = ((float) width) / ((float) MAX_IMAGE_DIMENSION);
    float heightRatio = ((float) height) / ((float) MAX_IMAGE_DIMENSION);
    float maxRatio = Math.max(widthRatio, heightRatio);
    return maxRatio;
}

From source file:Main.java

/**
 * This helper method creates a 'nice' scrim or background protection for layering text over
 * an image. This non-linear scrim is less noticable than a linear or constant one.
 *
 * Borrowed from github.com/romannurik/muzei
 *
 * Creates an approximated cubic gradient using a multi-stop linear gradient. See
 * <a href="https://plus.google.com/+RomanNurik/posts/2QvHVFWrHZf">this post</a> for more
 * details./*from ww w. j  av a 2  s  .  co  m*/
 */
public static Drawable makeCubicGradientScrimDrawable(int baseColor, int numStops, int gravity) {
    numStops = Math.max(numStops, 2);

    PaintDrawable paintDrawable = new PaintDrawable();
    paintDrawable.setShape(new RectShape());

    final int[] stopColors = new int[numStops];

    int alpha = Color.alpha(baseColor);

    for (int i = 0; i < numStops; i++) {
        double x = i * 1f / (numStops - 1);
        double opacity = Math.max(0, Math.min(1, Math.pow(x, 3)));
        stopColors[i] = (baseColor & 0x00ffffff) | ((int) (alpha * opacity) << 24);
    }

    final float x0, x1, y0, y1;
    switch (gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
    case Gravity.LEFT:
        x0 = 1;
        x1 = 0;
        break;
    case Gravity.RIGHT:
        x0 = 0;
        x1 = 1;
        break;
    default:
        x0 = 0;
        x1 = 0;
        break;
    }
    switch (gravity & Gravity.VERTICAL_GRAVITY_MASK) {
    case Gravity.TOP:
        y0 = 1;
        y1 = 0;
        break;
    case Gravity.BOTTOM:
        y0 = 0;
        y1 = 1;
        break;
    default:
        y0 = 0;
        y1 = 0;
        break;
    }

    paintDrawable.setShaderFactory(new ShapeDrawable.ShaderFactory() {
        @Override
        public Shader resize(int width, int height) {
            LinearGradient linearGradient = new LinearGradient(width * x0, height * y0, width * x1, height * y1,
                    stopColors, null, Shader.TileMode.CLAMP);
            return linearGradient;
        }
    });

    return paintDrawable;
}

From source file:Main.java

/**
 * Returns the contrast ratio between {@code foreground} and {@code background}.
 * {@code background} must be opaque.// w w  w  .  j a v a 2 s .  co m
 * <p>
 * Formula defined
 * <a href="http://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef">here</a>.
 */
public static double calculateContrast(int foreground, int background) {
    if (Color.alpha(background) != 255) {
        throw new IllegalArgumentException("background can not be translucent");
    }
    if (Color.alpha(foreground) < 255) {
        // If the foreground is translucent, composite the foreground over the background
        foreground = compositeColors(foreground, background);
    }

    final double luminance1 = calculateLuminance(foreground) + 0.05;
    final double luminance2 = calculateLuminance(background) + 0.05;

    // Now return the lighter luminance divided by the darker luminance
    return Math.max(luminance1, luminance2) / Math.min(luminance1, luminance2);
}

From source file:Main.java

/**
 * Trims the transparent pixels from the given {@link BufferedImage} (returns a sub-image).
 *
 * @param source The source image./*w ww. j  a  v  a2 s .  com*/
 * @return A new, trimmed image, or the source image if no trim is performed.
 */
public static BufferedImage trimmedImage(BufferedImage source) {
    final int minAlpha = 1;
    final int srcWidth = source.getWidth();
    final int srcHeight = source.getHeight();
    Raster raster = source.getRaster();
    int l = srcWidth, t = srcHeight, r = 0, b = 0;

    int alpha, x, y;
    int[] pixel = new int[4];
    for (y = 0; y < srcHeight; y++) {
        for (x = 0; x < srcWidth; x++) {
            raster.getPixel(x, y, pixel);
            alpha = pixel[3];
            if (alpha >= minAlpha) {
                l = Math.min(x, l);
                t = Math.min(y, t);
                r = Math.max(x, r);
                b = Math.max(y, b);
            }
        }
    }

    if (l > r || t > b) {
        // No pixels, couldn't trim
        return source;
    }

    return source.getSubimage(l, t, r - l + 1, b - t + 1);
}

From source file:Main.java

/** Crops a line to the given x-coordinates.
 * @param line/*from  w  ww. j  a v  a2 s .c o m*/
 * @param startingXcoord
 * @param stoppingXcoord
 * @return the line cropped to the specified x-coordinates 
 */
public static Point2D[] cropLineX(Point2D[] line, double startingXcoord, double stoppingXcoord) {

    // ensure that the line has at least one point
    if (line.length < 1) {
        throw new IllegalArgumentException("The line must contain at least one point.");
    }

    int maxIdx = line.length - 1;

    // determine the actual starting and stopping coordinates
    double startX = Math.max(startingXcoord, line[0].getX());
    double stopX = Math.min(stoppingXcoord, line[maxIdx].getX());

    // determine the index of each starting and stopping point;
    // these are the indices of the first vertices inside the cutoff
    // from the original set of vertices.
    int startXidx = 0;
    while (startXidx < maxIdx && line[startXidx].getX() <= startX) {
        startXidx++;
    }
    int stopXidx = maxIdx;
    while (stopXidx > 0 && line[stopXidx].getX() >= stopX) {
        stopXidx--;
    }

    // determine the new starting and stopping points
    Point2D firstPt = interpolate(line[startXidx - 1], line[startXidx], startX);
    Point2D lastPt = interpolate(line[stopXidx], line[stopXidx + 1], stopX);

    // generate the new line
    Point2D[] croppedLine = new Point2D[stopXidx - startXidx + 3]; // leaving room for the 2 end points
    // copy all interior points verbatim from the original line
    for (int origIdx = startXidx, newIdx = 1; origIdx <= stopXidx; origIdx++, newIdx++) {
        croppedLine[newIdx] = line[origIdx];
    }
    // add in the new endpoints
    croppedLine[0] = firstPt;
    croppedLine[croppedLine.length - 1] = lastPt;

    return croppedLine;

}