当前位置: 首页>>代码示例>>Java>>正文


Java ColorUtils.calculateContrast方法代码示例

本文整理汇总了Java中android.support.v4.graphics.ColorUtils.calculateContrast方法的典型用法代码示例。如果您正苦于以下问题:Java ColorUtils.calculateContrast方法的具体用法?Java ColorUtils.calculateContrast怎么用?Java ColorUtils.calculateContrast使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在android.support.v4.graphics.ColorUtils的用法示例。


在下文中一共展示了ColorUtils.calculateContrast方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: findContrastColor

import android.support.v4.graphics.ColorUtils; //导入方法依赖的package包/类
/**
 * Finds a suitable color such that there's enough contrast.
 *
 * @param color the color to start searching from.
 * @param other the color to ensure contrast against. Assumed to be lighter than {@param color}
 * @param findFg if true, we assume {@param color} is a foreground, otherwise a background.
 * @param minRatio the minimum contrast ratio required.
 * @return a color with the same hue as {@param color}, potentially darkened to meet the
 *          contrast ratio.
 *
 * This was copied from com.android.internal.util.NotificationColorUtil.
 */
private static int findContrastColor(int color, int other, boolean findFg, double minRatio) {
    int fg = findFg ? color : other;
    int bg = findFg ? other : color;
    if (ColorUtils.calculateContrast(fg, bg) >= minRatio) {
        return color;
    }

    double[] lab = new double[3];
    ColorUtils.colorToLAB(findFg ? fg : bg, lab);

    double low = 0, high = lab[0];
    final double a = lab[1], b = lab[2];
    for (int i = 0; i < 15 && high - low > 0.00001; i++) {
        final double l = (low + high) / 2;
        if (findFg) {
            fg = ColorUtils.LABToColor(l, a, b);
        } else {
            bg = ColorUtils.LABToColor(l, a, b);
        }
        if (ColorUtils.calculateContrast(fg, bg) > minRatio) {
            low = l;
        } else {
            high = l;
        }
    }
    return ColorUtils.LABToColor(low, a, b);
}
 
开发者ID:enricocid,项目名称:LaunchEnr,代码行数:40,代码来源:IconPalette.java

示例2: setMoreImagesColor

import android.support.v4.graphics.ColorUtils; //导入方法依赖的package包/类
/**
 * Sets the background color of the more images image
 * @param color the color to set the "more images" image to.
 */
public void setMoreImagesColor(int color){
    moreColor = color;
    double whiteContrast = ColorUtils.calculateContrast(Color.WHITE, moreColor);
    double blackContrast = ColorUtils.calculateContrast(Color.BLACK, moreColor);
    if(whiteContrast >= blackContrast){
        moreTextColor = Color.WHITE;
    }else{
        moreTextColor = Color.BLACK;
    }
    if(overflowView != null)
        overflowView.setTextColor(moreTextColor).setImageBackgroundColor(moreColor);
}
 
开发者ID:0lumide,项目名称:ImageGridLayout,代码行数:17,代码来源:ImageGridLayout.java

示例3: isLegible

import android.support.v4.graphics.ColorUtils; //导入方法依赖的package包/类
/** @return Whether the foreground color is legible on the background color. */
private static boolean isLegible(int foreground, int background) {
    background = ColorUtils.setAlphaComponent(background, 255);
    return ColorUtils.calculateContrast(foreground, background) >= MIN_CONTRAST_RATIO;
}
 
开发者ID:enricocid,项目名称:LaunchEnr,代码行数:6,代码来源:ExtractionUtils.java

示例4: getMaxContrastingColor

import android.support.v4.graphics.ColorUtils; //导入方法依赖的package包/类
public int getMaxContrastingColor(int target) {
    int color = 0;
    double contrast = 0;

    int x = 0;
    int y = 0;
    int z = 0;
    for (AdjacentColorCube cube : cubes) {
        if (cube != null) {
            int[] frequency = cube.getFrequency();
            int cubeX = 0;
            int cubeY = 0;
            int cubeZ = 0;
            for (int item : frequency) {
                if (item > 0) {
                    int rgb = Color.rgb(x + cubeX, y + cubeY, z + cubeZ);
                    double itemContrast = ColorUtils.calculateContrast(target, rgb);
                    if (itemContrast > contrast) {
                        color = rgb;
                        contrast = itemContrast;
                    }
                }
                cubeZ++;
                if (cubeZ >= AdjacentColorCube.DIMENSION) {
                    cubeZ = 0;
                    cubeY++;
                    if (cubeY >= AdjacentColorCube.DIMENSION) {
                        cubeY = 0;
                        cubeX++;
                    }
                }
            }
        }
        z += AdjacentColorCube.DIMENSION;
        if (z >= 256) {
            z = 0;
            y += AdjacentColorCube.DIMENSION;
            if (y >= 256) {
                y = 0;
                x += AdjacentColorCube.DIMENSION;
            }
        }
    }
    return color;
}
 
开发者ID:kalikov,项目名称:lighthouse,代码行数:46,代码来源:BitmapColors.java

示例5: MyTarget

import android.support.v4.graphics.ColorUtils; //导入方法依赖的package包/类
MyTarget(@NonNull Feed feed, @NonNull FeedColorsListener listener, final @ColorInt int backgroundColor) {
    this.feedId = feed.getId();
    this.listener = listener;
    contrastFilter = (rgb, hsl) -> ColorUtils.calculateContrast(rgb, backgroundColor) >= 4;
}
 
开发者ID:schaal,项目名称:ocreader,代码行数:6,代码来源:FaviconLoader.java

示例6: updateTint

import android.support.v4.graphics.ColorUtils; //导入方法依赖的package包/类
private static void updateTint(final Object nowPlayingFragment) throws Throwable {
    if (PREFS.getBoolean(Common.NP_TINT_ICONS, false)) {
        ViewGroup root = (ViewGroup) getObjectField(nowPlayingFragment, "mRootView");
        Object currentState = getObjectField(nowPlayingFragment, "mCurrentState");
        Class exStateClass = findClass(EXPANDING_STATE, nowPlayingFragment.getClass().getClassLoader());
        if (currentState == Enum.valueOf(exStateClass, "FULLY_EXPANDED")) {
            Object artPager = getObjectField(nowPlayingFragment, "mArtPager");
            ArrayList<?> mItems = (ArrayList<?>) getObjectField(artPager, "mItems");
            Object artPageFragment = null;
            for (int i = 0; i < mItems.size(); i++) {
                if (getIntField(mItems.get(i), "position") == (int) callMethod(artPager, "getCurrentItem")) {
                    artPageFragment = getObjectField(mItems.get(i), "object");
                    break;
                }
            }
            // Update color
            if (artPageFragment != null) {
                ImageView mAlbum = (ImageView) getObjectField(artPageFragment, "mAlbum");
                if (mAlbum.getDrawable() != null) {
                    Palette coverPalette = Palette.from(((BitmapDrawable) mAlbum.getDrawable()).getBitmap()).maximumColorCount(16).generate();
                    lastColor = coverPalette.getVibrantColor(Color.parseColor("#9E9E9E"));
                } else {
                    ((Handler) getObjectField(nowPlayingFragment, "mHandler")).postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                updateTint(nowPlayingFragment);
                            } catch (Throwable t) {
                                log(t);
                            }
                        }
                    }, 200);
                    return;
                }
            }
            if (isNewDesignEnabled()) {
                // Tint header bar & its items
                RelativeLayout customHeaderBar = (RelativeLayout) root.findViewById(modRes.getIdentifier("header_bar", "id", XGPM));
                if (customHeaderBar != null) {
                    customHeaderBar.setBackgroundColor(lastColor);
                    RelativeLayout wrapper = (RelativeLayout) customHeaderBar.getChildAt(0);
                    double contrastBlack = ColorUtils.calculateContrast(Color.BLACK, lastColor);
                    double contrastWhite = ColorUtils.calculateContrast(Color.WHITE, lastColor);
                    int imageColor = contrastBlack > contrastWhite ? Color.BLACK : Color.WHITE;
                    for (int j = 0; j < wrapper.getChildCount(); j++) {
                        View current = wrapper.getChildAt(j);
                        if (current instanceof ImageView && current.getId() != root.getResources().getIdentifier("play_pause_header", "id", GPM)) {
                            ((ImageView) current).setColorFilter(imageColor);
                        } else if (current instanceof FrameLayout && ((FrameLayout) current).getChildCount() > 0 &&
                                ((FrameLayout) current).getChildAt(0).getClass().getSimpleName().equals("MediaRouteButton")) {
                            ((Drawable) getObjectField(((FrameLayout) current).getChildAt(0), "mRemoteIndicator")).setColorFilter(imageColor, PorterDuff.Mode.SRC_ATOP);
                        }
                    }
                }
            } else {
                tintQueueButton(nowPlayingFragment);
            }
            // Tint all the rest
            for (Object pager : new Object[]{getObjectField(nowPlayingFragment, "mHeaderPager"), artPager}) {
                if (pager == null)
                    continue;
                for (Object edgeEffectCompat : new Object[]{getObjectField(pager, "mLeftEdge"), getObjectField(pager, "mRightEdge")}) {
                    ((Paint) getObjectField(getObjectField(edgeEffectCompat, "mEdgeEffect"), "mPaint")).setColor(lastColor);
                }
            }
            SeekBar seekBar = (SeekBar) getObjectField(nowPlayingFragment, "mProgress");
            LayerDrawable progress = (LayerDrawable) seekBar.getProgressDrawable().getCurrent();
            ClipDrawable clipProgress = (ClipDrawable) progress.findDrawableByLayerId(root.getResources().getIdentifier("progress", "id", "android"));
            clipProgress.setColorFilter(lastColor, PorterDuff.Mode.SRC_IN);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                AnimatedStateListDrawable thumb = (AnimatedStateListDrawable) seekBar.getThumb();
                thumb.setColorFilter(lastColor, PorterDuff.Mode.SRC_IN);
            }
            ImageButton playPause = (ImageButton) root.findViewById(root.getResources().getIdentifier("pause", "id", GPM));
            playPause.getBackground().setColorFilter(lastColor, PorterDuff.Mode.SRC_ATOP);
        }
    }
}
 
开发者ID:Maxr1998,项目名称:XGPM,代码行数:79,代码来源:NowPlaying.java

示例7: onLoadFinished

import android.support.v4.graphics.ColorUtils; //导入方法依赖的package包/类
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    ArrayList<WeekViewEvent> events = mWeekViewLoader.getEvents(loader.getId());
    events.clear();

    Account mAccount = AppUtils.getAccount(getContext());
    if (mAccount != null) {
        // fetch calendar colors
        final SparseIntArray mColors = new SparseIntArray();
        ContentResolver cr = getContext().getContentResolver();
        Cursor cursor = cr
                .query(CalendarContractWrapper.Calendars.CONTENT_URI(),
                        new String[]{
                                CalendarContractWrapper.Calendars._ID(),
                                CalendarContractWrapper.Calendars
                                        .CALENDAR_COLOR()}, null, null,
                        null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                int color = cursor.getInt(1);

                double lastContrast = ColorUtils.calculateContrast(color, mWeekView.getEventTextColor());
                //Log.d(TAG, String.format("color=%d %d %d, contrast=%f", Color.red(color), Color.green(color), Color.blue(color), lastContrast));

                while (lastContrast < 1.6) {
                    float[] hsv = new float[3];

                    Color.colorToHSV(color, hsv);
                    hsv[2] = Math.max(0f, hsv[2] - 0.033f); // darken
                    color = Color.HSVToColor(hsv);

                    lastContrast = ColorUtils.calculateContrast(color, mWeekView.getEventTextColor());
                    //Log.d(TAG, String.format("new color=%d %d %d, contrast=%f", Color.red(color), Color.green(color), Color.blue(color), lastContrast));

                    if (hsv[2] == 0) break;
                }

                mColors.put(cursor.getInt(0), color);
            }
            cursor.close();
        }


        if (data != null) {
            data.moveToFirst();
            data.moveToPrevious();
            while (data.moveToNext()) {

                boolean allDay = data.getInt(CalendarUtils.COLUMN_EVENT_ALL_DAY) == 1;

                Calendar startTime = Calendar.getInstance();
                if (allDay) {
                    startTime.setTimeZone(TimeZone.getTimeZone("UTC"));
                }
                startTime.setTimeInMillis(data.getLong(CalendarUtils.COLUMN_EVENT_DTSTART));

                Calendar endTime = Calendar.getInstance();
                if (allDay) {
                    endTime.setTimeZone(TimeZone.getTimeZone("UTC"));
                }
                endTime.setTimeInMillis(data.getLong(CalendarUtils.COLUMN_EVENT_DTEND));
                if (allDay && endTime.getTimeInMillis() % DateUtils.DAY_IN_MILLIS == 0) {
                    endTime.add(Calendar.MILLISECOND, -1);
                }

                WeekViewEvent event = new WeekViewEvent(data.getString(CalendarUtils.COLUMN_EVENT_ID),
                        data.getString(CalendarUtils.COLUMN_EVENT_TITLE),
                        data.getString(CalendarUtils.COLUMN_EVENT_LOCATION),
                        startTime,
                        endTime,
                        allDay);

                event.setColor(mColors.get(data.getInt(CalendarUtils.COLUMN_EVENT_CAL_ID)));

                events.add(event);
            }
        }
    }

    mWeekView.notifyDatasetChanged();
}
 
开发者ID:marunjar,项目名称:anewjkuapp,代码行数:82,代码来源:CalendarFragment2.java


注:本文中的android.support.v4.graphics.ColorUtils.calculateContrast方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。