List of usage examples for java.lang Math min
@HotSpotIntrinsicCandidate public static double min(double a, double b)
From source file:Main.java
/** * Converts the given string to a char-array of exactly the given length. * If the given string is short than the wanted length, then the array is * padded with trailing padding chars. If the string is longer, the last * character are cut off that the string has the wanted size. * * @param string The string to convert.//from w ww.ja v a 2 s . co m * @param exactArrayLength The length of the retirned char-array. * @param paddingChar The character to use for padding, if necessary. * @return The string as char array, padded or cut off, if necessary. * The array will have length exactArrayLength. null, if the * given string is null. * @preconditions (exactArrayLength >= 0) * @postconditions (result == null) * or (result <> null) * and (result.length == exactArrayLength) */ public static char[] toPaddedCharArray(String string, int exactArrayLength, char paddingChar) { char[] charArray = null; if (string != null) { int stringLength = string.length(); charArray = new char[exactArrayLength]; string.getChars(0, Math.min(stringLength, exactArrayLength), charArray, 0); for (int i = stringLength; i < charArray.length; i++) { // fill the rest of the array with padding char charArray[i] = paddingChar; } } return charArray; }
From source file:Main.java
/** * Split the list and return the stream with the resultant lists. * * @param list to be split//w w w. j av a 2s . c om * @param size of each list after splitting. * @param <T> type of list. * @return {@link Stream} of {@link List}s */ public static <T> Stream<List<T>> splitListStream(final List<T> list, final int size) { if (size <= 0) { throw new IllegalArgumentException("Invalid Split Size"); } final int listSize = list.size(); if (listSize == 0) { return Stream.empty(); } return IntStream.rangeClosed(0, (listSize - 1) / size) .mapToObj(n -> list.subList(n * size, Math.min((n + 1) * size, listSize))); }
From source file:Main.java
/** * Convert a color to a HSL array./*from w ww.jav a 2 s . co m*/ * * @param color The color to convert. * @param hsl A size-3 array to load with the HSL values. */ public static void colorToHsl(int color, float[] hsl) { float r = ((0x00ff0000 & color) >> 16) / 255.0F; float g = ((0x0000ff00 & color) >> 8) / 255.0F; float b = ((0x000000ff & color)) / 255.0F; float max = Math.max(Math.max(r, g), b); float min = Math.min(Math.min(r, g), b); float c = max - min; float hTemp = 0.0F; if (c == 0) { hTemp = 0; } else if (max == r) { hTemp = (float) (g - b) / c; if (hTemp < 0) hTemp += 6.0F; } else if (max == g) { hTemp = (float) (b - r) / c + 2.0F; } else if (max == b) { hTemp = (float) (r - g) / c + 4.0F; } float h = 60.0F * hTemp; float l = (max + min) * 0.5F; float s; if (c == 0) { s = 0.0F; } else { s = c / (1 - Math.abs(2.0F * l - 1.0F)); } hsl[0] = h; hsl[1] = s; hsl[2] = l; }
From source file:Main.java
/** * Calculates the contrast ratio of two order-independent luminance values. * <p>//from w w w.java 2 s . c o m * Derived from formula at http://gmazzocato.altervista.org/colorwheel/algo.php * * @param lum1 The first luminance value * @param lum2 The second luminance value * @return The contrast ratio of the luminance values * @throws IllegalArgumentException if luminance values are < 0 */ public static double calculateContrastRatio(double lum1, double lum2) { if ((lum1 < 0.0d) || (lum2 < 0.0d)) { throw new IllegalArgumentException("Luminance values may not be negative."); } return (Math.max(lum1, lum2) + 0.05d) / (Math.min(lum1, lum2) + 0.05d); }
From source file:Main.java
public static float calculatorRatioScreen(Activity activity, boolean portrait) { DisplayMetrics disp = new DisplayMetrics(); activity.getWindowManager().getDefaultDisplay().getMetrics(disp); int screenWidth, screenHeight; screenWidth = disp.widthPixels;//w w w .j a v a 2 s . co m screenHeight = disp.heightPixels; Log.d("", "ratio screenWidth " + screenWidth + " screenHeight " + screenHeight); int defaultWidth = 480; int defaultHeight = 320; if (portrait) { defaultWidth = DEFAULT_SCREEN_WIDTH_PORTRAIT; defaultHeight = DEFAULT_SCREEN_HEIGHT_PORTRAIT; } else { defaultWidth = DEFAULT_SCREEN_WIDTH_LANSCAPE; defaultHeight = DEFAULT_SCREEN_HEIGHT_LANSCAPE; } float ratioByWidth = (float) screenWidth / defaultWidth; float ratioByHeight = (float) screenHeight / defaultHeight; float ratioReturn = Math.min(ratioByWidth, ratioByHeight); // isAlReady = true; return ratioReturn; }
From source file:Main.java
/** * Linearly interpolates a between two one-byte channels. If the value is outside the range of * 0-255 (either because the an input is outside the range, or the ratio is outside the range of * 0.0-1.0), the results will be clamped to 0-255. * * @param a The start color, or the result if the {@code ratio} is 0.0. * @param b The end color, or the result if the {@code ratio} is 1.0. * @param ratio The ratio of {@code b}'s influence in the result, between 0.0 to 1.0. * @return The computed blend value./* w ww.j av a 2 s.c o m*/ */ private static int clampedLerp(int a, int b, float ratio) { int rawResult = a + (int) ((b - a) * ratio); return Math.max(Math.min(rawResult, 255), 0); }
From source file:Main.java
/** * Convert a RGB Color to it corresponding HSL values. * * @return an array containing the 3 HSL values. *//* w ww . j a v a 2 s .c om*/ public static float[] hslFromRGB(int color) { // Get RGB values in the range 0 - 1 float r = (float) Color.red(color) / 255.0f; float g = (float) Color.green(color) / 255.0f; float b = (float) Color.blue(color) / 255.0f; // Minimum and Maximum RGB values are used in the HSL calculations float min = Math.min(r, Math.min(g, b)); float max = Math.max(r, Math.max(g, b)); // Calculate the Hue float h = 0; if (max == min) h = 0; else if (max == r) h = ((60 * (g - b) / (max - min)) + 360) % 360; else if (max == g) h = (60 * (b - r) / (max - min)) + 120; else if (max == b) h = (60 * (r - g) / (max - min)) + 240; // Calculate the Luminance float l = (max + min) / 2; // Calculate the Saturation float s = 0; if (max == min) s = 0; else if (l <= .5f) s = (max - min) / (max + min); else s = (max - min) / (2 - max - min); return new float[] { h, s * 100, l * 100 }; }
From source file:Main.java
/** * cut a circle from a bitmap/*from w ww. ja v a 2 s .co m*/ * * @param picturePath complete path name for the file. * @param destCube the cube dimension of dest bitmap * @return the bitmap */ public static Bitmap cutCircleFromBitmap(String picturePath, int destCube) { BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inDither = false; //Disable Dithering mode opts.inPurgeable = true; //Tell to gc that whether it needs free memory, the Bitmap can be cleared opts.inInputShareable = true; //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future opts.inTempStorage = new byte[32 * 1024]; Bitmap bitmapImg = BitmapFactory.decodeFile(picturePath, opts); int cube = destCube; if (bitmapImg == null) return null; int smallest = Math.min(bitmapImg.getWidth(), bitmapImg.getHeight()); Bitmap output = Bitmap.createBitmap(cube, cube, Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); int left = (int) ((bitmapImg.getWidth() - smallest) * 0.5); int top = (int) ((bitmapImg.getHeight() - smallest) * 0.5); final Rect rectSrc = new Rect(left, top, left + smallest, top + smallest); final Rect rectDest = new Rect(0, 0, cube, cube); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawCircle(cube / 2, cube / 2, cube / 2, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmapImg, rectSrc, rectDest, paint); bitmapImg.recycle(); return output; }
From source file:Main.java
/** * This method calculates maximum size of both width and height of bitmap. * It is twice the device screen diagonal for default implementation (extra quality to zoom image). * Size cannot exceed max texture size.// w ww . j av a 2 s .c o m * * @return - max bitmap size in pixels. */ @SuppressWarnings({ "SuspiciousNameCombination", "deprecation" }) public static int calculateMaxBitmapSize(@NonNull Context context) { WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); Point size = new Point(); int width, height; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { display.getSize(size); width = size.x; height = size.y; } else { width = display.getWidth(); height = display.getHeight(); } int screenDiagonal = (int) Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2)); Canvas canvas = new Canvas(); return Math.min(screenDiagonal * 2, Math.min(canvas.getMaximumBitmapWidth(), canvas.getMaximumBitmapHeight())); }
From source file:Main.java
public static float blendColorDodge(float cS, float cB) { if (cS == 1) { return 1; } else {/*w w w. j av a 2s .co m*/ return Math.min(1, cB / (1 - cS)); } }