Skip to content

Commit

Permalink
Merge pull request #2 from Musenkishi/feature-improved-swatch
Browse files Browse the repository at this point in the history
Feature improved swatch
  • Loading branch information
musenkishi committed Jun 8, 2015
2 parents 0734bae + e1a4217 commit 5caa159
Show file tree
Hide file tree
Showing 29 changed files with 800 additions and 457 deletions.
3 changes: 2 additions & 1 deletion lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.musenkishi.paletteloader;
package com.musenkishi.atelier;

import android.app.Application;
import android.test.ApplicationTestCase;
Expand Down

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions lib/src/main/java/com/musenkishi/atelier/ColorType.java
Original file line number Diff line number Diff line change
@@ -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}
* <p>Created by Freddie (Musenkishi) Lust-Hed on 04/06/15.</p>
*/
public enum ColorType {
BACKGROUND,
TEXT_BODY,
TEXT_TITLE
}
67 changes: 67 additions & 0 deletions lib/src/main/java/com/musenkishi/atelier/ColorUtils.java
Original file line number Diff line number Diff line change
@@ -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.
* <p>Created by Freddie (Musenkishi) Lust-Hed on 08/06/15.</p>
*/
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);
}

}
Original file line number Diff line number Diff line change
@@ -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.
* <p>Created by Freddie (Musenkishi) Lust-Hed on 04/06/15.</p>
*/
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);
}

}
Original file line number Diff line number Diff line change
@@ -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().
* <p>Created by Freddie (Musenkishi) Lust-Hed on 04/06/15.</p>
*/
public class DarkMutedSwatch extends AbstractSwatch {

public DarkMutedSwatch(ColorType colorType) {
super(colorType);
}

@Override
public int getColor(Palette palette) {
return getSwatchColor(palette, palette.getDarkMutedSwatch());
}
}
Original file line number Diff line number Diff line change
@@ -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().
* <p>Created by Freddie (Musenkishi) Lust-Hed on 04/06/15.</p>
*/
public class DarkVibrantSwatch extends AbstractSwatch {

public DarkVibrantSwatch(ColorType colorType) {
super(colorType);
}

@Override
public int getColor(Palette palette) {
return getSwatchColor(palette, palette.getDarkVibrantSwatch());
}
}
Original file line number Diff line number Diff line change
@@ -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().
* <p>Created by Freddie (Musenkishi) Lust-Hed on 04/06/15.</p>
*/
public class LightMutedSwatch extends AbstractSwatch {

public LightMutedSwatch(ColorType colorType) {
super(colorType);
}

@Override
public int getColor(Palette palette) {
return getSwatchColor(palette, palette.getLightMutedSwatch());
}
}
Original file line number Diff line number Diff line change
@@ -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().
* <p>Created by Freddie (Musenkishi) Lust-Hed on 04/06/15.</p>
*/
public class LightVibrantSwatch extends AbstractSwatch {

public LightVibrantSwatch(ColorType colorType) {
super(colorType);
}

@Override
public int getColor(Palette palette) {
return getSwatchColor(palette, palette.getLightVibrantSwatch());
}
}
38 changes: 38 additions & 0 deletions lib/src/main/java/com/musenkishi/atelier/swatch/MutedSwatch.java
Original file line number Diff line number Diff line change
@@ -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().
* <p>Created by Freddie (Musenkishi) Lust-Hed on 04/06/15.</p>
*/
public class MutedSwatch extends AbstractSwatch {

public MutedSwatch(ColorType colorType) {
super(colorType);
}

@Override
public int getColor(Palette palette) {
return getSwatchColor(palette, palette.getMutedSwatch());
}
}
Loading

0 comments on commit 5caa159

Please sign in to comment.