Skip to content

Commit 7355e4b

Browse files
authored
Merge pull request #20 from KvColorPalette/feature/pass-color-count-for-color-palettes
Feature/pass color count for color palettes
2 parents f1b703c + 7a89618 commit 7355e4b

File tree

4 files changed

+46
-45
lines changed

4 files changed

+46
-45
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ Add the following dependency to your `build.gradle` / `build.gradle.kts` file:
2626
For Groovy - `build.gradle`:
2727
````
2828
dependencies {
29-
implementation 'com.github.KvColorPalette:KvColorPalette-Android:2.1.1'
29+
implementation 'com.github.KvColorPalette:KvColorPalette-Android:2.2.0'
3030
}
3131
````
3232
For Kotlin DSL - `build.gradle.kts`:
3333
````
3434
dependencies {
35-
implementation("com.github.KvColorPalette:KvColorPalette-Android:2.1.1")
35+
implementation("com.github.KvColorPalette:KvColorPalette-Android:2.2.0")
3636
}
3737
````
3838

@@ -46,13 +46,13 @@ If you wants to consume basic features in `KvColorPalette-Android` then use sing
4646
KvColorPalette.instance.generateColorPalette(givenColor = MatPackage().matGold)
4747
4848
// Generate alpha color schem of given color
49-
KvColorPalette.instance.generateAlphaColorPalette(givenColor = MatPackage().matGold.color)
49+
KvColorPalette.instance.generateAlphaColorPalette(givenColor = MatPackage().matGold.color, colorCount = 8)
5050
5151
// Generate lightness color schem of given color
52-
KvColorPalette.instance.generateLightnessColorPalette(givenColor = MatPackage().matGold.color)
52+
KvColorPalette.instance.generateLightnessColorPalette(givenColor = MatPackage().matGold.color, colorCount = 12)
5353
5454
// Generate saturation color schem of given color
55-
KvColorPalette.instance.generateSaturationColorPalette(givenColor = MatPackage().matGold.color)
55+
KvColorPalette.instance.generateSaturationColorPalette(givenColor = MatPackage().matGold.color, colorCount = 15)
5656
5757
// Generate theme color palette of given color
5858
KvColorPalette.instance.generateThemeColorSchemePalette(givenColor = MatPackage().matGold.color)

kv-color-palette/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
kvColorPaletteGroupId=com.github.KvColorPalette
22
kvColorPaletteArtifactId=KvColorPalette-Android
3-
kvColorPaletteVersion=2.1.1
3+
kvColorPaletteVersion=2.2.0

kv-color-palette/src/main/kotlin/com/kavi/droid/color/palette/KvColorPalette.kt

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -66,70 +66,61 @@ class KvColorPalette {
6666
* this method generate a list of colors with different alpha values.
6767
*
6868
* @param givenColor The color to generate the alpha values for.
69+
* @param colorCount The number of colors to generate. In default that returns 10 colors.
70+
* This accept integer value in a range of 2 - 30. Even someone passes number more than 30, this will returns only 30 colors.
6971
* @return A list of colors.
7072
*/
71-
fun generateAlphaColorPalette(givenColor: Color): List<Color> =
72-
listOf(
73-
Color(givenColor.red, givenColor.green, givenColor.blue, 1f),
74-
Color(givenColor.red, givenColor.green, givenColor.blue, .9f),
75-
Color(givenColor.red, givenColor.green, givenColor.blue, .8f),
76-
Color(givenColor.red, givenColor.green, givenColor.blue, .7f),
77-
Color(givenColor.red, givenColor.green, givenColor.blue, .6f),
78-
Color(givenColor.red, givenColor.green, givenColor.blue, .5f),
79-
Color(givenColor.red, givenColor.green, givenColor.blue, .4f),
80-
Color(givenColor.red, givenColor.green, givenColor.blue, .3f),
81-
Color(givenColor.red, givenColor.green, givenColor.blue, .2f),
82-
Color(givenColor.red, givenColor.green, givenColor.blue, .1f),
83-
)
73+
fun generateAlphaColorPalette(givenColor: Color, colorCount: Int = 10): List<Color> {
74+
val colorList = mutableListOf<Color>()
75+
val reviseColorCount = ColorUtil.validateAndReviseColorCount(colorCount)
76+
for (i in reviseColorCount downTo 1) {
77+
colorList.add(Color(givenColor.red, givenColor.green, givenColor.blue, ((1f/colorCount)*i)))
78+
}
79+
return colorList
80+
}
8481

8582
/**
8683
* Generate a list of colors with color saturation values. According to the feeding color,
8784
* this method generate a list of color with different saturation values.
8885
*
8986
* @param givenColor The color to generate the saturation values for.
87+
* @param colorCount The number of colors to generate. In default that returns 10 colors.
88+
* This accept integer value in a range of 2 - 30. Even someone passes number more than 30, this will returns only 30 colors.
9089
* @return A list of colors.
9190
*/
92-
fun generateSaturationColorPalette(givenColor: Color): List<Color> {
91+
fun generateSaturationColorPalette(givenColor: Color, colorCount: Int = 10): List<Color> {
9392
val hue = givenColor.hsl.hue
9493
val lightness = givenColor.hsl.lightness
9594

96-
return listOf(
97-
Color.hsl(hue = hue, saturation = 1f, lightness = lightness),
98-
Color.hsl(hue = hue, saturation = 0.9f, lightness = lightness),
99-
Color.hsl(hue = hue, saturation = 0.8f, lightness = lightness),
100-
Color.hsl(hue = hue, saturation = 0.7f, lightness = lightness),
101-
Color.hsl(hue = hue, saturation = 0.6f, lightness = lightness),
102-
Color.hsl(hue = hue, saturation = 0.5f, lightness = lightness),
103-
Color.hsl(hue = hue, saturation = 0.4f, lightness = lightness),
104-
Color.hsl(hue = hue, saturation = 0.3f, lightness = lightness),
105-
Color.hsl(hue = hue, saturation = 0.2f, lightness = lightness),
106-
Color.hsl(hue = hue, saturation = 0.1f, lightness = lightness)
107-
)
95+
val colorList = mutableListOf<Color>()
96+
val reviseColorCount = ColorUtil.validateAndReviseColorCount(colorCount)
97+
98+
for (i in reviseColorCount downTo 1) {
99+
colorList.add(Color.hsl(hue = hue, saturation = ((1f/colorCount)*i), lightness = lightness))
100+
}
101+
return colorList
108102
}
109103

110104
/**
111105
* Generate a list of colors with color lightness values. According to the feeding color,
112106
* this method generate a list of color with different lightness values.
113107
*
114108
* @param givenColor The color to generate the lightness values for.
109+
* @param colorCount The number of colors to generate. In default that returns 10 colors.
110+
* This accept integer value in a range of 2 - 30. Even someone passes number more than 30, this will returns only 30 colors.
115111
* @return A list of colors.
116112
*/
117-
fun generateLightnessColorPalette(givenColor: Color): List<Color> {
113+
fun generateLightnessColorPalette(givenColor: Color, colorCount: Int = 10): List<Color> {
118114
val hue = givenColor.hsl.hue
119115
val saturation = givenColor.hsl.saturation
120116

121-
return listOf(
122-
Color.hsl(hue = hue, saturation = saturation, lightness = 1f),
123-
Color.hsl(hue = hue, saturation = saturation, lightness = 0.9f),
124-
Color.hsl(hue = hue, saturation = saturation, lightness = 0.8f),
125-
Color.hsl(hue = hue, saturation = saturation, lightness = 0.7f),
126-
Color.hsl(hue = hue, saturation = saturation, lightness = 0.6f),
127-
Color.hsl(hue = hue, saturation = saturation, lightness = 0.5f),
128-
Color.hsl(hue = hue, saturation = saturation, lightness = 0.4f),
129-
Color.hsl(hue = hue, saturation = saturation, lightness = 0.3f),
130-
Color.hsl(hue = hue, saturation = saturation, lightness = 0.2f),
131-
Color.hsl(hue = hue, saturation = saturation, lightness = 0.1f)
132-
)
117+
val colorList = mutableListOf<Color>()
118+
val reviseColorCount = ColorUtil.validateAndReviseColorCount(colorCount)
119+
120+
for (i in reviseColorCount downTo 1) {
121+
colorList.add(Color.hsl(hue = hue, saturation = saturation, lightness = ((1f/colorCount)*i)))
122+
}
123+
return colorList
133124
}
134125

135126
/**

kv-color-palette/src/main/kotlin/com/kavi/droid/color/palette/util/ColorUtil.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,14 @@ object ColorUtil {
120120
abs(colorOne.alpha - colorTwo.alpha)
121121

122122
}
123+
124+
/**
125+
* Validate the color count requested by the user in the color palette.
126+
* If the color count is greater than 30, then it will return 30. If the color count is less than 1, then it will return 1.
127+
*
128+
* @param colorCount [Int] The number of colors to generate.
129+
* @return Int: The validated color count.
130+
*/
131+
internal fun validateAndReviseColorCount(colorCount: Int): Int =
132+
if (colorCount >= 30) { 30 } else if (colorCount <= 1) { 1 } else { colorCount }
123133
}

0 commit comments

Comments
 (0)