Skip to content

Commit efae3a0

Browse files
authored
Merge pull request #22 from KvColorPalette/feature/accept-secondary-color-to-generate-theme
Feature/accept secondary color to generate theme
2 parents 4b474e8 + 9a31bca commit efae3a0

File tree

7 files changed

+208
-100
lines changed

7 files changed

+208
-100
lines changed

README.md

Lines changed: 19 additions & 4 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.2.0'
29+
implementation 'com.github.KvColorPalette:KvColorPalette-Android:3.0.0'
3030
}
3131
````
3232
For Kotlin DSL - `build.gradle.kts`:
3333
````
3434
dependencies {
35-
implementation("com.github.KvColorPalette:KvColorPalette-Android:2.2.0")
35+
implementation("com.github.KvColorPalette:KvColorPalette-Android:3.0.0")
3636
}
3737
````
3838

@@ -60,12 +60,27 @@ KvColorPalette.instance.generateThemeColorSchemePalette(givenColor = MatPackage(
6060

6161
### Advance Usage
6262
If you wants to use `KvColorPalette-Android` to generate your theme color palette when your application start-up, then you have to initiate the library in Application level.
63-
To initiate you have to pass one base color that you think your application will use. Use following code to initiate the library package.
63+
There are two ways to initiate the library. These two methods for two different scenarios. First is to initiate the library with one base color and other is to initiate the library with two base colors.
64+
In two color method, there are additional parameters like `bias` and `themeGenPattern`.
65+
If your application styling with one base color then you can use following method to initialized,
6466
````
6567
override fun onCreate() {
6668
super.onCreate()
6769
// Initialize the KvColorPalette-Android
68-
KvColorPalette.initialize(Color.blue)
70+
KvColorPalette.initialize(baseColor = Color.blue)
71+
}
72+
````
73+
If your application has two base colors, then you can use following method to initialized,
74+
````
75+
override fun onCreate() {
76+
super.onCreate()
77+
// Initialize the KvColorPalette-Android
78+
KvColorPalette.initialize(
79+
baseColor = Color.blue,
80+
secondColor = Color.red,
81+
themeGenPattern = ThemeGenMode.BLEND // The way to generate the theme palette using two colors.
82+
bias = 0.5f, // How bias to first or second color
83+
)
6984
}
7085
````
7186
This initiation create a color schemas for a theme using the given color at the initiation. This generated color schemas will available for light and dark theme variants.

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.2.0
3+
kvColorPaletteVersion=3.0.0

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

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import com.kavi.droid.color.palette.color.Mat800Package
1212
import com.kavi.droid.color.palette.color.Mat900Package
1313
import com.kavi.droid.color.palette.color.MatPackage
1414
import com.kavi.droid.color.palette.extension.hsl
15-
import com.kavi.droid.color.palette.model.AppThemePalette
1615
import com.kavi.droid.color.palette.model.ColorSchemeThemePalette
1716
import com.kavi.droid.color.palette.model.KvColor
17+
import com.kavi.droid.color.palette.model.ThemeGenMode
1818
import com.kavi.droid.color.palette.util.ColorUtil
1919
import com.kavi.droid.color.palette.util.ThemeGenUtil
2020

@@ -29,32 +29,48 @@ class KvColorPalette {
2929
* provide a theme color palette. Consumer can use this as a singleton.
3030
*/
3131
var instance: KvColorPalette = KvColorPalette()
32-
@Deprecated("This field is deprecated. This is replaced by colorSchemeThemePalette")
33-
lateinit var appThemePalette: AppThemePalette
3432
lateinit var colorSchemeThemePalette: ColorSchemeThemePalette
3533

3634
/**
3735
* KvColorPalette initialization. Consumer can use this to initialize the KvColorPalette from their application, if they need a
3836
* Theme color palette at the application start-up.
3937
*
40-
* On this initiation of KvColorPalette, we generate a theme color palette using the given color.
41-
* `basicColor` is mandatory parameter while initiate the library.
38+
* On this initiation of KvColorPalette, library generate a theme color palette using the given color.
39+
* `baseColor` is mandatory parameter while initiate the library.
4240
*
43-
* @param basicColor: Color: Given color for generate theme palette.
41+
* @param baseColor: Color: Given color for generate theme palette.
4442
*/
45-
fun initialize(basicColor: Color) {
46-
val closestColor = ColorUtil.findClosestColor(givenColor = basicColor)
47-
appThemePalette = instance.generateThemeColorPalette(givenColor = closestColor.color)
48-
colorSchemeThemePalette = instance.generateThemeColorSchemePalette(givenColor = closestColor.color)
43+
fun initialize(baseColor: Color) {
44+
colorSchemeThemePalette = instance.generateThemeColorSchemePalette(givenColor = baseColor)
4945
}
50-
}
5146

52-
init {
5347
/**
54-
* This generate theme-palette with color transparent. This is un-usable.
48+
* KvColorPalette initialization. Consumer can use this to initialize the KvColorPalette from their application, if they need a
49+
* Theme color palette at the application start-up.
50+
*
51+
* On this initiation of KvColorPalette, library generate a theme color palette using the given base color and second color.
52+
* `baseColor` and `secondColor` are mandatory parameter while initiate the library. Other two parameters are optional.
53+
*
54+
* @param baseColor: Color: Given first for generate theme palette.
55+
* @param secondColor: Color: Given second color for generate theme palette.
56+
* @param bias: Float: The bias value to blend the two colors. In default that is 0.5f. This accept float value in a range of 0.0 - 1.0.
57+
* 0f means full bias to first color and 1f means full bias to second color.
58+
* @param themeGenMode: ThemeGenPattern: The pattern to generate the theme color palette.
59+
* Default is [ThemeGenMode.SEQUENCE] and available options are [ThemeGenMode.SEQUENCE] and [ThemeGenMode.BLEND]
60+
* - [ThemeGenMode.SEQUENCE] will add base color & primary & second color as secondary, rest of the colors will generate by using given base color.
61+
* - [ThemeGenMode.BLEND] will add base color & primary & second color as primary, rest of the colors will generate by after generating new color blend first and second colors.
5562
*/
56-
appThemePalette = generateThemeColorPalette(Color.Transparent)
63+
fun initialize(baseColor: Color, secondColor: Color, bias: Float = .5f, themeGenMode: ThemeGenMode = ThemeGenMode.SEQUENCE) {
64+
colorSchemeThemePalette = instance.generateMultiColorThemeColorSchemePalette(
65+
givenColor = baseColor,
66+
secondColor = secondColor,
67+
bias = bias,
68+
themeGenMode = themeGenMode
69+
)
70+
}
71+
}
5772

73+
init {
5874
/**
5975
* This generate theme-palette with color transparent. This is un-usable.
6076
*/
@@ -146,25 +162,30 @@ class KvColorPalette {
146162

147163
/**
148164
* Generate a theme color palette. According to the feeding color,
149-
* this method generate a theme color palette.
165+
* this method generate a color scheme theme color palette.
150166
*
151167
* @param givenColor The color to generate the theme color palette for.
152-
* @return A theme color palette.
168+
* @return A color scheme theme palette. [ColorSchemeThemePalette]
153169
*/
154-
@Deprecated("This method is deprecated and replaced by generateThemeColorSchemePalette method", replaceWith = ReplaceWith(
155-
"KvColorPalette.instance.generateThemeColorSchemePalette(givenColor = givenColor)"
156-
))
157-
fun generateThemeColorPalette(givenColor: Color): AppThemePalette = ThemeGenUtil.generateThemeColorSet(givenColor = givenColor)
170+
fun generateThemeColorSchemePalette(givenColor: Color): ColorSchemeThemePalette = ThemeGenUtil.singleColorThemeColorScheme(givenColor = givenColor)
158171

159172
/**
160173
* Generate a theme color palette. According to the feeding color,
161174
* this method generate a color scheme theme color palette.
162175
*
163176
* @param givenColor The color to generate the theme color palette for.
177+
* @param secondColor The secondary color to generate the theme color palette blending with first color.
178+
* @param bias The bias value to blend the two colors. In default that is 0.5f. This accept float value in a range of 0.0 - 1.0.
179+
* 0f means full bias to first color and 1f means full bias to second color.
180+
* @param themeGenMode: ThemeGenPattern: The pattern to generate the theme color palette.
181+
* Default is [ThemeGenMode.SEQUENCE] and available options are [ThemeGenMode.SEQUENCE] and [ThemeGenMode.BLEND]
182+
* - [ThemeGenMode.SEQUENCE] will add base color & primary & second color as secondary, rest of the colors will generate by using given base color.
183+
* - [ThemeGenMode.BLEND] will add base color & primary & second color as primary, rest of the colors will generate by after generating new color blend first and second colors.
164184
* @return A color scheme theme palette. [ColorSchemeThemePalette]
165185
*/
166-
fun generateThemeColorSchemePalette(givenColor: Color): ColorSchemeThemePalette = ThemeGenUtil.generateThemeColorScheme(givenColor = givenColor)
167-
186+
fun generateMultiColorThemeColorSchemePalette(givenColor: Color, secondColor: Color, bias: Float = .5f, themeGenMode: ThemeGenMode = ThemeGenMode.SEQUENCE): ColorSchemeThemePalette {
187+
return ThemeGenUtil.multiColorInputThemeColorScheme(givenColor = givenColor, secondColor = secondColor, bias = bias, themeGenMode = themeGenMode)
188+
}
168189
/**
169190
* This method finds the closest KvColor available in the KvColorPalette-Android to the given color
170191
*

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,6 @@ package com.kavi.droid.color.palette.model
33
import androidx.compose.material3.ColorScheme
44
import androidx.compose.ui.graphics.Color
55

6-
/**
7-
* Application theme palette for light and dark mode.
8-
*/
9-
@Deprecated(message = "This model is deprecated",
10-
ReplaceWith("ColorSchemeThemePalette(lightColorScheme = lightScheme, darkColorScheme = darkScheme)")
11-
)
12-
data class AppThemePalette(
13-
val light: ThemeColorPalette,
14-
val dark: ThemeColorPalette
15-
)
16-
176
/**
187
* Application [ColorScheme] theme palette for light and dark mode.
198
*/
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.kavi.droid.color.palette.model
2+
3+
enum class ThemeGenMode {
4+
SEQUENCE, BLEND
5+
}

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ object ColorUtil {
5151
return String.format("#%02x%02x%02x%02x", (color.alpha * 255).toInt(),(color.red * 255).toInt(), (color.green * 255).toInt(), (color.blue * 255).toInt())
5252
}
5353

54+
/**
55+
* This method is to blend given two colors and return new color
56+
*
57+
* @param firstColor [Color] First color to blend
58+
* @param secondColor [Color] Second color to blend
59+
* @param bias [Float] Bias to the new color for first / second color.
60+
*/
61+
internal fun blendColors(firstColor: Color, secondColor: Color, bias: Float = 0.5f): Color {
62+
val blendRed = colorBlendingComponent(firstColor.red, secondColor.red, bias)
63+
val blendGreen = colorBlendingComponent(firstColor.green, secondColor.green, bias)
64+
val blendBlue = colorBlendingComponent(firstColor.blue, secondColor.blue, bias)
65+
66+
return Color(blendRed / 255, blendGreen / 255, blendBlue / 255)
67+
}
68+
5469
/**
5570
* Get closest color to the given color from available color packages.
5671
* This compares the available colors and find out the closest `KvColor` to the given color.
@@ -130,4 +145,26 @@ object ColorUtil {
130145
*/
131146
internal fun validateAndReviseColorCount(colorCount: Int): Int =
132147
if (colorCount >= 30) { 30 } else if (colorCount <= 1) { 1 } else { colorCount }
148+
149+
/**
150+
* This method can return the color value of red/green/blue according to the blending bias
151+
* with given first color's red/green/blue value and second color's red/green/blue value.
152+
*
153+
* @param firstColor The first color's red/green/blue value
154+
* @param secondColor The second color's red/green/blue value
155+
* @param bias The blending bias value.
156+
*
157+
*/
158+
private fun colorBlendingComponent(firstColor: Float, secondColor: Float, bias: Float): Float {
159+
val difference = abs(firstColor * 255 - secondColor * 255)
160+
val blending = difference * bias // How bias to the blending colors, first or second
161+
162+
return if (firstColor < secondColor) {
163+
(firstColor * 255) + blending // First color is in lower end, therefore adding bias
164+
} else if (firstColor > secondColor) {
165+
(firstColor * 255) - blending // First color is in higher end, therefore subtracting bias
166+
} else {
167+
(firstColor * 255) // This means, first component and second component are same. Therefore, returns same value.
168+
}
169+
}
133170
}

0 commit comments

Comments
 (0)