Java Code Examples for androidx.core.graphics.ColorUtils#calculateLuminance()

The following examples show how to use androidx.core.graphics.ColorUtils#calculateLuminance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImageHelper.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
static Bitmap generateLetterIcon(String letter, float h, int size, Context context) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    float s = prefs.getInt("saturation", 100) / 100f;
    float v = prefs.getInt("brightness", 100) / 100f;
    float t = prefs.getInt("threshold", 50) / 100f;

    int bg = Color.HSVToColor(new float[]{h, s, v});
    double lum = ColorUtils.calculateLuminance(bg);

    Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawColor(bg);

    Paint paint = new Paint();
    paint.setColor(lum < t ? Color.WHITE : Color.BLACK);
    paint.setTextSize(size / 2f);
    paint.setTypeface(Typeface.DEFAULT_BOLD);

    canvas.drawText(letter,
            size / 2f - paint.measureText(letter) / 2,
            size / 2f - (paint.descent() + paint.ascent()) / 2, paint);

    return bitmap;
}
 
Example 2
Source File: ViewButtonColor.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
void setColor(Integer color) {
    if (color == null)
        color = Color.TRANSPARENT;
    this.color = color;

    GradientDrawable background = new GradientDrawable();
    background.setColor(color);
    background.setStroke(
            Helper.dp2pixels(getContext(), 1),
            Helper.resolveColor(getContext(), R.attr.colorSeparator));
    setBackground(background);

    if (color == Color.TRANSPARENT)
        setTextColor(Helper.resolveColor(getContext(), android.R.attr.textColorPrimary));
    else {
        double lum = ColorUtils.calculateLuminance(color);
        setTextColor(lum < 0.5 ? Color.WHITE : Color.BLACK);
    }
}
 
Example 3
Source File: InterfaceSettingsFragment.java    From revolution-irc with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void bind(CheckBoxSetting entry) {
    super.bind(entry);
    int[] overrideColors = ((ThemeOptionSetting) entry).overrideColors;
    int bgColor = StyledAttributesHelper.getColor(mCheckBox.getContext(),
            android.R.attr.colorBackground, 0);
    boolean darkBg = ColorUtils.calculateLuminance(bgColor) < 0.4;
    int overrideColor = overrideColors[0];
    for (int c : overrideColors) {
        if ((!darkBg && ColorUtils.calculateLuminance(c) < 0.75)
                || (darkBg && ColorUtils.calculateLuminance(c) > 0.25)) {
            overrideColor = c;
            break;
        }
    }
    if (overrideColor != 0)
        CompoundButtonCompat.setButtonTintList(mCheckBox,
                ColorStateList.valueOf(overrideColor));
    else
        CompoundButtonCompat.setButtonTintList(mCheckBox, mDefaultButtonTintList);
}
 
Example 4
Source File: Helper.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
static int adjustLuminance(int color, boolean dark, float min) {
    float lum = (float) ColorUtils.calculateLuminance(color);
    if (dark ? lum < min : lum > 1 - min)
        return ColorUtils.blendARGB(color,
                dark ? Color.WHITE : Color.BLACK,
                dark ? min - lum : lum - (1 - min));
    return color;
}
 
Example 5
Source File: ExpandableColorSetting.java    From revolution-irc with GNU General Public License v3.0 5 votes vote down vote up
protected void setPaletteBtnColor(int color) {
    ViewCompat.setBackgroundTintList(mPaletteBtn, ColorStateList.valueOf(color));
    int paletteIconColor = 0xFFFFFFFF;
    if (ColorUtils.calculateLuminance(color) >= 0.6)
        paletteIconColor = 0xFF000000;
    ImageViewCompat.setImageTintList(mPaletteIcon, ColorStateList.valueOf(paletteIconColor));
}
 
Example 6
Source File: SavedColorListAdapter.java    From revolution-irc with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void bind(int color) {
    super.bind(color);
    if (ColorUtils.calculateLuminance(color) < 0.6)
        ImageViewCompat.setImageTintList(mIcon, ColorStateList.valueOf(0xFFFFFFFF));
    else
        ImageViewCompat.setImageTintList(mIcon, ColorStateList.valueOf(0xFF000000));
}
 
Example 7
Source File: ColorPaletteAdapter.java    From ColorPicker with Apache License 2.0 5 votes vote down vote up
private void setColorFilter(int position) {
  if (position == selectedPosition && ColorUtils.calculateLuminance(colors[position]) >= 0.65) {
    imageView.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_IN);
  } else {
    imageView.setColorFilter(null);
  }
}
 
Example 8
Source File: StatisticActivity.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
public void recolor() {
    chartView.updateColors();
    chartView.invalidate();

    zoomedChartView.updateColors();
    zoomedChartView.invalidate();

    chartHeaderView.recolor();
    chartHeaderView.invalidate();

    if (data != null && data.chartData != null && data.chartData.lines != null && data.chartData.lines.size() > 1) {
        for (int i = 0; i < data.chartData.lines.size(); i++) {
            int color;
            if (data.chartData.lines.get(i).colorKey != null && Theme.hasThemeKey(data.chartData.lines.get(i).colorKey)) {
                color = Theme.getColor(data.chartData.lines.get(i).colorKey);
            } else {
                boolean darkBackground = ColorUtils.calculateLuminance(Theme.getColor(Theme.key_windowBackgroundWhite)) < 0.5f;
                color = darkBackground ? data.chartData.lines.get(i).colorDark :
                        data.chartData.lines.get(i).color;
            }
            if (i < checkBoxes.size()) {
                checkBoxes.get(i).recolor(color);
            }
        }
    }
    progressView.setProgressColor(Theme.getColor(Theme.key_progressCircle));
    errorTextView.setTextColor(Theme.getColor(Theme.key_dialogTextGray4));
}
 
Example 9
Source File: LineViewData.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
public void updateColors() {
    if (line.colorKey != null && Theme.hasThemeKey(line.colorKey)) {
        lineColor = Theme.getColor(line.colorKey);
    } else {
        int color = Theme.getColor(Theme.key_windowBackgroundWhite);
        boolean darkBackground = ColorUtils.calculateLuminance(color) < 0.5f;
        lineColor = darkBackground ? line.colorDark : line.color;
    }
    paint.setColor(lineColor);
    bottomLinePaint.setColor(lineColor);
    selectionPaint.setColor(lineColor);
}
 
Example 10
Source File: StatisticActivity.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
public void recolor() {
    chartView.updateColors();
    chartView.invalidate();

    zoomedChartView.updateColors();
    zoomedChartView.invalidate();

    chartHeaderView.recolor();
    chartHeaderView.invalidate();

    if (data != null && data.chartData != null && data.chartData.lines != null && data.chartData.lines.size() > 1) {
        for (int i = 0; i < data.chartData.lines.size(); i++) {
            int color;
            if (data.chartData.lines.get(i).colorKey != null && Theme.hasThemeKey(data.chartData.lines.get(i).colorKey)) {
                color = Theme.getColor(data.chartData.lines.get(i).colorKey);
            } else {
                boolean darkBackground = ColorUtils.calculateLuminance(Theme.getColor(Theme.key_windowBackgroundWhite)) < 0.5f;
                color = darkBackground ? data.chartData.lines.get(i).colorDark :
                        data.chartData.lines.get(i).color;
            }
            if (i < checkBoxes.size()) {
                checkBoxes.get(i).recolor(color);
            }
        }
    }
    progressView.setProgressColor(Theme.getColor(Theme.key_progressCircle));
    errorTextView.setTextColor(Theme.getColor(Theme.key_dialogTextGray4));
}
 
Example 11
Source File: LineViewData.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
public void updateColors() {
    if (line.colorKey != null && Theme.hasThemeKey(line.colorKey)) {
        lineColor = Theme.getColor(line.colorKey);
    } else {
        int color = Theme.getColor(Theme.key_windowBackgroundWhite);
        boolean darkBackground = ColorUtils.calculateLuminance(color) < 0.5f;
        lineColor = darkBackground ? line.colorDark : line.color;
    }
    paint.setColor(lineColor);
    bottomLinePaint.setColor(lineColor);
    selectionPaint.setColor(lineColor);
}
 
Example 12
Source File: ColorUtil.java    From HaoReader with GNU General Public License v3.0 4 votes vote down vote up
public static boolean isDark(@ColorInt int color) {
    return ColorUtils.calculateLuminance(color) < 0.5;
}
 
Example 13
Source File: WindowPreferencesManager.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
private static boolean isColorLight(@ColorInt int color) {
  return color != TRANSPARENT && ColorUtils.calculateLuminance(color) > 0.5;
}
 
Example 14
Source File: PaletteFlyoutMenu.java    From FlyoutMenus with MIT License 4 votes vote down vote up
public void setCurrentColor(@ColorInt int currentColor) {
    this.currentColor = currentColor;
    currentColorLuminance = ColorUtils.calculateLuminance(this.currentColor);
}