Java Code Examples for android.support.v4.graphics.ColorUtils#calculateLuminance()

The following examples show how to use android.support.v4.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: ColorPaletteAdapter.java    From IslamicLibraryAndroid with GNU General Public License v3.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 2
Source File: CircleView.java    From AcDisplay with GNU General Public License v2.0 5 votes vote down vote up
private void setInnerColor(int color, boolean needsColorReset) {
    if (mInnerColor == (mInnerColor = color) && !needsColorReset) return;

    // Inverse the drawable if needed
    boolean isBright = ColorUtils.calculateLuminance(color) > 0.5;
    mDrawable.setColorFilter(isBright ? mInverseColorFilter : null);
}
 
Example 3
Source File: SimpleSlide.java    From Puff-Android with MIT License 4 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    Bundle arguments = getArguments();

    View fragment = inflater.inflate(arguments.getInt(ARGUMENT_LAYOUT_RES,
            R.layout.fragment_simple_slide), container, false);

    TextView titleView = (TextView) fragment.findViewById(R.id.mi_title);
    TextView descriptionView = (TextView) fragment.findViewById(R.id.mi_description);
    buttonGrantPermissions = (Button) fragment.findViewById(R.id.mi_button_grant_permissions);
    ImageView imageView = (ImageView) fragment.findViewById(R.id.mi_image);

    CharSequence title = arguments.getCharSequence(ARGUMENT_TITLE);
    int titleRes = arguments.getInt(ARGUMENT_TITLE_RES);
    CharSequence description = arguments.getCharSequence(ARGUMENT_DESCRIPTION);
    int descriptionRes = arguments.getInt(ARGUMENT_DESCRIPTION_RES);
    int imageRes = arguments.getInt(ARGUMENT_IMAGE_RES);
    int backgroundRes = arguments.getInt(ARGUMENT_BACKGROUND_RES);
    int backgroundDarkRes = arguments.getInt(ARGUMENT_BACKGROUND_DARK_RES);

    //Title
    if (titleView != null) {
        if (title != null) {
            titleView.setText(title);
            titleView.setVisibility(View.VISIBLE);
        } else if (titleRes != 0) {
            titleView.setText(titleRes);
            titleView.setVisibility(View.VISIBLE);
        } else {
            titleView.setVisibility(View.GONE);
        }
    }

    //Description
    if (descriptionView != null) {
        if (description != null) {
            descriptionView.setText(description);
            descriptionView.setVisibility(View.VISIBLE);
        } else if (descriptionRes != 0) {
            descriptionView.setText(descriptionRes);
            descriptionView.setVisibility(View.VISIBLE);
        } else {
            descriptionView.setVisibility(View.GONE);
        }
    }

    //Image
    if (imageView != null) {
        if (imageRes != 0) {
            imageView.setImageResource(imageRes);
            imageView.setVisibility(View.VISIBLE);
        } else {
            imageView.setVisibility(View.GONE);
        }
    }

    if (backgroundDarkRes != 0 && buttonGrantPermissions != null) {
        ViewCompat.setBackgroundTintList(buttonGrantPermissions, ColorStateList.valueOf(
                ContextCompat.getColor(getContext(), backgroundDarkRes)));
    }
    
    @ColorInt
    int textColorPrimary;
    @ColorInt
    int textColorSecondary;

    if (backgroundRes != 0 &&
            ColorUtils.calculateLuminance(ContextCompat.getColor(getContext(), backgroundRes)) < 0.6) {
        //Use light text color
        textColorPrimary = ContextCompat.getColor(getContext(), R.color.mi_text_color_primary_dark);
        textColorSecondary = ContextCompat.getColor(getContext(), R.color.mi_text_color_secondary_dark);
    } else {
        //Use dark text color
        textColorPrimary = ContextCompat.getColor(getContext(), R.color.mi_text_color_primary_light);
        textColorSecondary = ContextCompat.getColor(getContext(), R.color.mi_text_color_secondary_light);
    }
    
    if (titleView != null) {
        titleView.setTextColor(textColorPrimary);
    }
    if (descriptionView != null) {
        descriptionView.setTextColor(textColorSecondary);
    }
    if (buttonGrantPermissions != null) {
        buttonGrantPermissions.setTextColor(textColorPrimary);
    }

    return fragment;
}
 
Example 4
Source File: SimpleSlide.java    From Prodigal with Apache License 2.0 4 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    Bundle arguments = getArguments();

    View fragment = inflater.inflate(arguments.getInt(ARGUMENT_LAYOUT_RES,
            R.layout.fragment_simple_slide), container, false);

    TextView titleView = (TextView) fragment.findViewById(R.id.mi_title);
    TextView descriptionView = (TextView) fragment.findViewById(R.id.mi_description);
    ImageView imageView = (ImageView) fragment.findViewById(R.id.mi_image);

    CharSequence title = arguments.getCharSequence(ARGUMENT_TITLE);
    int titleRes = arguments.getInt(ARGUMENT_TITLE_RES);
    CharSequence description = arguments.getCharSequence(ARGUMENT_DESCRIPTION);
    int descriptionRes = arguments.getInt(ARGUMENT_DESCRIPTION_RES);
    int imageRes = arguments.getInt(ARGUMENT_IMAGE_RES);
    int backgroundRes = arguments.getInt(ARGUMENT_BACKGROUND_RES);

    //Title
    if (titleView != null) {
        if (title != null) {
            titleView.setText(title);
            titleView.setVisibility(View.VISIBLE);
        } else if (titleRes != 0) {
            titleView.setText(titleRes);
            titleView.setVisibility(View.VISIBLE);
        } else {
            titleView.setVisibility(View.GONE);
        }
    }

    //Description
    if (descriptionView != null) {
        if (description != null) {
            descriptionView.setText(description);
            descriptionView.setVisibility(View.VISIBLE);
        } else if (descriptionRes != 0) {
            descriptionView.setText(descriptionRes);
            descriptionView.setVisibility(View.VISIBLE);
        } else {
            descriptionView.setVisibility(View.GONE);
        }
    }

    //Image
    if (imageView != null) {
        if (imageRes != 0) {
            imageView.setImageResource(imageRes);
            imageView.setVisibility(View.VISIBLE);
        } else {
            imageView.setVisibility(View.GONE);
        }
    }
    
    @ColorInt
    int textColorPrimary;
    @ColorInt
    int textColorSecondary;

    if (backgroundRes != 0 &&
            ColorUtils.calculateLuminance(ContextCompat.getColor(getContext(), backgroundRes)) < 0.6) {
        //Use light text color
        textColorPrimary = ContextCompat.getColor(getContext(), R.color.mi_text_color_primary_dark);
        textColorSecondary = ContextCompat.getColor(getContext(), R.color.mi_text_color_secondary_dark);
    } else {
        //Use dark text color
        textColorPrimary = ContextCompat.getColor(getContext(), R.color.mi_text_color_primary_light);
        textColorSecondary = ContextCompat.getColor(getContext(), R.color.mi_text_color_secondary_light);
    }
    
    if (titleView != null) {
        titleView.setTextColor(textColorPrimary);
    }
    if (descriptionView != null) {
        descriptionView.setTextColor(textColorSecondary);
    }

    return fragment;
}
 
Example 5
Source File: StatusUtils.java    From tysq-android with GNU General Public License v3.0 2 votes vote down vote up
/**
 * 判断颜色是不是亮色
 * @param color 想要设置的颜色
 * @return
 */
private static boolean isLightColor(@ColorInt int color){
    return ColorUtils.calculateLuminance(color) >= 0.5;
}