List of usage examples for java.lang Math max
@HotSpotIntrinsicCandidate public static double max(double a, double b)
From source file:Main.java
/** * 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 w w w . ja va 2 s . c om */ 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 red = Color.red(baseColor); int green = Color.green(baseColor); int blue = Color.blue(baseColor); int alpha = Color.alpha(baseColor); for (int i = 0; i < numStops; i++) { float x = i * 1f / (numStops - 1); float opacity = (float) Math.max(0, Math.min(1, Math.pow(x, 3))); stopColors[i] = Color.argb((int) (alpha * opacity), red, green, blue); } 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
public static final Bitmap createBlurredBitmap(final Bitmap msentBitmap) { if (msentBitmap == null) { return null; }/*from w ww.j a v a 2 s . c om*/ Bitmap sentBitmap = resizeAndCropCenter(msentBitmap, 150); final Bitmap mBitmap = sentBitmap.copy(sentBitmap.getConfig(), true); final int w = mBitmap.getWidth(); final int h = mBitmap.getHeight(); final int[] pix = new int[w * h]; mBitmap.getPixels(pix, 0, w, 0, 0, w, h); final int wm = w - 1; final int hm = h - 1; final int wh = w * h; final int div = DEFAULT_BLUR_RADIUS + DEFAULT_BLUR_RADIUS + 1; final int r[] = new int[wh]; final int g[] = new int[wh]; final int b[] = new int[wh]; final int vmin[] = new int[Math.max(w, h)]; int rsum, gsum, bsum, x, y, i, p, yp, yi, yw; int divsum = div + 1 >> 1; divsum *= divsum; final int dv[] = new int[256 * divsum]; for (i = 0; i < 256 * divsum; i++) { dv[i] = i / divsum; } yw = yi = 0; final int[][] stack = new int[div][3]; int stackpointer; int stackstart; int[] sir; int rbs; final int r1 = DEFAULT_BLUR_RADIUS + 1; int routsum, goutsum, boutsum; int rinsum, ginsum, binsum; for (y = 0; y < h; y++) { rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0; for (i = -DEFAULT_BLUR_RADIUS; i <= DEFAULT_BLUR_RADIUS; i++) { p = pix[yi + Math.min(wm, Math.max(i, 0))]; sir = stack[i + DEFAULT_BLUR_RADIUS]; sir[0] = (p & 0xff0000) >> 16; sir[1] = (p & 0x00ff00) >> 8; sir[2] = p & 0x0000ff; rbs = r1 - Math.abs(i); rsum += sir[0] * rbs; gsum += sir[1] * rbs; bsum += sir[2] * rbs; if (i > 0) { rinsum += sir[0]; ginsum += sir[1]; binsum += sir[2]; } else { routsum += sir[0]; goutsum += sir[1]; boutsum += sir[2]; } } stackpointer = DEFAULT_BLUR_RADIUS; for (x = 0; x < w; x++) { r[yi] = dv[rsum]; g[yi] = dv[gsum]; b[yi] = dv[bsum]; rsum -= routsum; gsum -= goutsum; bsum -= boutsum; stackstart = stackpointer - DEFAULT_BLUR_RADIUS + div; sir = stack[stackstart % div]; routsum -= sir[0]; goutsum -= sir[1]; boutsum -= sir[2]; if (y == 0) { vmin[x] = Math.min(x + DEFAULT_BLUR_RADIUS + 1, wm); } p = pix[yw + vmin[x]]; sir[0] = (p & 0xff0000) >> 16; sir[1] = (p & 0x00ff00) >> 8; sir[2] = p & 0x0000ff; rinsum += sir[0]; ginsum += sir[1]; binsum += sir[2]; rsum += rinsum; gsum += ginsum; bsum += binsum; stackpointer = (stackpointer + 1) % div; sir = stack[stackpointer % div]; routsum += sir[0]; goutsum += sir[1]; boutsum += sir[2]; rinsum -= sir[0]; ginsum -= sir[1]; binsum -= sir[2]; yi++; } yw += w; } for (x = 0; x < w; x++) { rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0; yp = -DEFAULT_BLUR_RADIUS * w; for (i = -DEFAULT_BLUR_RADIUS; i <= DEFAULT_BLUR_RADIUS; i++) { yi = Math.max(0, yp) + x; sir = stack[i + DEFAULT_BLUR_RADIUS]; sir[0] = r[yi]; sir[1] = g[yi]; sir[2] = b[yi]; rbs = r1 - Math.abs(i); rsum += r[yi] * rbs; gsum += g[yi] * rbs; bsum += b[yi] * rbs; if (i > 0) { rinsum += sir[0]; ginsum += sir[1]; binsum += sir[2]; } else { routsum += sir[0]; goutsum += sir[1]; boutsum += sir[2]; } if (i < hm) { yp += w; } } yi = x; stackpointer = DEFAULT_BLUR_RADIUS; for (y = 0; y < h; y++) { pix[yi] = 0xff000000 | dv[rsum] << 16 | dv[gsum] << 8 | dv[bsum]; rsum -= routsum; gsum -= goutsum; bsum -= boutsum; stackstart = stackpointer - DEFAULT_BLUR_RADIUS + div; sir = stack[stackstart % div]; routsum -= sir[0]; goutsum -= sir[1]; boutsum -= sir[2]; if (x == 0) { vmin[y] = Math.min(y + r1, hm) * w; } p = x + vmin[y]; sir[0] = r[p]; sir[1] = g[p]; sir[2] = b[p]; rinsum += sir[0]; ginsum += sir[1]; binsum += sir[2]; rsum += rinsum; gsum += ginsum; bsum += binsum; stackpointer = (stackpointer + 1) % div; sir = stack[stackpointer]; routsum += sir[0]; goutsum += sir[1]; boutsum += sir[2]; rinsum -= sir[0]; ginsum -= sir[1]; binsum -= sir[2]; yi += w; } } mBitmap.setPixels(pix, 0, w, 0, 0, w, h); return mBitmap; }
From source file:Main.java
public static Bitmap loadMpoBitmapFromFile(File file, long offset, int maxWidth, int maxHeight) throws IOException { // First, decode the width and height of the image, so that we know how much to scale // it down when loading it into our ImageView (so we don't need to do a huge allocation). BitmapFactory.Options opts = new BitmapFactory.Options(); InputStream fs = null;// w ww. j ava 2 s. co m try { fs = new BufferedInputStream(new FileInputStream(file)); fs.skip(offset); opts.inJustDecodeBounds = true; BitmapFactory.decodeStream(fs, null, opts); } finally { if (fs != null) { try { fs.close(); } catch (IOException e) { // don't worry } } } int scale = 1; if (opts.outHeight > maxHeight || opts.outWidth > maxWidth) { scale = (int) Math.pow(2, (int) Math .round(Math.log(maxWidth / (double) Math.max(opts.outHeight, opts.outWidth)) / Math.log(0.5))); } if ((opts.outHeight <= 0) || (opts.outWidth <= 0)) { return null; } // Decode the image for real, but now with a sampleSize. // We have to reopen the file stream, and re-skip to the correct offset, since // FileInputStream doesn't support reset(). Bitmap bmp = null; fs = null; try { fs = new BufferedInputStream(new FileInputStream(file)); fs.skip(offset); BitmapFactory.Options opts2 = new BitmapFactory.Options(); opts2.inSampleSize = scale; bmp = BitmapFactory.decodeStream(fs, null, opts2); } finally { if (fs != null) { try { fs.close(); } catch (IOException e) { // don't worry } } } return bmp; }
From source file:com.centeractive.ws.common.ResourceUtils.java
private static String getFullPath(String resourcePath) { int linuxIndex = resourcePath.lastIndexOf("/"); int windowsIndex = resourcePath.lastIndexOf("\\"); int index = Math.max(linuxIndex, windowsIndex); if (index < 0) { return ""; }/*from w ww . j av a 2 s. c o m*/ return resourcePath.substring(0, index); }
From source file:UriUtils.java
/** * Returns the parent of the specified URI. * <p/>/*from w w w .ja va 2s. c o m*/ * For example, applying this method to the URI "<tt>http://www.site.com/articles/article.html</tt>" will * return the URI for "<tt>http://www.site.com/articles/</tt>". * * @param uri The URI for which to return the parent. * @return The parent of the specified URI. * @throws IllegalArgumentException <ul> <li>The URI cannot be null></li> <li>Can't resolve parent for the specified * URI.</li> </ul> */ public static URI getParent(final URI uri) throws IllegalArgumentException { if (uri == null) throw new IllegalArgumentException("The URI cannot be null."); final String path = uri.toString(); final int finalSeparator = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\')); final int extension = path.lastIndexOf('.'); if (extension > finalSeparator) try { // Extract all but final segment return new URI(path.substring(0, finalSeparator + 1)).normalize(); } catch (URISyntaxException e) { throw new IllegalArgumentException("Can't resolve parent for the specified URI.", e); } else return uri.resolve(up); }
From source file:com.github.steveash.jg2p.align.ProbTable.java
/** * Returns a set of all non-empty x,y pairs from a unioned with all non-empty x,y pairs from b * @param a//from ww w .ja v a2 s. co m * @param b * @return */ public static Set<Pair<String, String>> unionOfAllCells(ProbTable a, ProbTable b) { Set<Pair<String, String>> xys = Sets.newHashSetWithExpectedSize(Math.max(a.xyProb.size(), b.xyProb.size())); addAllPresent(a, xys); addAllPresent(b, xys); return xys; }
From source file:Main.java
/** * 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 ava 2s. c o m*/ */ public static Drawable makeCubicGradientScrimDrawable(int baseColor, int numStops, int gravity) { // Generate a cache key by hashing together the inputs, based on the method described in the Effective Java book int cacheKeyHash = baseColor; cacheKeyHash = 31 * cacheKeyHash + numStops; cacheKeyHash = 31 * cacheKeyHash + gravity; Drawable cachedGradient = cubicGradientScrimCache.get(cacheKeyHash); if (cachedGradient != null) { return cachedGradient; } numStops = Math.max(numStops, 2); PaintDrawable paintDrawable = new PaintDrawable(); paintDrawable.setShape(new RectShape()); final int[] stopColors = new int[numStops]; int red = Color.red(baseColor); int green = Color.green(baseColor); int blue = Color.blue(baseColor); int alpha = Color.alpha(baseColor); for (int i = 0; i < numStops; i++) { float x = i * 1f / (numStops - 1); float opacity = constrain(0, 1, (float) Math.pow(x, 3)); stopColors[i] = Color.argb((int) (alpha * opacity), red, green, blue); } 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) { return new LinearGradient(width * x0, height * y0, width * x1, height * y1, stopColors, null, Shader.TileMode.CLAMP); } }); cubicGradientScrimCache.put(cacheKeyHash, paintDrawable); return paintDrawable; }
From source file:Main.java
/** * Returns a {@link Collection} containing the exclusive disjunction * (symmetric difference) of the given {@link Collection}s. * <p>/*from ww w . j a va 2 s . com*/ * The cardinality of each element <i>e</i> in the returned * {@link Collection} will be equal to * <tt>max(cardinality(<i>e</i>,<i>a</i>),cardinality(<i>e</i>,<i>b</i>)) - min(cardinality(<i>e</i>,<i>a</i>),cardinality(<i>e</i>,<i>b</i>))</tt>. * <p> * This is equivalent to * <tt>{@link #subtract subtract}({@link #union union(a,b)},{@link #intersection intersection(a,b)})</tt> * or * <tt>{@link #union union}({@link #subtract subtract(a,b)},{@link #subtract subtract(b,a)})</tt>. * * @param a * the first collection, must not be null * @param b * the second collection, must not be null * @return the symmetric difference of the two collections */ public static <E> Collection<E> disjunction(final Collection<E> a, final Collection<E> b) { if (a == null || b == null) { return null; } List<E> list = getArrayList(); Map<E, Integer> mapa = getCardinalityMap(a); Map<E, Integer> mapb = getCardinalityMap(b); Set<E> elts = getHashSet(a); elts.addAll(b); for (E e : elts) { for (int i = 0, m = ((Math.max(getFreq(e, mapa), getFreq(e, mapb))) - (Math.min(getFreq(e, mapa), getFreq(e, mapb)))); i < m; i++) { list.add(e); } } return list; }
From source file:com.liveramp.cascading_ext.counters.Counter.java
private static String padSpaces(String str, int num) { int numSpaces = Math.max(0, num - str.length()); return str + StringUtils.repeat(" ", numSpaces); }
From source file:Main.java
/** * Convert RGB components to HSL (hue-saturation-lightness). * <ul>//from www. j a v a 2 s. c om * <li>outHsl[0] is Hue [0 .. 360)</li> * <li>outHsl[1] is Saturation [0...1]</li> * <li>outHsl[2] is Lightness [0...1]</li> * </ul> * * @param r red component value [0..255] * @param g green component value [0..255] * @param b blue component value [0..255] * @param outHsl 3-element array which holds the resulting HSL components */ public static void RGBToHSL(int r, int g, int b, float[] outHsl) { final float rf = r / 255f; final float gf = g / 255f; final float bf = b / 255f; final float max = Math.max(rf, Math.max(gf, bf)); final float min = Math.min(rf, Math.min(gf, bf)); final float deltaMaxMin = max - min; float h, s; float l = (max + min) / 2f; if (max == min) { // Monochromatic h = s = 0f; } else { if (max == rf) { h = ((gf - bf) / deltaMaxMin) % 6f; } else if (max == gf) { h = ((bf - rf) / deltaMaxMin) + 2f; } else { h = ((rf - gf) / deltaMaxMin) + 4f; } s = deltaMaxMin / (1f - Math.abs(2f * l - 1f)); } h = (h * 60f) % 360f; if (h < 0) { h += 360f; } outHsl[0] = constrain(h, 0f, 360f); outHsl[1] = constrain(s, 0f, 1f); outHsl[2] = constrain(l, 0f, 1f); }