Java Code Examples for androidx.palette.graphics.Palette#Swatch

The following examples show how to use androidx.palette.graphics.Palette#Swatch . 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: AlbumItemViewModel.java    From Jockey with Apache License 2.0 6 votes vote down vote up
private Palette.Swatch pickSwatch(Palette palette) {
    if (palette.getVibrantSwatch() != null) {
        return palette.getVibrantSwatch();
    }
    if (palette.getLightVibrantSwatch() != null) {
        return palette.getLightVibrantSwatch();
    }
    if (palette.getDarkVibrantSwatch() != null) {
        return palette.getDarkVibrantSwatch();
    }
    if (palette.getLightMutedSwatch() != null) {
        return palette.getLightMutedSwatch();
    }
    if (palette.getDarkMutedSwatch() != null) {
        return palette.getDarkMutedSwatch();
    }
    return null;
}
 
Example 2
Source File: AlbumItemViewModel.java    From Jockey with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onResourceReady(GlideDrawable resource, File model,
                               Target<GlideDrawable> target, boolean isFromMemoryCache,
                               boolean isFirstResource) {

    if (sColorMap.containsKey(model)) {
        Palette.Swatch swatch = sColorMap.get(model);
        if (isFromMemoryCache) {
            setSwatch(swatch);
        } else {
            animateSwatch(swatch);
        }
    } else {
        generateSwatch(model, resource);
    }

    return false;
}
 
Example 3
Source File: PaletteActivity.java    From leafpicrevived with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onBindViewHolder(final PaletteActivity.PaletteAdapter.ViewHolder holder, final int position) {
    Palette.Swatch sw = swatches.get(position);
    holder.txtColor.setTextColor(sw.getTitleTextColor());
    holder.txtColor.setText(String.format("#%06X", (0xFFFFFF & sw.getRgb())));
    holder.itemBackground.setBackgroundColor(sw.getRgb());
}
 
Example 4
Source File: AlbumItemViewModel.java    From Jockey with Apache License 2.0 5 votes vote down vote up
private void animateSwatch(Palette.Swatch swatch) {
    if (swatch == null) {
        return;
    }

    animateColorValue(mBackgroundColor, swatch.getRgb());
    animateColorValue(mTitleTextColor, swatch.getTitleTextColor());
    animateColorValue(mArtistTextColor, swatch.getBodyTextColor());
}
 
Example 5
Source File: AlbumItemViewModel.java    From Jockey with Apache License 2.0 5 votes vote down vote up
private void setSwatch(Palette.Swatch swatch) {
    if (swatch == null) {
        return;
    }

    mBackgroundColor.set(swatch.getRgb());
    mTitleTextColor.set(swatch.getTitleTextColor());
    mArtistTextColor.set(swatch.getBodyTextColor());
}
 
Example 6
Source File: ColorUtil.java    From HaoReader with GNU General Public License v3.0 5 votes vote down vote up
public static Palette.Swatch getMostPopulousSwatch(Palette palette) {
    Palette.Swatch mostPopulous = null;
    if (palette != null) {
        for (Palette.Swatch swatch : palette.getSwatches()) {
            if (mostPopulous == null || swatch.getPopulation() > mostPopulous.getPopulation()) {
                mostPopulous = swatch;
            }
        }
    }
    return mostPopulous;
}
 
Example 7
Source File: ColorUtil.java    From HaoReader with GNU General Public License v3.0 5 votes vote down vote up
@Lightness
public static int isDark(Palette palette) {
    Palette.Swatch mostPopulous = getMostPopulousSwatch(palette);
    if (mostPopulous == null) {
        return LIGHTNESS_UNKNOWN;
    }
    if (isDark(mostPopulous.getRgb())) {
        return IS_DARK;
    } else {
        return IS_LIGHT;
    }
}
 
Example 8
Source File: ExtraUtils.java    From Bop with Apache License 2.0 5 votes vote down vote up
public static int getSwatchColor(Bitmap bitmap) {

        Palette p = Palette.from(bitmap).generate();
        Palette.Swatch vibrantSwatch = p.getVibrantSwatch();
        if (vibrantSwatch != null) {
            return vibrantSwatch.getBodyTextColor();
        }
        return Color.WHITE;
    }
 
Example 9
Source File: PaletteGeneratorTask.java    From MusicPlayer with GNU General Public License v3.0 4 votes vote down vote up
private boolean onGeneratedPalette(@NonNull Palette p) {
    int[] palette = new int[6];
    // access palette colors here
    Palette.Swatch psVibrant = p.getVibrantSwatch();
    Palette.Swatch psVibrantLight = p.getLightVibrantSwatch();
    Palette.Swatch psVibrantDark = p.getDarkVibrantSwatch();
    Palette.Swatch psMuted = p.getMutedSwatch();
    Palette.Swatch psMutedLight = p.getLightMutedSwatch();
    Palette.Swatch psMutedDark = p.getDarkMutedSwatch();

    for (int i = 0; i < 6; i++)
        palette[i] = 0;
    if (psVibrant != null) {
        palette[0] = psVibrant.getRgb();
    }
    if (psVibrantLight != null) {
        palette[1] = psVibrantLight.getRgb();
    }
    if (psVibrantDark != null) {
        palette[2] = psVibrantDark.getRgb();
    }
    if (psMuted != null) {
        palette[3] = psMuted.getRgb();
    }
    if (psMutedLight != null) {
        palette[4] = psMutedLight.getRgb();
    }
    if (psMutedDark != null) {
        palette[5] = psMutedDark.getRgb();
    }



    float[] hsv = new float[3];
    Color.colorToHSV(Tool.getMostCommonColor(), hsv);
    //     Log.d(hsv[0] + "|" + hsv[1] + "|" + hsv[2], "ColorMe");
    float alpha_7basic = hsv[1];

    final int color1,color2;
    float alpha1,alpha2;

    if(alpha_7basic<0.5f) //  Đủ đậm thì màu mostCommon sẽ là màu song name, màu basic là màu artist
    {
        color1= Tool.getMostCommonColor();
        alpha1 =1;
        color2 = Tool.getBaseColor();
        alpha2 =alpha_7basic;
    }
    else // ngược lại thì màu basic sẽ là màu song name
    {
        int tempColor1 = getBestColorFromPalette(palette);
        if(tempColor1==0) color1 = Tool.getBaseColor();
        else color1 = tempColor1;
        alpha1 = 1;
        color2 =Color.WHITE;
        alpha2 = 0.7f;
    }

    Context context = mWeakRefContext.get();

    if(context!=null&&!mCanceled) {
        Intent intent = new Intent(PALETTE_ACTION);
        intent.putExtra(RESULT, true);
        intent.putExtra(COLOR_ONE, color1);
        intent.putExtra(COLOR_TWO, color2);
        intent.putExtra(ALPHA_ONE, alpha1);
        intent.putExtra(ALPHA_TWO, alpha2);
        context.sendBroadcast(intent);
    }

    else return false;
    return true;
}
 
Example 10
Source File: PhonographColorUtil.java    From MusicPlayer with GNU General Public License v3.0 4 votes vote down vote up
@Override
public int compare(Palette.Swatch lhs, Palette.Swatch rhs) {
    return lhs.getPopulation() - rhs.getPopulation();
}
 
Example 11
Source File: ChannelActivity.java    From Pocket-Plays-for-Twitch with GNU General Public License v3.0 4 votes vote down vote up
private Target getLightThemeTarget() {
	return new Target() {
		@Override
		public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
			streamerImage.setImageBitmap(bitmap);

			Palette palette = 		Palette.from(bitmap).generate();
			int defaultColor = 		Service.getColorAttribute(R.attr.colorPrimary, R.color.primary, getBaseContext());
			int defaultDarkColor = 	Service.getColorAttribute(R.attr.colorPrimaryDark, R.color.primaryDark, getBaseContext());

			int vibrant = 		palette.getVibrantColor(defaultColor);
			int vibrantDark = 	palette.getDarkVibrantColor(defaultColor);
			int vibrantLight = 	palette.getLightVibrantColor(defaultColor);

			int muted = 	 palette.getMutedColor(defaultColor);
			int mutedDark =  palette.getDarkMutedColor(defaultColor);
			int mutedLight = palette.getLightMutedColor(defaultColor);

			Palette.Swatch swatch = null;

			if (vibrant != defaultColor) {
				swatch = palette.getVibrantSwatch();
			} else if (vibrantDark != defaultColor) {
				swatch = palette.getDarkVibrantSwatch();
			} else if (vibrantLight != defaultColor){
				swatch = palette.getLightVibrantSwatch();
			} else if (muted != defaultColor) {
				swatch = palette.getMutedSwatch();
			} else if (mutedDark != defaultColor) {
				swatch = palette.getDarkMutedSwatch();
			} else {
				swatch = palette.getLightMutedSwatch();
			}

			if (swatch != null) {
				float[] swatchValues = swatch.getHsl();
				float[] newSwatch = {swatchValues[0], (float) 0.85, (float) 0.85};
				float[] newSwatchComposite = {(swatchValues[0] + 180) % 360, newSwatch[1], newSwatch[2]};
				float[] newSwatchDark = {newSwatch[0], newSwatch[1], (float) 0.6};

				int newColorDark = Color.HSVToColor(newSwatchDark);
				int newColor = Color.HSVToColor(newSwatch);
				int compositeNewColor = Color.HSVToColor(newSwatchComposite);

				int primaryColor = Service.getBackgroundColorFromView(toolbar, defaultColor);
				int primaryColorDark = Service.getBackgroundColorFromView(mTabs, defaultDarkColor);

				Service.animateBackgroundColorChange(toolbar, newColor, primaryColor, COLOR_FADE_DURATION);
				Service.animateBackgroundColorChange(additionalToolbar, newColor, primaryColor, COLOR_FADE_DURATION);
				Service.animateBackgroundColorChange(mTabs, newColorDark, primaryColorDark, COLOR_FADE_DURATION);
				mFab.setBackgroundTintList(ColorStateList.valueOf(compositeNewColor));
				mTabs.setSelectedTabIndicatorColor(compositeNewColor);

				if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
					Window window = getWindow();
					window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
					window.setStatusBarColor(newColorDark);
				}
			}
		}

		@Override
		public void onBitmapFailed(Drawable errorDrawable) {}

		@Override
		public void onPrepareLoad(Drawable placeHolderDrawable) {}
	};
}
 
Example 12
Source File: VinylMusicPlayerColorUtil.java    From VinylMusicPlayer with GNU General Public License v3.0 4 votes vote down vote up
@Override
public int compare(Palette.Swatch lhs, Palette.Swatch rhs) {
    return lhs.getPopulation() - rhs.getPopulation();
}
 
Example 13
Source File: PaletteUtils.java    From AndroidAnimationExercise with Apache License 2.0 4 votes vote down vote up
@Nullable
private static Palette.Swatch getSwatchInternal(final Palette pPalette, @SwatchStyle final int... pSwatchStyle) {
    for (final int style : pSwatchStyle) {
        switch (style) {
            case SwatchStyle.Vibrant:
                if (pPalette.getVibrantSwatch() != null) {
                    return pPalette.getVibrantSwatch();
                } else {
                    break;
                }
            case SwatchStyle.LightVibrant:
                if (pPalette.getLightVibrantSwatch() != null) {
                    return pPalette.getLightVibrantSwatch();
                } else {
                    break;
                }
            case SwatchStyle.DarkVibrant:
                if (pPalette.getDarkVibrantSwatch() != null) {
                    return pPalette.getDarkVibrantSwatch();
                } else {
                    break;
                }
            case SwatchStyle.Muted:
                if (pPalette.getMutedSwatch() != null) {
                    return pPalette.getMutedSwatch();
                } else {
                    break;
                }
            case SwatchStyle.LightMuted:
                if (pPalette.getLightMutedSwatch() != null) {
                    return pPalette.getLightMutedSwatch();
                } else {
                    break;
                }
            case SwatchStyle.DarkMuted:
                if (pPalette.getDarkMutedSwatch() != null) {
                    return pPalette.getDarkMutedSwatch();
                } else {
                    break;
                }
            default:
                break;
        }
    }

    return DEFAULT_SWATCH;
}
 
Example 14
Source File: PaletteUtils.java    From AndroidAnimationExercise with Apache License 2.0 4 votes vote down vote up
public static Palette.Swatch getSwatch(Palette palette) {
    return getSwatchInternal(palette, SwatchStyle.LightMuted, SwatchStyle.Muted, SwatchStyle.DarkMuted,
            SwatchStyle.LightVibrant, SwatchStyle.Vibrant, SwatchStyle.DarkVibrant);
}
 
Example 15
Source File: PaletteActivity.java    From AndroidAnimationExercise with Apache License 2.0 4 votes vote down vote up
private Palette.Swatch getSwatch(Palette palette) {
    return PaletteUtils.getSwatch(palette);
}
 
Example 16
Source File: PhonographColorUtil.java    From Phonograph with GNU General Public License v3.0 4 votes vote down vote up
@Override
public int compare(Palette.Swatch lhs, Palette.Swatch rhs) {
    return lhs.getPopulation() - rhs.getPopulation();
}
 
Example 17
Source File: MusicColorUtil.java    From Music-Player with GNU General Public License v3.0 4 votes vote down vote up
@Override
public int compare(Palette.Swatch lhs, Palette.Swatch rhs) {
    return lhs.getPopulation() - rhs.getPopulation();
}
 
Example 18
Source File: ChannelActivity.java    From Twire with GNU General Public License v3.0 4 votes vote down vote up
private Target<Bitmap> getLightThemeTarget() {
    return new CustomTarget<Bitmap>() {
        @Override
        public void onResourceReady(@NonNull Bitmap bitmap, @Nullable Transition<? super Bitmap> transition) {
            streamerImage.setImageBitmap(bitmap);

            Palette palette = Palette.from(bitmap).generate();
            int defaultColor = Service.getColorAttribute(R.attr.colorPrimary, R.color.primary, getBaseContext());
            int defaultDarkColor = Service.getColorAttribute(R.attr.colorPrimaryDark, R.color.primaryDark, getBaseContext());

            int vibrant = palette.getVibrantColor(defaultColor);
            int vibrantDark = palette.getDarkVibrantColor(defaultColor);
            int vibrantLight = palette.getLightVibrantColor(defaultColor);

            int muted = palette.getMutedColor(defaultColor);
            int mutedDark = palette.getDarkMutedColor(defaultColor);

            Palette.Swatch swatch;

            if (vibrant != defaultColor) {
                swatch = palette.getVibrantSwatch();
            } else if (vibrantDark != defaultColor) {
                swatch = palette.getDarkVibrantSwatch();
            } else if (vibrantLight != defaultColor) {
                swatch = palette.getLightVibrantSwatch();
            } else if (muted != defaultColor) {
                swatch = palette.getMutedSwatch();
            } else if (mutedDark != defaultColor) {
                swatch = palette.getDarkMutedSwatch();
            } else {
                swatch = palette.getLightMutedSwatch();
            }

            if (swatch != null) {
                float[] swatchValues = swatch.getHsl();
                float[] newSwatch = {swatchValues[0], (float) 0.85, (float) 0.85};
                float[] newSwatchComposite = {(swatchValues[0] + 180) % 360, newSwatch[1], newSwatch[2]};
                float[] newSwatchDark = {newSwatch[0], newSwatch[1], (float) 0.6};

                int newColorDark = Color.HSVToColor(newSwatchDark);
                int newColor = Color.HSVToColor(newSwatch);
                int compositeNewColor = Color.HSVToColor(newSwatchComposite);

                int primaryColor = Service.getBackgroundColorFromView(toolbar, defaultColor);
                int primaryColorDark = Service.getBackgroundColorFromView(mTabs, defaultDarkColor);

                Service.animateBackgroundColorChange(toolbar, newColor, primaryColor, COLOR_FADE_DURATION);
                Service.animateBackgroundColorChange(additionalToolbar, newColor, primaryColor, COLOR_FADE_DURATION);
                Service.animateBackgroundColorChange(mTabs, newColorDark, primaryColorDark, COLOR_FADE_DURATION);
                mFab.setBackgroundTintList(ColorStateList.valueOf(compositeNewColor));
                mTabs.setSelectedTabIndicatorColor(compositeNewColor);

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    Window window = getWindow();
                    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                    window.setStatusBarColor(newColorDark);
                }
            }
        }

        @Override
        public void onLoadCleared(@Nullable Drawable placeholder) {

        }
    };
}
 
Example 19
Source File: PaletteActivity.java    From leafpicrevived with GNU General Public License v3.0 4 votes vote down vote up
private PaletteAdapter(List<Palette.Swatch> sws) {
    this.swatches = sws;
}