diff --git a/lib/build.gradle b/lib/build.gradle index 9967e1b..1789f85 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -21,8 +21,9 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) - compile 'com.android.support:palette-v7:21.0.0' + compile 'com.android.support:palette-v7:22.1.1' compile 'com.android.support:cardview-v7:22.1.1' + compile 'com.android.support:design:22.2.0' compile 'org.apache.commons:commons-collections4:4.0' } diff --git a/lib/src/androidTest/java/com/musenkishi/paletteloader/ApplicationTest.java b/lib/src/androidTest/java/com/musenkishi/atelier/ApplicationTest.java similarity index 89% rename from lib/src/androidTest/java/com/musenkishi/paletteloader/ApplicationTest.java rename to lib/src/androidTest/java/com/musenkishi/atelier/ApplicationTest.java index 4b8489c..ddf971d 100644 --- a/lib/src/androidTest/java/com/musenkishi/paletteloader/ApplicationTest.java +++ b/lib/src/androidTest/java/com/musenkishi/atelier/ApplicationTest.java @@ -1,4 +1,4 @@ -package com.musenkishi.paletteloader; +package com.musenkishi.atelier; import android.app.Application; import android.test.ApplicationTestCase; diff --git a/lib/src/main/java/com/musenkishi/paletteloader/PaletteLoader.java b/lib/src/main/java/com/musenkishi/atelier/Atelier.java similarity index 67% rename from lib/src/main/java/com/musenkishi/paletteloader/PaletteLoader.java rename to lib/src/main/java/com/musenkishi/atelier/Atelier.java index 67d34fd..704db71 100644 --- a/lib/src/main/java/com/musenkishi/paletteloader/PaletteLoader.java +++ b/lib/src/main/java/com/musenkishi/atelier/Atelier.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2014 Freddie (Musenkishi) Lust-Hed + * Copyright (C) 2015 Freddie (Musenkishi) Lust-Hed * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,11 +14,12 @@ * limitations under the License. */ -package com.musenkishi.paletteloader; +package com.musenkishi.atelier; import android.animation.ArgbEvaluator; import android.animation.ValueAnimator; import android.content.Context; +import android.content.res.ColorStateList; import android.graphics.Bitmap; import android.graphics.Color; import android.graphics.PorterDuff; @@ -29,6 +30,7 @@ import android.os.Handler; import android.os.HandlerThread; import android.os.Message; +import android.support.design.widget.FloatingActionButton; import android.support.v7.graphics.Palette; import android.support.v7.widget.CardView; import android.util.Pair; @@ -36,6 +38,9 @@ import android.widget.ImageView; import android.widget.TextView; +import com.musenkishi.atelier.swatch.Swatch; +import com.musenkishi.atelier.swatch.VibrantSwatch; + import org.apache.commons.collections4.map.LRUMap; import java.util.Collections; @@ -49,7 +54,7 @@ *

* Created by Freddie (Musenkishi) Lust-Hed on 2014-10-21. */ -public class PaletteLoader { +public class Atelier { private static final int MSG_RENDER_PALETTE = 4194; private static final int MSG_DISPLAY_PALETTE = 4195; @@ -65,31 +70,7 @@ public class PaletteLoader { private static Map paletteCache; private static ExecutorService executorService = Executors.newFixedThreadPool(MAX_CONCURRENT_THREADS); - - private PaletteLoader() { - //Don't use - } - - public static PaletteBuilder with(Context context, String id) { - if (paletteCache == null) { - paletteCache = Collections.synchronizedMap( - new LRUMap(MAX_ITEMS_IN_CACHE) - ); - } - if (uiHandler == null || backgroundHandler == null) { - setupHandlers(context); - } - return new PaletteBuilder(id); - } - - private static void setupHandlers(Context context) { - HandlerThread handlerThread = new HandlerThread("palette-loader-background"); - handlerThread.start(); - backgroundHandler = new Handler(handlerThread.getLooper(), sCallback); - uiHandler = new Handler(context.getMainLooper(), sCallback); - } - - private static Handler.Callback sCallback = new Handler.Callback() { + private static Handler.Callback callback = new Handler.Callback() { @Override public boolean handleMessage(Message message) { @@ -98,7 +79,7 @@ public boolean handleMessage(Message message) { case MSG_RENDER_PALETTE: Pair pair = (Pair) message.obj; if (pair != null && !pair.first.isRecycled()) { - Palette palette = Palette.generate(pair.first); + Palette palette = Palette.from(pair.first).generate(); paletteCache.put(pair.second.getId(), palette); @@ -125,93 +106,177 @@ public boolean handleMessage(Message message) { } }; - public static class PaletteBuilder { - - private String id; - private Bitmap bitmap; - private boolean maskDrawable; - private int fallbackColor = Color.TRANSPARENT; - private PaletteRequest paletteRequest = new PaletteRequest(PaletteRequest.SwatchType.REGULAR_VIBRANT, PaletteRequest.SwatchColor.BACKGROUND); - private Palette palette; - private OnPaletteRenderedListener onPaletteRenderedListener; - - public PaletteBuilder(String id) { - this.id = id; - } + private Atelier() { + //Don't use + } - public PaletteBuilder load(Bitmap bitmap) { - this.bitmap = bitmap; - return this; + public static AtelierBuilder with(Context context, String id) { + if (paletteCache == null) { + paletteCache = Collections.synchronizedMap( + new LRUMap(MAX_ITEMS_IN_CACHE) + ); } - - public PaletteBuilder load(Palette palette) { - this.palette = palette; - return this; + if (uiHandler == null || backgroundHandler == null) { + setupHandlers(context); } + return new AtelierBuilder(id); + } - public PaletteBuilder mask() { - maskDrawable = true; - return this; - } + private static void setupHandlers(Context context) { + HandlerThread handlerThread = new HandlerThread("palette-loader-background"); + handlerThread.start(); + backgroundHandler = new Handler(handlerThread.getLooper(), callback); + uiHandler = new Handler(context.getMainLooper(), callback); + } - public PaletteBuilder fallbackColor(int fallbackColor) { - this.fallbackColor = fallbackColor; - return this; + private static void applyColorToView(PaletteTarget target, Palette palette, boolean fromCache) { + if (!isViewRecycled(target)) { + applyColorToView(target, target.getSwatch().getColor(palette), fromCache); } + } - public PaletteBuilder setPaletteRequest(PaletteRequest paletteRequest) { - this.paletteRequest = paletteRequest; - return this; - } + private static void applyColorToView(final PaletteTarget target, int color, boolean fromCache) { + if (target.getView() instanceof TextView) { + applyColorToView((TextView) target.getView(), color, fromCache); + } else if (target.getView() instanceof CardView) { + applyColorToView((CardView) target.getView(), color, fromCache); + } else if (target.getView() instanceof FloatingActionButton) { + applyColorToView((FloatingActionButton) target.getView(), color, fromCache, target.shouldMaskDrawable()); + } else if (target.getView() instanceof ImageView) { + applyColorToView((ImageView) target.getView(), color, fromCache, target.shouldMaskDrawable()); + } else { + if (fromCache) { + target.getView().setBackgroundColor(color); + } else { + Drawable preDrawable; - public PaletteBuilder setListener(OnPaletteRenderedListener onPaletteRenderedListener) { - this.onPaletteRenderedListener = onPaletteRenderedListener; - return this; - } + if (target.getView().getBackground() == null) { + preDrawable = new ColorDrawable(Color.TRANSPARENT); + } else { + preDrawable = target.getView().getBackground(); + } - public void into(View view) { - final PaletteTarget paletteTarget = new PaletteTarget(id, paletteRequest, view, maskDrawable, fallbackColor, onPaletteRenderedListener); - if (palette != null) { - paletteCache.put(paletteTarget.getId(), palette); - applyColorToView(paletteTarget, palette, false); - callListener(palette, onPaletteRenderedListener); - } else { - if (paletteCache.get(id) != null) { - Palette palette = paletteCache.get(id); - applyColorToView(paletteTarget, palette, true); - callListener(palette, onPaletteRenderedListener); + TransitionDrawable transitionDrawable = new TransitionDrawable(new Drawable[]{ + preDrawable, + new ColorDrawable(color) + }); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { + target.getView().setBackground(transitionDrawable); } else { - if (Build.VERSION.SDK_INT >= 21) { - executorService.submit(new PaletteRenderer(bitmap, paletteTarget)); - } else { - Message bgMessage = backgroundHandler.obtainMessage(); - bgMessage.what = MSG_RENDER_PALETTE; - bgMessage.obj = new Pair<>(bitmap, paletteTarget); - backgroundHandler.sendMessage(bgMessage); - } + target.getView().setBackgroundDrawable(transitionDrawable); } + transitionDrawable.startTransition(300); } } + } + private static void applyColorToView(final TextView textView, int color, boolean fromCache) { + if (fromCache) { + textView.setTextColor(color); + } else { + Integer colorFrom = textView.getCurrentTextColor(); + Integer colorTo = color; + ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo); + colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { + @Override + public void onAnimationUpdate(ValueAnimator animator) { + textView.setTextColor((Integer) animator.getAnimatedValue()); + } + }); + colorAnimation.start(); + } } - private static void applyColorToView(PaletteTarget target, Palette palette, boolean fromCache) { - if (!isViewRecycled(target)) { - applyColorToView(target, target.getPaletteRequest().getColor(palette), fromCache); + private static void applyColorToView(final CardView cardView, int color, boolean fromCache) { + if (fromCache) { + cardView.setCardBackgroundColor(color); + } else { + Integer colorFrom = Color.parseColor("#FFFAFAFA"); //Default light CardView color. + Integer colorTo = color; + ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo); + colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { + @Override + public void onAnimationUpdate(ValueAnimator animator) { + cardView.setCardBackgroundColor((Integer) animator.getAnimatedValue()); + } + }); + colorAnimation.setDuration(300); + colorAnimation.start(); } } - private static void applyColorToView(final PaletteTarget target, int color, boolean fromCache) { - if (target.getView() instanceof TextView) { - applyColorToView((TextView) target.getView(), color, fromCache); - return; - } else if (target.getView() instanceof CardView) { - applyColorToCardView((CardView) target.getView(), color, fromCache); - return; + private static void applyColorToView(final FloatingActionButton floatingActionButton, int color, boolean fromCache, boolean shouldMask) { + if (fromCache) { + if (shouldMask) { + if (floatingActionButton.getDrawable() != null) { + floatingActionButton.getDrawable().mutate() + .setColorFilter(color, PorterDuff.Mode.MULTIPLY); + } else if (floatingActionButton.getBackground() != null) { + floatingActionButton.getBackground().mutate() + .setColorFilter(color, PorterDuff.Mode.MULTIPLY); + } + } else { + ColorStateList colorStateList = ColorUtils.generateColorStateList(color); + floatingActionButton.setBackgroundTintList(colorStateList); + } + } else { + if (shouldMask) { + + Integer colorFrom; + ValueAnimator.AnimatorUpdateListener imageAnimatorUpdateListener = new ValueAnimator.AnimatorUpdateListener() { + @Override + public void onAnimationUpdate(ValueAnimator valueAnimator) { + if (floatingActionButton.getDrawable() != null) { + floatingActionButton.getDrawable().mutate() + .setColorFilter((Integer) valueAnimator + .getAnimatedValue(), PorterDuff.Mode.MULTIPLY); + } else if (floatingActionButton.getBackground() != null) { + floatingActionButton.getBackground().mutate() + .setColorFilter((Integer) valueAnimator + .getAnimatedValue(), PorterDuff.Mode.MULTIPLY); + } + } + }; + ValueAnimator.AnimatorUpdateListener animatorUpdateListener; + + PaletteTag paletteTag = (PaletteTag) floatingActionButton.getTag(); + animatorUpdateListener = imageAnimatorUpdateListener; + colorFrom = paletteTag.getColor(); + floatingActionButton.setTag(new PaletteTag(paletteTag.getId(), color)); + + Integer colorTo = color; + ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo); + colorAnimation.addUpdateListener(animatorUpdateListener); + colorAnimation.setDuration(300); + colorAnimation.start(); + + } else { + + Integer colorFrom = Color.parseColor("#FFFAFAFA"); + + ColorStateList colorStateList = floatingActionButton.getBackgroundTintList(); + if (colorStateList != null) { + colorFrom = colorStateList.getDefaultColor(); + } + + Integer colorTo = color; + ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo); + colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { + @Override + public void onAnimationUpdate(ValueAnimator animator) { + int color = (Integer) animator.getAnimatedValue(); + floatingActionButton.setBackgroundTintList(ColorUtils.generateColorStateList(color)); + } + }); + colorAnimation.setDuration(300); + colorAnimation.start(); + } } + } + + private static void applyColorToView(final ImageView imageView, int color, boolean fromCache, boolean shouldMask) { if (fromCache) { - if (target.getView() instanceof ImageView && target.shouldMaskDrawable()) { - ImageView imageView = ((ImageView) target.getView()); + if (shouldMask) { if (imageView.getDrawable() != null) { imageView.getDrawable().mutate() .setColorFilter(color, PorterDuff.Mode.MULTIPLY); @@ -220,13 +285,10 @@ private static void applyColorToView(final PaletteTarget target, int color, bool .setColorFilter(color, PorterDuff.Mode.MULTIPLY); } } else { - target.getView().setBackgroundColor(color); + imageView.setBackgroundColor(color); } } else { - if (target.getView() instanceof ImageView && target.shouldMaskDrawable()) { - - final ImageView imageView = ((ImageView) target.getView()); - + if (shouldMask) { Integer colorFrom; ValueAnimator.AnimatorUpdateListener imageAnimatorUpdateListener = new ValueAnimator.AnimatorUpdateListener() { @Override @@ -244,10 +306,10 @@ public void onAnimationUpdate(ValueAnimator valueAnimator) { }; ValueAnimator.AnimatorUpdateListener animatorUpdateListener; - PaletteTag paletteTag = (PaletteTag) target.getView().getTag(); + PaletteTag paletteTag = (PaletteTag) imageView.getTag(); animatorUpdateListener = imageAnimatorUpdateListener; colorFrom = paletteTag.getColor(); - target.getView().setTag(new PaletteTag(paletteTag.getId(), color)); + imageView.setTag(new PaletteTag(paletteTag.getId(), color)); Integer colorTo = color; ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo); @@ -255,13 +317,12 @@ public void onAnimationUpdate(ValueAnimator valueAnimator) { colorAnimation.setDuration(300); colorAnimation.start(); } else { - Drawable preDrawable; - if (target.getView().getBackground() == null) { + if (imageView.getBackground() == null) { preDrawable = new ColorDrawable(Color.TRANSPARENT); } else { - preDrawable = target.getView().getBackground(); + preDrawable = imageView.getBackground(); } TransitionDrawable transitionDrawable = new TransitionDrawable(new Drawable[]{ @@ -269,56 +330,22 @@ public void onAnimationUpdate(ValueAnimator valueAnimator) { new ColorDrawable(color) }); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { - target.getView().setBackground(transitionDrawable); + imageView.setBackground(transitionDrawable); } else { - target.getView().setBackgroundDrawable(transitionDrawable); + imageView.setBackgroundDrawable(transitionDrawable); } transitionDrawable.startTransition(300); } } } - private static void applyColorToView(final TextView textView, int color, boolean fromCache) { - if (fromCache) { - textView.setTextColor(color); - } else { - Integer colorFrom = textView.getCurrentTextColor(); - Integer colorTo = color; - ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo); - colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { - @Override - public void onAnimationUpdate(ValueAnimator animator) { - textView.setTextColor((Integer) animator.getAnimatedValue()); - } - }); - colorAnimation.start(); - } - } - - private static void applyColorToCardView(final CardView cardView, int color, boolean fromCache) { - if (fromCache) { - cardView.setCardBackgroundColor(color); - } else { - Integer colorFrom = Color.parseColor("#FFFAFAFA"); //Default light CardView color. - Integer colorTo = color; - ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo); - colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { - @Override - public void onAnimationUpdate(ValueAnimator animator) { - cardView.setCardBackgroundColor((Integer) animator.getAnimatedValue()); - } - }); - colorAnimation.start(); - } - } - /** * Is it null? Is that null? What about that one? Is that null too? What about this one? * And this one? Is this null? Is null even null? How can null be real if our eyes aren't real? *

*

Checks whether the view has been recycled or not.

* - * @param target A {@link com.musenkishi.paletteloader.PaletteLoader.PaletteTarget} to check + * @param target A {@link Atelier.PaletteTarget} to check * @return true is view has been recycled, otherwise false. */ private static boolean isViewRecycled(PaletteTarget target) { @@ -339,6 +366,86 @@ private static boolean isViewRecycled(PaletteTarget target) { } } + private static void callListener(Palette palette, OnPaletteRenderedListener onPaletteRenderedListener) { + if (onPaletteRenderedListener != null) { + onPaletteRenderedListener.onRendered(palette); + } + } + + public interface OnPaletteRenderedListener { + void onRendered(Palette palette); + } + + public static class AtelierBuilder { + + private String id; + private Bitmap bitmap; + private boolean maskDrawable; + private int fallbackColor = Color.TRANSPARENT; + private Swatch swatch = new VibrantSwatch(ColorType.BACKGROUND); + private Palette palette; + private OnPaletteRenderedListener onPaletteRenderedListener; + + public AtelierBuilder(String id) { + this.id = id; + } + + public AtelierBuilder load(Bitmap bitmap) { + this.bitmap = bitmap; + return this; + } + + public AtelierBuilder load(Palette palette) { + this.palette = palette; + return this; + } + + public AtelierBuilder mask() { + maskDrawable = true; + return this; + } + + public AtelierBuilder fallbackColor(int fallbackColor) { + this.fallbackColor = fallbackColor; + return this; + } + + public AtelierBuilder swatch(Swatch swatch) { + this.swatch = swatch; + return this; + } + + public AtelierBuilder listener(OnPaletteRenderedListener onPaletteRenderedListener) { + this.onPaletteRenderedListener = onPaletteRenderedListener; + return this; + } + + public void into(View view) { + final PaletteTarget paletteTarget = new PaletteTarget(id, swatch, view, maskDrawable, fallbackColor, onPaletteRenderedListener); + if (palette != null) { + paletteCache.put(paletteTarget.getId(), palette); + applyColorToView(paletteTarget, palette, false); + callListener(palette, onPaletteRenderedListener); + } else { + if (paletteCache.get(id) != null) { + Palette palette = paletteCache.get(id); + applyColorToView(paletteTarget, palette, true); + callListener(palette, onPaletteRenderedListener); + } else { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + executorService.submit(new PaletteRenderer(bitmap, paletteTarget)); + } else { + Message bgMessage = backgroundHandler.obtainMessage(); + bgMessage.what = MSG_RENDER_PALETTE; + bgMessage.obj = new Pair<>(bitmap, paletteTarget); + backgroundHandler.sendMessage(bgMessage); + } + } + } + } + + } + private static class PaletteRenderer implements Runnable { private Bitmap bitmap; @@ -352,7 +459,7 @@ private PaletteRenderer(Bitmap bitmap, PaletteTarget paletteTarget) { @Override public void run() { if (bitmap != null && !bitmap.isRecycled()) { - Palette palette = Palette.generate(bitmap); + Palette palette = Palette.from(bitmap).generate(); paletteCache.put(paletteTarget.getId(), palette); PalettePresenter palettePresenter = new PalettePresenter( @@ -386,14 +493,15 @@ public void run() { private static class PaletteTarget { private String id; - private PaletteRequest paletteRequest; + // private PaletteRequest paletteRequest; + private Swatch swatch; private View view; private boolean maskDrawable; private OnPaletteRenderedListener onPaletteRenderedListener; - private PaletteTarget(String id, PaletteRequest paletteRequest, View view, boolean maskDrawable, int fallbackColor, OnPaletteRenderedListener onPaletteRenderedListener) { + private PaletteTarget(String id, Swatch swatch, View view, boolean maskDrawable, int fallbackColor, OnPaletteRenderedListener onPaletteRenderedListener) { this.id = id; - this.paletteRequest = paletteRequest; + this.swatch = swatch; this.view = view; this.view.setTag(new PaletteTag(this.id, fallbackColor)); this.maskDrawable = maskDrawable; @@ -404,8 +512,8 @@ public String getId() { return id; } - public PaletteRequest getPaletteRequest() { - return paletteRequest; + public Swatch getSwatch() { + return swatch; } public View getView() { @@ -456,14 +564,4 @@ public NoPaletteTagFoundException(String message) { super(message); } } - - public interface OnPaletteRenderedListener { - abstract void onRendered(Palette palette); - } - - private static void callListener(Palette palette, OnPaletteRenderedListener onPaletteRenderedListener) { - if (onPaletteRenderedListener != null) { - onPaletteRenderedListener.onRendered(palette); - } - } } diff --git a/lib/src/main/java/com/musenkishi/atelier/ColorType.java b/lib/src/main/java/com/musenkishi/atelier/ColorType.java new file mode 100644 index 0000000..0b93e6a --- /dev/null +++ b/lib/src/main/java/com/musenkishi/atelier/ColorType.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2015 Freddie (Musenkishi) Lust-Hed + * + * 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.atelier; + +/** + * An enum representing the different colors available in a + * {@link android.support.v7.graphics.Palette.Swatch} + *

Created by Freddie (Musenkishi) Lust-Hed on 04/06/15.

+ */ +public enum ColorType { + BACKGROUND, + TEXT_BODY, + TEXT_TITLE +} diff --git a/lib/src/main/java/com/musenkishi/atelier/ColorUtils.java b/lib/src/main/java/com/musenkishi/atelier/ColorUtils.java new file mode 100644 index 0000000..b205ba2 --- /dev/null +++ b/lib/src/main/java/com/musenkishi/atelier/ColorUtils.java @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2015 Freddie (Musenkishi) Lust-Hed + * + * 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.atelier; + +import android.content.res.ColorStateList; +import android.graphics.Color; + +/** + * General class containing helpful functions regarding colors. + *

Created by Freddie (Musenkishi) Lust-Hed on 08/06/15.

+ */ +public class ColorUtils { + + /** + * Will generate a {@link ColorStateList} based on the color provided. + * The generated {@link ColorStateList} will contain 3 different colors based on the provided; + * a regular, a greyed out (disabled state), and a darkened (pressed state). + * @param color The color you want the ColorStateList based on + * @return a ColorStateList based on provided color + */ + public static ColorStateList generateColorStateList(int color) { + int[][] states = new int[][] { + new int[] { android.R.attr.state_enabled}, // enabled + new int[] {-android.R.attr.state_enabled}, // disabled + new int[] {-android.R.attr.state_checked}, // unchecked + new int[] { android.R.attr.state_pressed} // pressed + }; + + int[] colors = new int[] { + color, + greyOutColor(color), + color, + darkenColor(color) + }; + + return new ColorStateList(states, colors); + } + + private static int darkenColor(int color) { + float[] hsv = new float[3]; + Color.colorToHSV(color, hsv); + hsv[2] *= 0.6f; + return Color.HSVToColor(hsv); + } + + private static int greyOutColor(int color) { + float[] hsv = new float[3]; + Color.colorToHSV(color, hsv); + hsv[1] *= 0.6f; + return Color.HSVToColor(hsv); + } + +} diff --git a/lib/src/main/java/com/musenkishi/atelier/swatch/AbstractSwatch.java b/lib/src/main/java/com/musenkishi/atelier/swatch/AbstractSwatch.java new file mode 100644 index 0000000..a17218a --- /dev/null +++ b/lib/src/main/java/com/musenkishi/atelier/swatch/AbstractSwatch.java @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2015 Freddie (Musenkishi) Lust-Hed + * + * 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.atelier.swatch; + +import android.graphics.Color; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.v7.graphics.Palette; + +import com.musenkishi.atelier.ColorType; + +/** + * An abstract class containing functions that should be available for all child classes. + *

Created by Freddie (Musenkishi) Lust-Hed on 04/06/15.

+ */ +abstract class AbstractSwatch implements Swatch { + + private ColorType colorType; + + public AbstractSwatch(@NonNull ColorType colorType) { + this.colorType = colorType; + } + + protected ColorType getColorType() { + return this.colorType; + } + + protected int getSwatchColor(@NonNull Palette palette, @Nullable Palette.Swatch swatch) { + + swatch = ensureSwatchIsNotNull(palette, swatch); + + switch (getColorType()) { + case BACKGROUND: + return swatch.getRgb(); + + case TEXT_BODY: + return swatch.getBodyTextColor(); + + case TEXT_TITLE: + return swatch.getTitleTextColor(); + } + + return Color.GRAY; + } + + /** + * A function that ensures a non-null {@link android.support.v7.graphics.Palette.Swatch} is + * returned, whether from the {@link Palette} or a new based on Color.GRAY. + * @param palette The desired {@link Palette} + * @param nullableSwatch The {@link Palette.Swatch} that could be null. + * @return A non-null {@link Palette.Swatch} from the + * {@link Palette}, or a new {@link Palette.Swatch} + * based on Color.GRAY. + */ + @NonNull + public Palette.Swatch ensureSwatchIsNotNull(@NonNull Palette palette, @Nullable Palette.Swatch nullableSwatch) { + + if (nullableSwatch != null) { + return nullableSwatch; + } + //nullableSwatch was null, fall back to a swatch that isn't null. + for (Palette.Swatch swatch : palette.getSwatches()) { + if (swatch != null) { + return swatch; + } + } + + return new Palette.Swatch(Color.GRAY, 1); + } + +} diff --git a/lib/src/main/java/com/musenkishi/atelier/swatch/DarkMutedSwatch.java b/lib/src/main/java/com/musenkishi/atelier/swatch/DarkMutedSwatch.java new file mode 100644 index 0000000..ddc3ce5 --- /dev/null +++ b/lib/src/main/java/com/musenkishi/atelier/swatch/DarkMutedSwatch.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2015 Freddie (Musenkishi) Lust-Hed + * + * 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.atelier.swatch; + +import android.support.v7.graphics.Palette; + +import com.musenkishi.atelier.ColorType; + +/** + * A Swatch delegate representing the {@link android.support.v7.graphics.Palette.Swatch} + * from {@link Palette}.getDarkMutedSwatch(). + *

Created by Freddie (Musenkishi) Lust-Hed on 04/06/15.

+ */ +public class DarkMutedSwatch extends AbstractSwatch { + + public DarkMutedSwatch(ColorType colorType) { + super(colorType); + } + + @Override + public int getColor(Palette palette) { + return getSwatchColor(palette, palette.getDarkMutedSwatch()); + } +} diff --git a/lib/src/main/java/com/musenkishi/atelier/swatch/DarkVibrantSwatch.java b/lib/src/main/java/com/musenkishi/atelier/swatch/DarkVibrantSwatch.java new file mode 100644 index 0000000..ec605a5 --- /dev/null +++ b/lib/src/main/java/com/musenkishi/atelier/swatch/DarkVibrantSwatch.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2015 Freddie (Musenkishi) Lust-Hed + * + * 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.atelier.swatch; + +import android.support.v7.graphics.Palette; + +import com.musenkishi.atelier.ColorType; + +/** + * A Swatch delegate representing the {@link android.support.v7.graphics.Palette.Swatch} + * from {@link Palette}.getDarkVibrantSwatch(). + *

Created by Freddie (Musenkishi) Lust-Hed on 04/06/15.

+ */ +public class DarkVibrantSwatch extends AbstractSwatch { + + public DarkVibrantSwatch(ColorType colorType) { + super(colorType); + } + + @Override + public int getColor(Palette palette) { + return getSwatchColor(palette, palette.getDarkVibrantSwatch()); + } +} diff --git a/lib/src/main/java/com/musenkishi/atelier/swatch/LightMutedSwatch.java b/lib/src/main/java/com/musenkishi/atelier/swatch/LightMutedSwatch.java new file mode 100644 index 0000000..6a2880a --- /dev/null +++ b/lib/src/main/java/com/musenkishi/atelier/swatch/LightMutedSwatch.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2015 Freddie (Musenkishi) Lust-Hed + * + * 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.atelier.swatch; + +import android.support.v7.graphics.Palette; + +import com.musenkishi.atelier.ColorType; + +/** + * A Swatch delegate representing the {@link android.support.v7.graphics.Palette.Swatch} + * from {@link Palette}.getLightMutedSwatch(). + *

Created by Freddie (Musenkishi) Lust-Hed on 04/06/15.

+ */ +public class LightMutedSwatch extends AbstractSwatch { + + public LightMutedSwatch(ColorType colorType) { + super(colorType); + } + + @Override + public int getColor(Palette palette) { + return getSwatchColor(palette, palette.getLightMutedSwatch()); + } +} diff --git a/lib/src/main/java/com/musenkishi/atelier/swatch/LightVibrantSwatch.java b/lib/src/main/java/com/musenkishi/atelier/swatch/LightVibrantSwatch.java new file mode 100644 index 0000000..0ac47ca --- /dev/null +++ b/lib/src/main/java/com/musenkishi/atelier/swatch/LightVibrantSwatch.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2015 Freddie (Musenkishi) Lust-Hed + * + * 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.atelier.swatch; + +import android.support.v7.graphics.Palette; + +import com.musenkishi.atelier.ColorType; + +/** + * A Swatch delegate representing the {@link android.support.v7.graphics.Palette.Swatch} + * from {@link Palette}.getLightVibrantSwatch(). + *

Created by Freddie (Musenkishi) Lust-Hed on 04/06/15.

+ */ +public class LightVibrantSwatch extends AbstractSwatch { + + public LightVibrantSwatch(ColorType colorType) { + super(colorType); + } + + @Override + public int getColor(Palette palette) { + return getSwatchColor(palette, palette.getLightVibrantSwatch()); + } +} diff --git a/lib/src/main/java/com/musenkishi/atelier/swatch/MutedSwatch.java b/lib/src/main/java/com/musenkishi/atelier/swatch/MutedSwatch.java new file mode 100644 index 0000000..2ab9884 --- /dev/null +++ b/lib/src/main/java/com/musenkishi/atelier/swatch/MutedSwatch.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2015 Freddie (Musenkishi) Lust-Hed + * + * 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.atelier.swatch; + +import android.support.v7.graphics.Palette; + +import com.musenkishi.atelier.ColorType; + +/** + * A Swatch delegate representing the {@link android.support.v7.graphics.Palette.Swatch} + * from {@link Palette}.getMutedSwatch(). + *

Created by Freddie (Musenkishi) Lust-Hed on 04/06/15.

+ */ +public class MutedSwatch extends AbstractSwatch { + + public MutedSwatch(ColorType colorType) { + super(colorType); + } + + @Override + public int getColor(Palette palette) { + return getSwatchColor(palette, palette.getMutedSwatch()); + } +} diff --git a/lib/src/main/java/com/musenkishi/atelier/swatch/Swatch.java b/lib/src/main/java/com/musenkishi/atelier/swatch/Swatch.java new file mode 100644 index 0000000..c593c8c --- /dev/null +++ b/lib/src/main/java/com/musenkishi/atelier/swatch/Swatch.java @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2015 Freddie (Musenkishi) Lust-Hed + * + * 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.atelier.swatch; + +import android.support.v7.graphics.Palette; + +/** + * An interface representing the basic function that + * {@link com.musenkishi.atelier.Atelier} wants (getColor()). + *

Created by Freddie (Musenkishi) Lust-Hed on 04/06/15.

+ */ +public interface Swatch { + + int getColor(Palette palette); + +} diff --git a/lib/src/main/java/com/musenkishi/atelier/swatch/VibrantSwatch.java b/lib/src/main/java/com/musenkishi/atelier/swatch/VibrantSwatch.java new file mode 100644 index 0000000..1a97bb0 --- /dev/null +++ b/lib/src/main/java/com/musenkishi/atelier/swatch/VibrantSwatch.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2015 Freddie (Musenkishi) Lust-Hed + * + * 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.atelier.swatch; + +import android.support.v7.graphics.Palette; + +import com.musenkishi.atelier.ColorType; + +/** + * A Swatch delegate representing the {@link android.support.v7.graphics.Palette.Swatch} + * from {@link Palette}.getVibrantSwatch(). + *

Created by Freddie (Musenkishi) Lust-Hed on 04/06/15.

+ */ +public class VibrantSwatch extends AbstractSwatch { + + public VibrantSwatch(ColorType colorType) { + super(colorType); + } + + @Override + public int getColor(Palette palette) { + return getSwatchColor(palette, palette.getVibrantSwatch()); + } +} diff --git a/lib/src/main/java/com/musenkishi/paletteloader/PaletteRequest.java b/lib/src/main/java/com/musenkishi/paletteloader/PaletteRequest.java deleted file mode 100644 index 924c95e..0000000 --- a/lib/src/main/java/com/musenkishi/paletteloader/PaletteRequest.java +++ /dev/null @@ -1,193 +0,0 @@ -/* - * Copyright (C) 2014 Freddie (Musenkishi) Lust-Hed - * - * 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.paletteloader; - -import android.graphics.Color; -import android.support.v7.graphics.Palette; - -/** - * 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 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. - } - 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; - } - -} diff --git a/lib/src/main/res/values/strings.xml b/lib/src/main/res/values/strings.xml index 8aadd84..fa530a0 100644 --- a/lib/src/main/res/values/strings.xml +++ b/lib/src/main/res/values/strings.xml @@ -1,3 +1,3 @@ - PaletteLoader + Atelier diff --git a/readme.md b/readme.md index e900d09..16a34fd 100644 --- a/readme.md +++ b/readme.md @@ -1,4 +1,5 @@ -# PaletteLoader +# Atelier +*Every artist needs a place where he can paint in peace and store his tools such as palette and swatches.* ![](https://img.shields.io/github/release/Musenkishi/PaletteLoader.svg?label=JitPack%20Maven) @@ -17,29 +18,27 @@ repositories { } ``` -Now, you can add the PaletteLoader dependency: +Now, you can add the Atelier dependency: ```groovy -compile 'com.github.Musenkishi:PaletteLoader:1.1.0' +compile 'com.github.Musenkishi:Atelier:1.2.0' ``` ## Usage -Currently PaletteLoader supports classes (and extensions of): +Currently Atelier supports classes (and extensions of): * View * ImageView (masking and background) * TextView +* Floating Action Button (icon masking and background) * CardView -If the supplied view is or extends `View`, PaletteLoader will apply the color to the background. If view extends `ImageView` and you use `.mask()`, PaletteLoader will apply the color to the image, not the background. And lastly if view is, or extends `TextView`, the color will be applied to the text, not the background. If view is or extends `CardView`, PaletteLoader will apply the color through `.setCardBackgroundColor()`. +If the supplied view is or extends `View`, Atelier will apply the color to the background. If view extends `ImageView` and you use `.mask()`, Atelier will apply the color to the image, not the background. This is also true for Floating Action Button. And if view is, or extends `TextView`, the color will be applied to the text, not the background. If view is or extends `CardView`, Atelier will apply the color through `.setCardBackgroundColor()`. ```java -PaletteLoader.with(context, uniqueStringForBitmap) //For reuse purposes uniqueStringForBitmap could be the url for the image. - .load(bitmap) - .setPaletteRequest( - new PaletteRequest( - PaletteRequest.SwatchType.REGULAR_VIBRANT, - PaletteRequest.SwatchColor.BACKGROUND)) - .into(view); +Atelier.with(context, uniqueStringForBitmap) //For reuse purposes uniqueStringForBitmap could be the url for the image. + .load(bitmap) + .swatch(new DarkVibrantSwatch(ColorType.BACKGROUND)) + .into(view); ``` ## Example @@ -57,32 +56,23 @@ RequestListener glideDrawableRequestListener = new Reques Context context = viewHolder.bottomBar.getContext(); //Set color to a View's background - PaletteLoader.with(context, model) + Atelier.with(context, model) .load(bitmap) - .setPaletteRequest( - new PaletteRequest( - PaletteRequest.SwatchType.REGULAR_VIBRANT, - PaletteRequest.SwatchColor.BACKGROUND)) + .swatch(new VibrantSwatch(ColorType.BACKGROUND)) .into(viewHolder.bottomBar); //Set color to text in a TextView - PaletteLoader.with(context, model) + Atelier.with(context, model) .load(bitmap) - .setPaletteRequest( - new PaletteRequest( - PaletteRequest.SwatchType.REGULAR_VIBRANT, - PaletteRequest.SwatchColor.TEXT_TITLE)) + .swatch(new VibrantSwatch(ColorType.TEXT_TITLE)) .into(viewHolder.textViewResolution); //Colorize an ImageView/ImageButton with .mask(). //Best result is accomplished with a white image (transparent bakground). - PaletteLoader.with(context, model) + Atelier.with(context, model) .load(bitmap) .fallbackColor(viewHolder.textViewResolution.getCurrentTextColor()) - .setPaletteRequest( - new PaletteRequest( - PaletteRequest.SwatchType.REGULAR_VIBRANT, - PaletteRequest.SwatchColor.TEXT_TITLE)) + .swatch(new VibrantSwatch(ColorType.TEXT_TITLE)) .mask() .into(viewHolder.imageButton); } @@ -98,7 +88,7 @@ RequestListener glideDrawableRequestListener = new Reques .into(viewHolder.imageView); ``` -PaletteLoader will generate the palette in a seperate thread (up to 5 threads on post-lollipop) and apply the color in the assigned view. +Atelier will generate the palette in a seperate thread (up to 5 threads on post-lollipop) and apply the color in the assigned view. ## License diff --git a/sample/build.gradle b/sample/build.gradle index 7413f7c..dd40577 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -21,10 +21,11 @@ android { dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') - compile 'com.android.support:support-v4:22.1.1' - compile 'com.android.support:appcompat-v7:22.1.1' + compile 'com.android.support:support-v4:22.2.0' + compile 'com.android.support:appcompat-v7:22.2.0' compile 'com.android.support:recyclerview-v7:21.0.0' compile 'com.android.support:cardview-v7:22.1.1' + compile 'com.android.support:design:22.2.0' compile 'com.github.bumptech.glide:glide:3.6.0' compile project(':lib') } diff --git a/sample/src/androidTest/java/com/musenkishi/paletteloader/ApplicationTest.java b/sample/src/androidTest/java/com/musenkishi/atelier/ApplicationTest.java similarity index 89% rename from sample/src/androidTest/java/com/musenkishi/paletteloader/ApplicationTest.java rename to sample/src/androidTest/java/com/musenkishi/atelier/ApplicationTest.java index 4b8489c..ddf971d 100644 --- a/sample/src/androidTest/java/com/musenkishi/paletteloader/ApplicationTest.java +++ b/sample/src/androidTest/java/com/musenkishi/atelier/ApplicationTest.java @@ -1,4 +1,4 @@ -package com.musenkishi.paletteloader; +package com.musenkishi.atelier; import android.app.Application; import android.test.ApplicationTestCase; diff --git a/sample/src/main/AndroidManifest.xml b/sample/src/main/AndroidManifest.xml index 92e7ab5..056fa56 100644 --- a/sample/src/main/AndroidManifest.xml +++ b/sample/src/main/AndroidManifest.xml @@ -1,6 +1,6 @@ + package="com.musenkishi.atelier.sample"> diff --git a/sample/src/main/java/com/musenkishi/paletteloader/sample/Countries.java b/sample/src/main/java/com/musenkishi/atelier/sample/Countries.java similarity index 96% rename from sample/src/main/java/com/musenkishi/paletteloader/sample/Countries.java rename to sample/src/main/java/com/musenkishi/atelier/sample/Countries.java index d6ea7d0..09c80b8 100644 --- a/sample/src/main/java/com/musenkishi/paletteloader/sample/Countries.java +++ b/sample/src/main/java/com/musenkishi/atelier/sample/Countries.java @@ -1,4 +1,4 @@ -package com.musenkishi.paletteloader.sample; +package com.musenkishi.atelier.sample; import java.util.ArrayList; import java.util.Arrays; diff --git a/sample/src/main/java/com/musenkishi/paletteloader/sample/Country.java b/sample/src/main/java/com/musenkishi/atelier/sample/Country.java similarity index 89% rename from sample/src/main/java/com/musenkishi/paletteloader/sample/Country.java rename to sample/src/main/java/com/musenkishi/atelier/sample/Country.java index 54ddbeb..bb29737 100644 --- a/sample/src/main/java/com/musenkishi/paletteloader/sample/Country.java +++ b/sample/src/main/java/com/musenkishi/atelier/sample/Country.java @@ -1,6 +1,7 @@ -package com.musenkishi.paletteloader.sample; +package com.musenkishi.atelier.sample; /** + * Sample data * Created by frelus on 13/05/15. */ public class Country { diff --git a/sample/src/main/java/com/musenkishi/paletteloader/sample/CountryAdapter.java b/sample/src/main/java/com/musenkishi/atelier/sample/CountryAdapter.java similarity index 83% rename from sample/src/main/java/com/musenkishi/paletteloader/sample/CountryAdapter.java rename to sample/src/main/java/com/musenkishi/atelier/sample/CountryAdapter.java index e4bad73..d2813c6 100644 --- a/sample/src/main/java/com/musenkishi/paletteloader/sample/CountryAdapter.java +++ b/sample/src/main/java/com/musenkishi/atelier/sample/CountryAdapter.java @@ -1,4 +1,4 @@ -package com.musenkishi.paletteloader.sample; +package com.musenkishi.atelier.sample; import android.content.Context; import android.graphics.Bitmap; @@ -6,7 +6,6 @@ import android.support.v7.widget.CardView; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; -import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; @@ -16,8 +15,9 @@ import com.bumptech.glide.load.resource.drawable.GlideDrawable; import com.bumptech.glide.request.RequestListener; import com.bumptech.glide.request.target.Target; -import com.musenkishi.paletteloader.PaletteLoader; -import com.musenkishi.paletteloader.PaletteRequest; +import com.musenkishi.atelier.Atelier; +import com.musenkishi.atelier.ColorType; +import com.musenkishi.atelier.swatch.VibrantSwatch; /** * A simple adapter for loading countries names and images. @@ -60,17 +60,13 @@ public boolean onResourceReady(GlideDrawable resource, Bitmap bitmap = ((GlideBitmapDrawable) resource).getBitmap(); if (bitmap != null) { Context context = viewHolder.rootView.getContext(); - PaletteLoader.with(context, url) + Atelier.with(context, url) .load(bitmap) - .setPaletteRequest(new PaletteRequest( - PaletteRequest.SwatchType.REGULAR_VIBRANT, - PaletteRequest.SwatchColor.BACKGROUND)) + .swatch(new VibrantSwatch(ColorType.BACKGROUND)) .into(viewHolder.rootView); - PaletteLoader.with(context, url) + Atelier.with(context, url) .load(bitmap) - .setPaletteRequest(new PaletteRequest( - PaletteRequest.SwatchType.REGULAR_VIBRANT, - PaletteRequest.SwatchColor.TEXT_TITLE)) + .swatch(new VibrantSwatch(ColorType.TEXT_TITLE)) .into(viewHolder.textView); } return false; diff --git a/sample/src/main/java/com/musenkishi/atelier/sample/MainActivity.java b/sample/src/main/java/com/musenkishi/atelier/sample/MainActivity.java new file mode 100644 index 0000000..041d3a7 --- /dev/null +++ b/sample/src/main/java/com/musenkishi/atelier/sample/MainActivity.java @@ -0,0 +1,45 @@ +package com.musenkishi.atelier.sample; + +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.os.Bundle; +import android.support.design.widget.FloatingActionButton; +import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; + +import com.musenkishi.atelier.Atelier; +import com.musenkishi.atelier.ColorType; +import com.musenkishi.atelier.swatch.DarkVibrantSwatch; + +public class MainActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview); + LinearLayoutManager layoutManager = new LinearLayoutManager(this); + recyclerView.setLayoutManager(layoutManager); + recyclerView.setAdapter(new CountryAdapter()); + + FloatingActionButton floatingActionButton = (FloatingActionButton) findViewById(R.id.floatingactionbutton); + loadSamplePaletteIntoFAB(floatingActionButton); + } + + private void loadSamplePaletteIntoFAB(FloatingActionButton floatingActionButton) { + Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sample); + Atelier.with(this, "sample") + .load(bitmap) + .swatch(new DarkVibrantSwatch(ColorType.BACKGROUND)) + .into(floatingActionButton); + + Atelier.with(this, "sample") + .load(bitmap) + .swatch(new DarkVibrantSwatch(ColorType.TEXT_TITLE)) + .mask() + .into(floatingActionButton); + } + +} diff --git a/sample/src/main/java/com/musenkishi/paletteloader/sample/MainActivity.java b/sample/src/main/java/com/musenkishi/paletteloader/sample/MainActivity.java deleted file mode 100644 index 0e4422e..0000000 --- a/sample/src/main/java/com/musenkishi/paletteloader/sample/MainActivity.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.musenkishi.paletteloader.sample; - -import android.os.Bundle; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; -import android.view.Menu; -import android.view.MenuItem; - - -public class MainActivity extends AppCompatActivity { - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); - - RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview); - LinearLayoutManager layoutManager = new LinearLayoutManager(this); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(new CountryAdapter()); - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - // Inflate the menu; this adds items to the action bar if it is present. - getMenuInflater().inflate(R.menu.menu_main, menu); - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - // Handle action bar item clicks here. The action bar will - // automatically handle clicks on the Home/Up button, so long - // as you specify a parent activity in AndroidManifest.xml. - int id = item.getItemId(); - - //noinspection SimplifiableIfStatement - if (id == R.id.action_settings) { - return true; - } - - return super.onOptionsItemSelected(item); - } -} diff --git a/sample/src/main/res/drawable-nodpi/sample.jpg b/sample/src/main/res/drawable-nodpi/sample.jpg new file mode 100644 index 0000000..71b0842 Binary files /dev/null and b/sample/src/main/res/drawable-nodpi/sample.jpg differ diff --git a/sample/src/main/res/drawable-xxxhdpi/ic_add_white_48dp.png b/sample/src/main/res/drawable-xxxhdpi/ic_add_white_48dp.png new file mode 100644 index 0000000..165c907 Binary files /dev/null and b/sample/src/main/res/drawable-xxxhdpi/ic_add_white_48dp.png differ diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml index 42f762a..8c0d6f4 100644 --- a/sample/src/main/res/layout/activity_main.xml +++ b/sample/src/main/res/layout/activity_main.xml @@ -1,16 +1,33 @@ + xmlns:custom="http://schemas.android.com/apk/res-auto" + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:clipChildren="false" + tools:context=".MainActivity"> + + + diff --git a/sample/src/main/res/menu/menu_main.xml b/sample/src/main/res/menu/menu_main.xml deleted file mode 100644 index b1cb908..0000000 --- a/sample/src/main/res/menu/menu_main.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - diff --git a/sample/src/main/res/values/strings.xml b/sample/src/main/res/values/strings.xml index fac0da4..f31d3ae 100644 --- a/sample/src/main/res/values/strings.xml +++ b/sample/src/main/res/values/strings.xml @@ -1,4 +1,4 @@ - PaletteLoader Sample + Atelier Sample Settings