Example usage for android.support.v4.graphics ColorUtils calculateLuminance

List of usage examples for android.support.v4.graphics ColorUtils calculateLuminance

Introduction

In this page you can find the example usage for android.support.v4.graphics ColorUtils calculateLuminance.

Prototype

@FloatRange(from = 0.0, to = 1.0)
public static double calculateLuminance(@ColorInt int color) 

Source Link

Document

Returns the luminance of a color as a float between 0.0 and 1.0 .

Usage

From source file:com.achep.acdisplay.ui.widgets.CircleView.java

private void setInnerColor(int color, boolean needsColorReset) {
    if (mInnerColor == (mInnerColor = color) && !needsColorReset)
        return;/*from w w  w .  ja  v  a2s  .c  o  m*/

    // Inverse the drawable if needed
    boolean isBright = ColorUtils.calculateLuminance(color) > 0.5;
    mDrawable.setColorFilter(isBright ? mInverseColorFilter : null);
}

From source file:com.mods.grx.settings.utils.Utils.java

public static int get_contrast_text_color(int bgcolor) {
    int textcolor;
    double luminance = ColorUtils.calculateLuminance(bgcolor);
    if (luminance > (double) 0.5)
        textcolor = 0xff222222;//from  w w w .  j a  v a2s  .co  m
    else
        textcolor = 0xffffffff;
    return textcolor;
}

From source file:com.jaredrummler.android.colorpicker.ColorPickerDialog.java

void createColorShades(@ColorInt final int color) {
    final int[] colorShades = getColorShades(color);

    if (shadesLayout.getChildCount() != 0) {
        for (int i = 0; i < shadesLayout.getChildCount(); i++) {
            FrameLayout layout = (FrameLayout) shadesLayout.getChildAt(i);
            final ColorPanelView cpv = (ColorPanelView) layout.findViewById(R.id.cpv_color_panel_view);
            ImageView iv = (ImageView) layout.findViewById(R.id.cpv_color_image_view);
            cpv.setColor(colorShades[i]);
            cpv.setTag(false);/*from   ww w.j  a  v  a 2s  .c  o  m*/
            iv.setImageDrawable(null);
        }
        return;
    }

    final int horizontalPadding = getResources().getDimensionPixelSize(R.dimen.cpv_item_horizontal_padding);

    for (final int colorShade : colorShades) {
        int layoutResId;
        if (colorShape == ColorShape.SQUARE) {
            layoutResId = R.layout.cpv_color_item_square;
        } else {
            layoutResId = R.layout.cpv_color_item_circle;
        }

        final View view = View.inflate(getActivity(), layoutResId, null);
        final ColorPanelView colorPanelView = (ColorPanelView) view.findViewById(R.id.cpv_color_panel_view);

        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) colorPanelView.getLayoutParams();
        params.leftMargin = params.rightMargin = horizontalPadding;
        colorPanelView.setLayoutParams(params);
        colorPanelView.setColor(colorShade);
        shadesLayout.addView(view);

        colorPanelView.post(new Runnable() {
            @Override
            public void run() {
                // The color is black when rotating the dialog. This is a dirty fix. WTF!?
                colorPanelView.setColor(colorShade);
            }
        });

        colorPanelView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getTag() instanceof Boolean && (Boolean) v.getTag()) {
                    colorPickerDialogListener.onColorSelected(dialogId, ColorPickerDialog.this.color);
                    dismiss();
                    return; // already selected
                }
                ColorPickerDialog.this.color = colorPanelView.getColor();
                adapter.selectNone();
                for (int i = 0; i < shadesLayout.getChildCount(); i++) {
                    FrameLayout layout = (FrameLayout) shadesLayout.getChildAt(i);
                    ColorPanelView cpv = (ColorPanelView) layout.findViewById(R.id.cpv_color_panel_view);
                    ImageView iv = (ImageView) layout.findViewById(R.id.cpv_color_image_view);
                    iv.setImageResource(cpv == v ? R.drawable.cpv_preset_checked : 0);
                    if (cpv == v && ColorUtils.calculateLuminance(cpv.getColor()) >= 0.65
                            || Color.alpha(cpv.getColor()) <= ALPHA_THRESHOLD) {
                        iv.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_IN);
                    } else {
                        iv.setColorFilter(null);
                    }
                    cpv.setTag(cpv == v);
                }
            }
        });
        colorPanelView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                colorPanelView.showHint();
                return true;
            }
        });
    }
}

From source file:com.jrummyapps.android.colorpicker.ColorPickerDialog.java

void createColorShades(@ColorInt final int color) {
    final int[] colorShades = getColorShades(color);

    if (shadesLayout.getChildCount() != 0) {
        for (int i = 0; i < shadesLayout.getChildCount(); i++) {
            FrameLayout layout = (FrameLayout) shadesLayout.getChildAt(i);
            final ColorPanelView cpv = layout.findViewById(R.id.cpv_color_panel_view);
            ImageView iv = layout.findViewById(R.id.cpv_color_image_view);
            cpv.setColor(colorShades[i]);
            cpv.setTag(false);/*from  w w w.ja va2s  . c o  m*/
            iv.setImageDrawable(null);
        }
        return;
    }

    final int horizontalPadding = getResources().getDimensionPixelSize(R.dimen.cpv_item_horizontal_padding);

    for (final int colorShade : colorShades) {
        int layoutResId;
        if (colorShape == ColorShape.SQUARE) {
            layoutResId = R.layout.cpv_color_item_square;
        } else {
            layoutResId = R.layout.cpv_color_item_circle;
        }

        final View view = View.inflate(getActivity(), layoutResId, null);
        final ColorPanelView colorPanelView = view.findViewById(R.id.cpv_color_panel_view);

        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) colorPanelView.getLayoutParams();
        params.leftMargin = params.rightMargin = horizontalPadding;
        colorPanelView.setLayoutParams(params);
        colorPanelView.setColor(colorShade);
        shadesLayout.addView(view);

        colorPanelView.post(new Runnable() {
            @Override
            public void run() {
                // The color is black when rotating the dialog. This is a dirty fix. WTF!?
                colorPanelView.setColor(colorShade);
            }
        });

        colorPanelView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getTag() instanceof Boolean && (Boolean) v.getTag()) {
                    colorPickerDialogListener.onColorSelected(dialogId, ColorPickerDialog.this.color);
                    dismiss();
                    return; // already selected
                }
                ColorPickerDialog.this.color = colorPanelView.getColor();
                adapter.selectNone();
                for (int i = 0; i < shadesLayout.getChildCount(); i++) {
                    FrameLayout layout = (FrameLayout) shadesLayout.getChildAt(i);
                    ColorPanelView cpv = layout.findViewById(R.id.cpv_color_panel_view);
                    ImageView iv = layout.findViewById(R.id.cpv_color_image_view);
                    iv.setImageResource(cpv == v ? R.drawable.cpv_preset_checked : 0);
                    if (cpv == v && ColorUtils.calculateLuminance(cpv.getColor()) >= 0.65
                            || Color.alpha(cpv.getColor()) <= ALPHA_THRESHOLD) {
                        iv.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_IN);
                    } else {
                        iv.setColorFilter(null);
                    }
                    cpv.setTag(cpv == v);
                }
            }
        });
        colorPanelView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                colorPanelView.showHint();
                return true;
            }
        });
    }
}

From source file:com.jaredrummler.android.colorpicker.ColorPickerDialog.java

private void setupTransparency() {
    int progress = 255 - Color.alpha(color);
    transparencySeekBar.setMax(255);/*w ww.j a v a2s. c om*/
    transparencySeekBar.setProgress(progress);
    int percentage = (int) ((double) progress * 100 / 255);
    transparencyPercText.setText(String.format(Locale.ENGLISH, "%d%%", percentage));
    transparencySeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            int percentage = (int) ((double) progress * 100 / 255);
            transparencyPercText.setText(String.format(Locale.ENGLISH, "%d%%", percentage));
            int alpha = 255 - progress;
            // update items in GridView:
            for (int i = 0; i < adapter.colors.length; i++) {
                int color = adapter.colors[i];
                int red = Color.red(color);
                int green = Color.green(color);
                int blue = Color.blue(color);
                adapter.colors[i] = Color.argb(alpha, red, green, blue);
            }
            adapter.notifyDataSetChanged();
            // update shades:
            for (int i = 0; i < shadesLayout.getChildCount(); i++) {
                FrameLayout layout = (FrameLayout) shadesLayout.getChildAt(i);
                ColorPanelView cpv = (ColorPanelView) layout.findViewById(R.id.cpv_color_panel_view);
                ImageView iv = (ImageView) layout.findViewById(R.id.cpv_color_image_view);
                if (layout.getTag() == null) {
                    // save the original border color
                    layout.setTag(cpv.getBorderColor());
                }
                int color = cpv.getColor();
                color = Color.argb(alpha, Color.red(color), Color.green(color), Color.blue(color));
                if (alpha <= ALPHA_THRESHOLD) {
                    cpv.setBorderColor(color | 0xFF000000);
                } else {
                    cpv.setBorderColor((int) layout.getTag());
                }
                if (cpv.getTag() != null && (Boolean) cpv.getTag()) {
                    // The alpha changed on the selected shaded color. Update the checkmark color filter.
                    if (alpha <= ALPHA_THRESHOLD) {
                        iv.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_IN);
                    } else {
                        if (ColorUtils.calculateLuminance(color) >= 0.65) {
                            iv.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_IN);
                        } else {
                            iv.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
                        }
                    }
                }
                cpv.setColor(color);
            }
            // update color:
            int red = Color.red(color);
            int green = Color.green(color);
            int blue = Color.blue(color);
            color = Color.argb(alpha, red, green, blue);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });
}

From source file:com.jrummyapps.android.colorpicker.ColorPickerDialog.java

private void setupTransparency() {
    int progress = 255 - Color.alpha(color);
    transparencySeekBar.setMax(255);/*from w  ww  .j a  v a2  s .  co m*/
    transparencySeekBar.setProgress(progress);
    int percentage = (int) ((double) progress * 100 / 255);
    transparencyPercText.setText(String.format(Locale.ENGLISH, "%d%%", percentage));
    transparencySeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            int percentage = (int) ((double) progress * 100 / 255);
            transparencyPercText.setText(String.format(Locale.ENGLISH, "%d%%", percentage));
            int alpha = 255 - progress;
            // update items in GridView:
            for (int i = 0; i < adapter.colors.length; i++) {
                int color = adapter.colors[i];
                int red = Color.red(color);
                int green = Color.green(color);
                int blue = Color.blue(color);
                adapter.colors[i] = Color.argb(alpha, red, green, blue);
            }
            adapter.notifyDataSetChanged();
            // update shades:
            for (int i = 0; i < shadesLayout.getChildCount(); i++) {
                FrameLayout layout = (FrameLayout) shadesLayout.getChildAt(i);
                ColorPanelView cpv = layout.findViewById(R.id.cpv_color_panel_view);
                ImageView iv = layout.findViewById(R.id.cpv_color_image_view);
                if (layout.getTag() == null) {
                    // save the original border color
                    layout.setTag(cpv.getBorderColor());
                }
                int color = cpv.getColor();
                color = Color.argb(alpha, Color.red(color), Color.green(color), Color.blue(color));
                if (alpha <= ALPHA_THRESHOLD) {
                    cpv.setBorderColor(color | 0xFF000000);
                } else {
                    cpv.setBorderColor((int) layout.getTag());
                }
                if (cpv.getTag() != null && (Boolean) cpv.getTag()) {
                    // The alpha changed on the selected shaded color. Update the checkmark color filter.
                    if (alpha <= ALPHA_THRESHOLD) {
                        iv.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_IN);
                    } else {
                        if (ColorUtils.calculateLuminance(color) >= 0.65) {
                            iv.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_IN);
                        } else {
                            iv.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
                        }
                    }
                }
                cpv.setColor(color);
            }
            // update color:
            int red = Color.red(color);
            int green = Color.green(color);
            int blue = Color.blue(color);
            color = Color.argb(alpha, red, green, blue);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });
}

From source file:com.heinrichreimersoftware.materialintro.app.IntroActivity.java

private void updateBackground() {
    @ColorInt/*from www  . ja  v  a2  s  . c  om*/
    int background;
    @ColorInt
    int backgroundNext;
    @ColorInt
    int backgroundDark;
    @ColorInt
    int backgroundDarkNext;

    if (position == getCount()) {
        background = Color.TRANSPARENT;
        backgroundNext = Color.TRANSPARENT;
        backgroundDark = Color.TRANSPARENT;
        backgroundDarkNext = Color.TRANSPARENT;
    } else {
        background = ContextCompat.getColor(IntroActivity.this, getBackground(position));
        backgroundNext = ContextCompat.getColor(IntroActivity.this,
                getBackground(Math.min(position + 1, getCount() - 1)));

        background = ColorUtils.setAlphaComponent(background, 0xFF);
        backgroundNext = ColorUtils.setAlphaComponent(backgroundNext, 0xFF);

        try {
            backgroundDark = ContextCompat.getColor(IntroActivity.this, getBackgroundDark(position));
        } catch (Resources.NotFoundException e) {
            backgroundDark = ContextCompat.getColor(IntroActivity.this, R.color.mi_status_bar_background);
        }
        try {
            backgroundDarkNext = ContextCompat.getColor(IntroActivity.this,
                    getBackgroundDark(Math.min(position + 1, getCount() - 1)));
        } catch (Resources.NotFoundException e) {
            backgroundDarkNext = ContextCompat.getColor(IntroActivity.this, R.color.mi_status_bar_background);
        }
    }

    if (position + positionOffset >= adapter.getCount() - 1) {
        backgroundNext = ColorUtils.setAlphaComponent(background, 0x00);
        backgroundDarkNext = ColorUtils.setAlphaComponent(backgroundDark, 0x00);
    }

    background = (Integer) evaluator.evaluate(positionOffset, background, backgroundNext);
    backgroundDark = (Integer) evaluator.evaluate(positionOffset, backgroundDark, backgroundDarkNext);

    miFrame.setBackgroundColor(background);

    float[] backgroundDarkHsv = new float[3];
    Color.colorToHSV(backgroundDark, backgroundDarkHsv);
    //Slightly darken the background color a bit for more contrast
    backgroundDarkHsv[2] *= 0.95;
    int backgroundDarker = Color.HSVToColor(backgroundDarkHsv);
    miPagerIndicator.setPageIndicatorColor(backgroundDarker);
    ViewCompat.setBackgroundTintList(miButtonNext, ColorStateList.valueOf(backgroundDarker));
    ViewCompat.setBackgroundTintList(miButtonBack, ColorStateList.valueOf(backgroundDarker));

    @ColorInt
    int backgroundButtonCta = buttonCtaTintMode == BUTTON_CTA_TINT_MODE_TEXT
            ? ContextCompat.getColor(this, android.R.color.white)
            : backgroundDarker;
    ViewCompat.setBackgroundTintList(miButtonCta.getChildAt(0), ColorStateList.valueOf(backgroundButtonCta));
    ViewCompat.setBackgroundTintList(miButtonCta.getChildAt(1), ColorStateList.valueOf(backgroundButtonCta));

    int iconColor;
    if (ColorUtils.calculateLuminance(backgroundDark) > 0.4) {
        //Light background
        iconColor = ContextCompat.getColor(this, R.color.mi_icon_color_light);
    } else {
        //Dark background
        iconColor = ContextCompat.getColor(this, R.color.mi_icon_color_dark);
    }
    miPagerIndicator.setCurrentPageIndicatorColor(iconColor);
    DrawableCompat.setTint(miButtonNext.getDrawable(), iconColor);
    DrawableCompat.setTint(miButtonBack.getDrawable(), iconColor);

    @ColorInt
    int textColorButtonCta = buttonCtaTintMode == BUTTON_CTA_TINT_MODE_TEXT ? backgroundDarker : iconColor;
    ((Button) miButtonCta.getChildAt(0)).setTextColor(textColorButtonCta);
    ((Button) miButtonCta.getChildAt(1)).setTextColor(textColorButtonCta);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().setStatusBarColor(backgroundDark);

        if (position == adapter.getCount()) {
            getWindow().setNavigationBarColor(Color.TRANSPARENT);
        } else if (position + positionOffset >= adapter.getCount() - 1) {
            TypedValue typedValue = new TypedValue();
            TypedArray a = obtainStyledAttributes(typedValue.data,
                    new int[] { android.R.attr.navigationBarColor });

            int defaultNavigationBarColor = a.getColor(0, Color.BLACK);

            a.recycle();

            int navigationBarColor = (Integer) evaluator.evaluate(positionOffset, defaultNavigationBarColor,
                    Color.TRANSPARENT);
            getWindow().setNavigationBarColor(navigationBarColor);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int systemUiVisibility = getWindow().getDecorView().getSystemUiVisibility();
            int flagLightStatusBar = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
            if (ColorUtils.calculateLuminance(backgroundDark) > 0.4) {
                //Light background
                systemUiVisibility |= flagLightStatusBar;
            } else {
                //Dark background
                systemUiVisibility &= ~flagLightStatusBar;
            }
            getWindow().getDecorView().setSystemUiVisibility(systemUiVisibility);
        }
    }
}