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 MapChecks 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 @@