Skip to content

Commit 1d67617

Browse files
authored
Merge pull request #16 from KvColorPalette/feature/theme-palette-extension
Feature/theme palette extension
2 parents 46bf467 + ee3c548 commit 1d67617

File tree

8 files changed

+177
-44
lines changed

8 files changed

+177
-44
lines changed

README.md

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This library simplifies the process of building consistent and visually appealin
77

88
# Features
99
* Generate a color palette based on a single input color. Using color alpha/mat-colors
10-
* Create a complete theme color set, including primary, secondary, background, and surface colors.
10+
* Create a complete theme color schemas, including primary, secondary, background, and surface colors.
1111
* Support for Material Design color guidelines.
1212
* Easy integration with Android projects.
1313

@@ -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:1.2.1'
29+
implementation 'com.github.KvColorPalette:KvColorPalette-Android:2.0.0'
3030
}
3131
````
3232
For Kotlin DSL - `build.gradle.kts`:
3333
````
3434
dependencies {
35-
implementation("com.github.KvColorPalette:KvColorPalette-Android:1.2.1")
35+
implementation("com.github.KvColorPalette:KvColorPalette-Android:2.0.0")
3636
}
3737
````
3838

@@ -68,9 +68,9 @@ override fun onCreate() {
6868
KvColorPalette.initialize(Color.blue)
6969
}
7070
````
71-
This initiation create a color set for a theme using the given color at the initiation. This generated color set available for light and dark theme variants.
71+
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.
7272

73-
In this `KvColorPalette.appThemePalette` you will have following color attributes.
73+
In this `KvColorPalette.colorSchemeThemePalette` you will have following color attributes.
7474
|Attribute |light-theme |dark-theme |Description |
7575
|-------------|------------|------------|--------------|
7676
|.base |original |original |This is the base color given by the user. |
@@ -83,37 +83,17 @@ In this `KvColorPalette.appThemePalette` you will have following color attribute
8383
|.onSecondary |available |available |This is the color you can use on any component use secondary color. |
8484
|.shadow |available |available |This is the color for your shadows. |
8585

86+
This `ColorSchemaThemePalette` is another Android Jetpack Compose `ColorSchema`. But that contains additional attributes like `base`, `quaternary`, `shadow` that provide
87+
by the `KvColorPalette-Android` library. All above table mentioned colors are generated according to the given color and created the `ColorScheme`.
88+
8689
### Use generated theme
8790
As mentioned above, according to the initiation color, that generate the color set for light and dark them.
88-
In your Jetpack Compose project, you can assign generate color set the your application theme.
91+
In your Jetpack Compose project, you can assign generate color schemas to your application theme.
8992
````
90-
// Access the generated theme color set from KvColorPalette-Android library
91-
val theme = KvColorPalette.appThemePalette
92-
93-
// Generate application light theme using KvColorPalette-Android light colors
94-
val themeLight = lightColorScheme(
95-
primary = theme.light.primary,
96-
secondary = theme.light.secondary,
97-
tertiary = theme.light.tertiary,
98-
background = theme.light.background,
99-
onPrimary = theme.light.onPrimary,
100-
onSecondary = theme.light.onSecondary
101-
)
102-
103-
// Generate application dark theme using KvColorPalette-Android dark colors
104-
val themeDark = darkColorScheme(
105-
primary = theme.dark.primary,
106-
secondary = theme.dark.secondary,
107-
tertiary = theme.dark.tertiary,
108-
background = theme.dark.background,
109-
onPrimary = theme.dark.onPrimary,
110-
onSecondary = theme.dark.onSecondary
111-
)
112-
11393
// Generate the color schema
11494
val appColorScheme = when {
115-
darkTheme -> themeDark
116-
else -> themeLight
95+
darkTheme -> KvColorPalette.colorSchemeThemePalette.darkColorScheme
96+
else -> KvColorPalette.colorSchemeThemePalette.lightColorScheme
11797
}
11898
11999
// Assign the color schema to the team.

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=1.2.1
3+
kvColorPaletteVersion=2.0.0

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ 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
1515
import com.kavi.droid.color.palette.model.AppThemePalette
16+
import com.kavi.droid.color.palette.model.ColorSchemeThemePalette
1617
import com.kavi.droid.color.palette.model.KvColor
1718
import com.kavi.droid.color.palette.util.ColorUtil
1819
import com.kavi.droid.color.palette.util.ThemeGenUtil
@@ -28,18 +29,23 @@ class KvColorPalette {
2829
* provide a theme color palette. Consumer can use this as a singleton.
2930
*/
3031
var instance: KvColorPalette = KvColorPalette()
32+
@Deprecated("This field is deprecated. This is replaced by colorSchemeThemePalette")
3133
lateinit var appThemePalette: AppThemePalette
34+
lateinit var colorSchemeThemePalette: ColorSchemeThemePalette
3235

3336
/**
3437
* KvColorPalette initialization. Consumer can use this to initialize the KvColorPalette from their application, if they need a
3538
* Theme color palette at the application start-up.
3639
*
3740
* On this initiation of KvColorPalette, we generate a theme color palette using the given color.
3841
* `basicColor` is mandatory parameter while initiate the library.
42+
*
43+
* @param basicColor: Color: Given color for generate theme palette.
3944
*/
4045
fun initialize(basicColor: Color) {
41-
val closestColor = ColorUtil.findClosestColor(basicColor)
42-
appThemePalette = instance.generateThemeColorPalette(closestColor.color)
46+
val closestColor = ColorUtil.findClosestColor(givenColor = basicColor)
47+
appThemePalette = instance.generateThemeColorPalette(givenColor = closestColor.color)
48+
colorSchemeThemePalette = instance.generateThemeColorSchemePalette(givenColor = closestColor.color)
4349
}
4450
}
4551

@@ -48,6 +54,11 @@ class KvColorPalette {
4854
* This generate theme-palette with color transparent. This is un-usable.
4955
*/
5056
appThemePalette = generateThemeColorPalette(Color.Transparent)
57+
58+
/**
59+
* This generate theme-palette with color transparent. This is un-usable.
60+
*/
61+
colorSchemeThemePalette = generateThemeColorSchemePalette(Color.Transparent)
5162
}
5263

5364
/**
@@ -149,8 +160,20 @@ class KvColorPalette {
149160
* @param givenColor The color to generate the theme color palette for.
150161
* @return A theme color palette.
151162
*/
163+
@Deprecated("This method is deprecated and replaced by generateThemeColorSchemePalette method", replaceWith = ReplaceWith(
164+
"KvColorPalette.instance.generateThemeColorSchemePalette(givenColor = givenColor)"
165+
))
152166
fun generateThemeColorPalette(givenColor: Color): AppThemePalette = ThemeGenUtil.generateThemeColorSet(givenColor = givenColor)
153167

168+
/**
169+
* Generate a theme color palette. According to the feeding color,
170+
* this method generate a color scheme theme color palette.
171+
*
172+
* @param givenColor The color to generate the theme color palette for.
173+
* @return A color scheme theme palette. [ColorSchemeThemePalette]
174+
*/
175+
fun generateThemeColorSchemePalette(givenColor: Color): ColorSchemeThemePalette = ThemeGenUtil.generateThemeColorScheme(givenColor = givenColor)
176+
154177
/**
155178
* This method finds the closest KvColor available in the KvColorPalette-Android to the given color
156179
*

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.kavi.droid.color.palette.extension
33
import androidx.compose.ui.graphics.Color
44
import androidx.compose.ui.graphics.toArgb
55
import androidx.core.graphics.ColorUtils
6+
import com.kavi.droid.color.palette.model.HSL
67

78
/**
89
* Extension function to get HSL properties of a [Color]
@@ -12,15 +13,6 @@ import androidx.core.graphics.ColorUtils
1213
internal val Color.hsl: HSL
1314
get() = getHslProperties(this)
1415

15-
/**
16-
* Data class to hold the hue, saturation and lightness properties of a [Color]
17-
*/
18-
data class HSL(
19-
val hue: Float,
20-
val saturation: Float,
21-
val lightness: Float
22-
)
23-
2416
/**
2517
* This method extract and returns the hue, saturation and lightness properties of a [Color]
2618
*
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
package com.kavi.droid.color.palette.extension
22

3+
import androidx.compose.material3.ColorScheme
34
import androidx.compose.ui.graphics.Color
45

56
/**
67
* Extension property to check if a [Color] is a high-light color.
78
*/
89
val Color.isHighLightColor: Boolean
9-
get() = this.hsl.lightness > 0.6f
10+
get() = this.hsl.lightness > 0.6f
11+
12+
/**
13+
* Providing new extension fields to [ColorScheme]
14+
* @see base: Base color is the consumer provided color to generate the theme
15+
* @see quaternary: Quaternary color is for the use of using primary color in light mode with contrast color in dark mode
16+
* @see shadow: Shadow color is for the use of shadow in light mode and dark mode.
17+
*/
18+
var ColorScheme.base: Color
19+
get() = Color.White
20+
set(value) {}
21+
var ColorScheme.quaternary: Color
22+
get() = Color.White
23+
set(value) {}
24+
var ColorScheme.shadow: Color
25+
get() = Color.White
26+
set(value) {}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.kavi.droid.color.palette.model
2+
3+
import androidx.compose.ui.graphics.Color
4+
5+
/**
6+
* Data class to hold the hue, saturation and lightness properties of a [Color]
7+
*/
8+
data class HSL(
9+
val hue: Float,
10+
val saturation: Float,
11+
val lightness: Float
12+
)

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
package com.kavi.droid.color.palette.model
22

3+
import androidx.compose.material3.ColorScheme
34
import androidx.compose.ui.graphics.Color
45

56
/**
67
* Application theme palette for light and dark mode.
78
*/
9+
@Deprecated(message = "This model is deprecated",
10+
ReplaceWith("ColorSchemeThemePalette(lightColorScheme = lightScheme, darkColorScheme = darkScheme)")
11+
)
812
data class AppThemePalette(
913
val light: ThemeColorPalette,
1014
val dark: ThemeColorPalette
1115
)
1216

17+
/**
18+
* Application [ColorScheme] theme palette for light and dark mode.
19+
*/
20+
data class ColorSchemeThemePalette(
21+
val lightColorScheme: ColorScheme,
22+
val darkColorScheme: ColorScheme
23+
)
24+
1325
/**
1426
* Color palette for theme.
1527
*/

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

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,79 @@
11
package com.kavi.droid.color.palette.util
22

3+
import android.content.Context
4+
import android.content.res.Configuration
5+
import androidx.compose.material3.ColorScheme
6+
import androidx.compose.material3.darkColorScheme
7+
import androidx.compose.material3.lightColorScheme
38
import androidx.compose.ui.graphics.Color
9+
import com.kavi.droid.color.palette.extension.base
410
import com.kavi.droid.color.palette.extension.hsl
11+
import com.kavi.droid.color.palette.extension.quaternary
12+
import com.kavi.droid.color.palette.extension.shadow
513
import com.kavi.droid.color.palette.model.AppThemePalette
14+
import com.kavi.droid.color.palette.model.ColorSchemeThemePalette
615
import com.kavi.droid.color.palette.model.ThemeColorPalette
716

817
object ThemeGenUtil {
918

19+
/**
20+
* This is to find out application is in dark mode or not.
21+
*
22+
* @param context: Context: Android context
23+
* @return Boolean: Boolean value says application in dark mode or not.
24+
*/
25+
fun isAppInNightMode(context: Context): Boolean {
26+
return when (context.resources.configuration.uiMode.and(Configuration.UI_MODE_NIGHT_MASK)) {
27+
Configuration.UI_MODE_NIGHT_YES -> {
28+
true
29+
}
30+
Configuration.UI_MODE_NIGHT_NO,
31+
Configuration.UI_MODE_NIGHT_UNDEFINED -> {
32+
false
33+
}
34+
else -> {
35+
false
36+
}
37+
}
38+
}
39+
1040
/**
1141
* Generate theme color set for given color.
1242
* @param givenColor The color to generate theme color set.
1343
* @return A theme color set. [AppThemePalette]
1444
*/
45+
@Deprecated(
46+
message = "This method is deprecated and replaced by generateThemeColorScheme.",
47+
replaceWith = ReplaceWith("ThemeGenUtil.generateThemeColorScheme(givenColor = givenColor)")
48+
)
1549
internal fun generateThemeColorSet(givenColor: Color): AppThemePalette {
1650
val lightColorPalette = generateLightThemeColorSet(givenColor)
1751
val darkColorPalette = generateDarkThemeColorSet(givenColor)
1852

1953
return AppThemePalette(light = lightColorPalette, dark = darkColorPalette)
2054
}
2155

56+
/**
57+
* Generate theme color set for given color.
58+
* @param givenColor The color to generate theme color set.
59+
* @return A theme color set. [ColorSchemeThemePalette]
60+
*/
61+
internal fun generateThemeColorScheme(givenColor: Color): ColorSchemeThemePalette {
62+
val lightColorPalette = generateThemeLightColorScheme(givenColor)
63+
val darkColorPalette = generateThemeDarkColorScheme(givenColor)
64+
65+
return ColorSchemeThemePalette(lightColorScheme = lightColorPalette, darkColorScheme = darkColorPalette)
66+
}
67+
2268
/**
2369
* Generate light theme color set for given color.
2470
* @param givenColor The color to generate theme color set.
2571
* @return A light theme color set. [ThemeColorPalette]
2672
*/
73+
@Deprecated(
74+
message = "This method is deprecated and replaced by generateThemeLightColorScheme.",
75+
replaceWith = ReplaceWith("ThemeGenUtil.generateThemeLightColorScheme(givenColor = givenColor)")
76+
)
2777
private fun generateLightThemeColorSet(givenColor: Color): ThemeColorPalette {
2878
return ThemeColorPalette(
2979
base = givenColor,
@@ -38,11 +88,36 @@ object ThemeGenUtil {
3888
)
3989
}
4090

91+
/**
92+
* Generate light theme color set for given color.
93+
* @param givenColor The color to generate theme color set.
94+
* @return A light theme color set. [ColorScheme]
95+
*/
96+
private fun generateThemeLightColorScheme(givenColor: Color): ColorScheme {
97+
val lightColorScheme = lightColorScheme(
98+
primary = givenColor,
99+
secondary = generateLightSecondaryColor(givenColor),
100+
tertiary = generateLightTertiaryColor(givenColor),
101+
background = generateLightBackgroundColor(givenColor),
102+
onPrimary = Color.White,
103+
onSecondary = Color.White
104+
)
105+
lightColorScheme.base = givenColor
106+
lightColorScheme.quaternary = givenColor // This is for use light theme primary color dark theme contrast color
107+
lightColorScheme.shadow = Color.Gray
108+
109+
return lightColorScheme
110+
}
111+
41112
/**
42113
* Generate dark theme color set for given color.
43114
* @param givenColor he color to generate theme color set.
44115
* @return A dark theme color set. [ThemeColorPalette]
45116
*/
117+
@Deprecated(
118+
message = "This method is deprecated and replaced by generateThemeDarkColorScheme.",
119+
replaceWith = ReplaceWith("ThemeGenUtil.generateThemeDarkColorScheme(givenColor = givenColor)")
120+
)
46121
private fun generateDarkThemeColorSet(givenColor: Color): ThemeColorPalette {
47122
return ThemeColorPalette(
48123
base = givenColor,
@@ -57,6 +132,28 @@ object ThemeGenUtil {
57132
)
58133
}
59134

135+
/**
136+
* Generate dark theme color set for given color.
137+
* @param givenColor he color to generate theme color set.
138+
* @return A dark theme color set. [ColorScheme]
139+
*/
140+
private fun generateThemeDarkColorScheme(givenColor: Color): ColorScheme {
141+
val darkColorScheme = darkColorScheme(
142+
primary = generateDarkPrimaryColor(givenColor),
143+
secondary = generateDarkSecondaryColor(givenColor),
144+
tertiary = generateDarkTertiaryColor(givenColor),
145+
background = generateDarkBackgroundColor(givenColor),
146+
onPrimary = Color.White,
147+
onSecondary = Color.Black,
148+
)
149+
150+
darkColorScheme.base = givenColor
151+
darkColorScheme.quaternary = generateDarkSecondaryColor(givenColor) // This is for use light theme primary color dark theme contrast color
152+
darkColorScheme.shadow = Color.White
153+
154+
return darkColorScheme
155+
}
156+
60157
/**
61158
* Generate light secondary color for given color.
62159
*/

0 commit comments

Comments
 (0)