Android Open Source - wally Palette Request






From Project

Back to project page wally.

License

The source code is released under:

Apache License

If you think the Android project wally listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2014 Freddie (Musenkishi) Lust-Hed
 *//from  w w w .  j ava2  s  .co  m
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.musenkishi.wally.util;

import android.graphics.Color;
import android.support.v7.graphics.Palette;

import com.crashlytics.android.Crashlytics;
import com.musenkishi.wally.base.WallyApplication;
import com.musenkishi.wally.dataprovider.SharedPreferencesDataProvider;

/**
 * A class for storing and generating a requested color from a requested
 * {@link android.support.v7.graphics.Palette.Swatch}.
 *
 * Created by Freddie (Musenkishi) Lust-Hed on 14-10-21.
 */
public class PaletteRequest {

    public enum SwatchType {
        REGULAR_VIBRANT,
        REGULAR_MUTED,
        DARK_VIBRANT,
        DARK_MUTED,
        LIGHT_VIBRANT,
        LIGHT_MUTED
    }

    public enum SwatchColor {
        BACKGROUND,
        TEXT_BODY,
        TEXT_TITLE
    }

    private SwatchType swatchType;
    private SwatchColor swatchColor;

    public PaletteRequest(SwatchType swatchType, SwatchColor swatchColor) {
        this.swatchType = swatchType;
        this.swatchColor = swatchColor;
    }

    private SwatchType getSwatchType() {
        return swatchType;
    }

    private SwatchColor getSwatchColor() {
        return swatchColor;
    }

    /**
     * This method will return the requested color if it's available. If the requested
     * {@link android.support.v7.graphics.Palette.Swatch} is null, it will iterate over all
     * swatches in the palette and grab the first one that doesn't return null.
     *
     * @param palette A generated {@link android.support.v7.graphics.Palette} where
     *                colors are picked from
     * @return requested color in integer form, otherwise next best available color,
     *         and in worst case {@link android.graphics.Color}.GRAY.
     */
    public int getColor(Palette palette) { //Here be fugly code.
        boolean requestedSwatchUsed = true;
        try {
            switch (getSwatchType()) {
                case REGULAR_MUTED:
                    requestedSwatchUsed = palette.getMutedSwatch() != null;
                    if (requestedSwatchUsed) {
                        switch (getSwatchColor()) {
                            case BACKGROUND:
                                return palette.getMutedSwatch().getRgb();
                            case TEXT_BODY:
                                return palette.getMutedSwatch().getBodyTextColor();
                            case TEXT_TITLE:
                                return palette.getMutedSwatch().getTitleTextColor();
                        }
                    }
                    break;
                case DARK_MUTED:
                    requestedSwatchUsed = palette.getDarkMutedSwatch() != null;
                    if (requestedSwatchUsed) {
                        switch (getSwatchColor()) {
                            case BACKGROUND:
                                return palette.getDarkMutedSwatch().getRgb();
                            case TEXT_BODY:
                                return palette.getDarkMutedSwatch().getBodyTextColor();
                            case TEXT_TITLE:
                                return palette.getDarkMutedSwatch().getTitleTextColor();
                        }
                    }
                    break;
                case LIGHT_MUTED:
                    requestedSwatchUsed = palette.getLightMutedSwatch() != null;
                    if (requestedSwatchUsed) {
                        switch (getSwatchColor()) {
                            case BACKGROUND:
                                return palette.getLightMutedSwatch().getRgb();
                            case TEXT_BODY:
                                return palette.getLightMutedSwatch().getBodyTextColor();
                            case TEXT_TITLE:
                                return palette.getLightMutedSwatch().getTitleTextColor();
                        }
                    }
                    break;
                case REGULAR_VIBRANT:
                    requestedSwatchUsed = palette.getVibrantSwatch() != null;
                    if (requestedSwatchUsed) {
                        switch (getSwatchColor()) {
                            case BACKGROUND:
                                return palette.getVibrantSwatch().getRgb();
                            case TEXT_BODY:
                                return palette.getVibrantSwatch().getBodyTextColor();
                            case TEXT_TITLE:
                                return palette.getVibrantSwatch().getTitleTextColor();
                        }
                    }
                    break;
                case DARK_VIBRANT:
                    requestedSwatchUsed = palette.getDarkVibrantSwatch() != null;
                    if (requestedSwatchUsed) {
                        switch (getSwatchColor()) {
                            case BACKGROUND:
                                return palette.getDarkVibrantSwatch().getRgb();
                            case TEXT_BODY:
                                return palette.getDarkVibrantSwatch().getBodyTextColor();
                            case TEXT_TITLE:
                                return palette.getDarkVibrantSwatch().getTitleTextColor();
                        }
                    }
                    break;
                case LIGHT_VIBRANT:
                    requestedSwatchUsed = palette.getLightVibrantSwatch() != null;
                    if (requestedSwatchUsed) {
                        switch (getSwatchColor()) {
                            case BACKGROUND:
                                return palette.getLightVibrantSwatch().getRgb();
                            case TEXT_BODY:
                                return palette.getLightVibrantSwatch().getBodyTextColor();
                            case TEXT_TITLE:
                                return palette.getLightVibrantSwatch().getTitleTextColor();
                        }
                    }
                    break;
            }

            if (!requestedSwatchUsed) {
                for (Palette.Swatch swatch : palette.getSwatches()){
                    if (swatch != null) {
                        switch (getSwatchColor()) {
                            case BACKGROUND:
                                return swatch.getRgb();
                            case TEXT_BODY:
                                return swatch.getBodyTextColor();
                            case TEXT_TITLE:
                                return swatch.getTitleTextColor();
                        }
                    }
                }
            }
        } catch (IllegalArgumentException e) {
            //This can happen if a Color we're trying to get is translucent.
            if (WallyApplication.getDataProviderInstance()
                    .getSharedPreferencesDataProviderInstance()
                    .hasUserApprovedCrashLogging()
                    == SharedPreferencesDataProvider.CRASH_LOGGING_APPROVED) {
                Crashlytics.logException(e);
            }
        }
        return Color.GRAY;
    }

    /**
     * A method for fetching the requested {@link android.support.v7.graphics.Palette.Swatch}
     * if available. If it's not available, it will return the next available swatch in the
     * {@link android.support.v7.graphics.Palette}. In worst case null.
     */
    public static Palette.Swatch getBestSwatch(Palette palette, Palette.Swatch swatch) {
        if (swatch != null) {
            return swatch;
        } else {
            for (Palette.Swatch listSwatch : palette.getSwatches()){
                if (listSwatch != null) {
                    return listSwatch;
                }
            }
        }
        return null;
    }

}




Java Source Code List

com.musenkishi.wally.activities.ImageDetailsActivity.java
com.musenkishi.wally.activities.MainActivity.java
com.musenkishi.wally.adapters.RecyclerImagesAdapter.java
com.musenkishi.wally.adapters.RecyclerSavedImagesAdapter.java
com.musenkishi.wally.adapters.SmartFragmentPagerAdapter.java
com.musenkishi.wally.adapters.SmartFragmentStatePagerAdapter.java
com.musenkishi.wally.anim.BaseItemAnimator.java
com.musenkishi.wally.anim.ScaleInOutItemAnimator.java
com.musenkishi.wally.anim.interpolator.EaseInOutBezierInterpolator.java
com.musenkishi.wally.anim.interpolator.FastOutLinearInInterpolator.java
com.musenkishi.wally.anim.interpolator.FastOutSlowInInterpolator.java
com.musenkishi.wally.anim.interpolator.LinearOutSlowInInterpolator.java
com.musenkishi.wally.base.BaseActivity.java
com.musenkishi.wally.base.BaseFragment.java
com.musenkishi.wally.base.GridFragment.java
com.musenkishi.wally.base.WallyApplication.java
com.musenkishi.wally.dataprovider.DataProvider.java
com.musenkishi.wally.dataprovider.FileManager.java
com.musenkishi.wally.dataprovider.NetworkDataProvider.java
com.musenkishi.wally.dataprovider.SharedPreferencesDataProvider.java
com.musenkishi.wally.dataprovider.models.DataProviderError.java
com.musenkishi.wally.dataprovider.models.SaveImageRequest.java
com.musenkishi.wally.dataprovider.okhttp.OkHttpStreamFetcher.java
com.musenkishi.wally.dataprovider.okhttp.OkHttpUrlLoader.java
com.musenkishi.wally.dataprovider.util.Parser.java
com.musenkishi.wally.fragments.CustomResolutionDialogFragment.java
com.musenkishi.wally.fragments.FilterDialogFragment.java
com.musenkishi.wally.fragments.ImageZoomFragment.java
com.musenkishi.wally.fragments.LatestFragment.java
com.musenkishi.wally.fragments.MaterialDialogFragment.java
com.musenkishi.wally.fragments.RandomImagesFragment.java
com.musenkishi.wally.fragments.SavedImagesFragment.java
com.musenkishi.wally.fragments.SearchFragment.java
com.musenkishi.wally.fragments.ToplistFragment.java
com.musenkishi.wally.models.Author.java
com.musenkishi.wally.models.ExceptionReporter.java
com.musenkishi.wally.models.Filter.java
com.musenkishi.wally.models.ImagePage.java
com.musenkishi.wally.models.Image.java
com.musenkishi.wally.models.ListFilterGroup.java
com.musenkishi.wally.models.Rating.java
com.musenkishi.wally.models.SavedImageData.java
com.musenkishi.wally.models.Size.java
com.musenkishi.wally.models.Tag.java
com.musenkishi.wally.models.filters.FilterAspectRatioKeys.java
com.musenkishi.wally.models.filters.FilterBoardsKeys.java
com.musenkishi.wally.models.filters.FilterBoards.java
com.musenkishi.wally.models.filters.FilterGroup.java
com.musenkishi.wally.models.filters.FilterGroupsStructure.java
com.musenkishi.wally.models.filters.FilterPurityKeys.java
com.musenkishi.wally.models.filters.FilterPurity.java
com.musenkishi.wally.models.filters.FilterResOptKeys.java
com.musenkishi.wally.models.filters.FilterResOpt.java
com.musenkishi.wally.models.filters.FilterResolutionKeys.java
com.musenkishi.wally.models.filters.FilterTimeSpanKeys.java
com.musenkishi.wally.muzei.WallyArtSource.java
com.musenkishi.wally.notification.NotificationProvider.java
com.musenkishi.wally.observers.FileChangeReceiver.java
com.musenkishi.wally.observers.FiltersChangeReceiver.java
com.musenkishi.wally.util.Blur.java
com.musenkishi.wally.util.PaletteLoader.java
com.musenkishi.wally.util.PaletteRequest.java
com.musenkishi.wally.util.SparseBooleanArrayParcelable.java
com.musenkishi.wally.util.TextClickableSpan.java
com.musenkishi.wally.util.TextLinkBuilder.java
com.musenkishi.wally.util.TypefaceSpan.java
com.musenkishi.wally.views.AutoGridView.java
com.musenkishi.wally.views.GridRecyclerView.java
com.musenkishi.wally.views.ObservableScrollView.java
com.musenkishi.wally.views.TabBarView.java
com.musenkishi.wally.views.TabView.java
com.musenkishi.wally.views.swipeclearlayout.SwipeClearLayout.java
net.margaritov.preference.colorpicker.dialog.ColorPickerDialogFragment.java
net.margaritov.preference.colorpicker.drawable.AlphaPatternDrawable.java
net.margaritov.preference.colorpicker.preference.ColorPickerPreference.java
net.margaritov.preference.colorpicker.view.ColorPanelView.java
net.margaritov.preference.colorpicker.view.ColorPickerView.java
nl.codesoup.cubicbezier.CubicBezierInterpolator.java