Colors { @ColorInt public static int withAlpha(@ColorInt final int color, @FloatRange(from = 0, to = 1) final float alphaRatio) { if (alphaRatio < 0.0 || alphaRatio > 1.0) { throw new IllegalArgumentException("Alpha ratio(" + alphaRatio + ") is out of [0.0, 1.0f] range."); } return withAlpha(color, (int) (alphaRatio * COMPONENT_VALUE_MAX)); } private  Colors(); @ColorInt static int withAlpha(@ColorInt final int color, @FloatRange(from = 0, to = 1) final float alphaRatio); @ColorInt static int withAlpha(@ColorInt final int color, @IntRange(from = COMPONENT_VALUE_MIN, to = COMPONENT_VALUE_MAX) final int alpha); @ColorInt static int blend(@ColorInt final int first, @ColorInt final int second); @SuppressWarnings("UnnecessaryLocalVariable") @ColorInt static int blend(@ColorInt final int first, @ColorInt final int second, @FloatRange(from = 0, to = 1) final float blendingRatio); @ColorInt static int brighter(@ColorInt final int color, @FloatRange(from = 0, to = 1) final float fraction); @ColorInt static int darker(@ColorInt final int color, @FloatRange(from = 0, to = 1) final float fraction); @ColorInt static int random(final boolean randomAlpha); @NonNull static String hexName(@ColorInt final int color); @NonNull static String hexName(@ColorInt final int color, final boolean withAlpha);  }
@Test public void testWithAlphaRatio() { final int color = Color.parseColor("#FF795548"); float ratio = 0.01f; while (ratio < 1.0f) { final int colorWithAlpha = Colors.withAlpha(color, ratio); assertThat(Color.alpha(colorWithAlpha), is((int) (ratio * 255))); assertThat(Color.red(colorWithAlpha), is(Color.red(color))); assertThat(Color.green(colorWithAlpha), is(Color.green(color))); assertThat(Color.blue(colorWithAlpha), is(Color.blue(color))); ratio += 0.01f; } }
@Test(expected = IllegalArgumentException.class) public void testWithAlphaRatioOutOfRangeOver() { Colors.withAlpha(0, 1.1f); }
@Test(expected = IllegalArgumentException.class) public void testWithAlphaRatioOutOfRangeUnder() { Colors.withAlpha(0, -0.1f); }
@Test(expected = IllegalArgumentException.class) public void testWithAlphaOutOfRangeOver() { Colors.withAlpha(0, 256); }
@Test(expected = IllegalArgumentException.class) public void testWithAlphaOutOfRangeUnder() { Colors.withAlpha(0, -1); }