Skip to content

Commit

Permalink
Native implementation of lightenColor replaced with standard method, …
Browse files Browse the repository at this point in the history
…added darkenColor
  • Loading branch information
kochetov committed Jul 29, 2023
1 parent f3844ee commit caa32b8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 48 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
|__/ \__/ \______/ \___/ |__/|__/|__/ \_______/|__/ \______/ \______/ |_______/
```

[![License: MIT](https://img.shields.io/badge/License-MIT-brightgreen.svg?style=flat-square)](LICENSE) [![MavenCentral](https://img.shields.io/maven-central/v/dev.vladleesi.kutilicious/base?versionPrefix=0.8.1&color=blue&style=flat-square)](https://central.sonatype.com/namespace/dev.vladleesi.kutilicious)
[![License: MIT](https://img.shields.io/badge/License-MIT-brightgreen.svg?style=flat-square)](LICENSE) [![MavenCentral](https://img.shields.io/maven-central/v/dev.vladleesi.kutilicious/base?versionPrefix=0.8.2&color=blue&style=flat-square)](https://central.sonatype.com/namespace/dev.vladleesi.kutilicious)

Small Kotlin and Android extensions for a better development experience

Expand All @@ -23,11 +23,11 @@ repositories {
}
dependencies {
implementation 'dev.vladleesi.kutilicious:base:0.8.1'
implementation 'dev.vladleesi.kutilicious:android-preferences:0.8.1'
implementation 'dev.vladleesi.kutilicious:android-view:0.8.1'
implementation 'dev.vladleesi.kutilicious:android-text:0.8.1'
implementation 'dev.vladleesi.kutilicious:android-color:0.8.1'
implementation 'dev.vladleesi.kutilicious:base:0.8.2'
implementation 'dev.vladleesi.kutilicious:android-preferences:0.8.2'
implementation 'dev.vladleesi.kutilicious:android-view:0.8.2'
implementation 'dev.vladleesi.kutilicious:android-text:0.8.2'
implementation 'dev.vladleesi.kutilicious:android-color:0.8.2'
}
```

Expand Down Expand Up @@ -145,6 +145,7 @@ sharedPreferences.putAsync("isDarkModeEnabled", true)

## Android Color
- `lightenColor`: Lightens an RGB color by a given percentage. It takes an integer color value and a percentage value between 0.0 and 1.0 as inputs. The function returns the lightened color as an integer value representing ARGB components.
- `darkenColor`: Darkens an RGB color by a given percentage. See `lightenColor`.

## License

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import org.junit.runner.RunWith
class ColorExtensionsTest {

@Test
fun testLightenColorByPercentage_withZeroPercentage() {
fun testLightenColor_withZeroPercentage() {
val originalColor = Color.rgb(100, 150, 200)
val lightenedColor = originalColor.lightenColor(0.0f)

assertEquals("Lightening by 0% should return the original color", originalColor, lightenedColor)
}

@Test
fun testLightenColorByPercentage_withMaxPercentage() {
fun testLightenColor_withMaxPercentage() {
val originalColor = Color.rgb(100, 150, 200)
val lightenedColor = originalColor.lightenColor(1.0f)

Expand All @@ -31,7 +31,7 @@ class ColorExtensionsTest {
}

@Test
fun testLightenColorByPercentage_withDifferentColors() {
fun testLightenColor_withDifferentColors() {
val originalColor1 = Color.rgb(50, 70, 90)
val originalColor2 = Color.rgb(200, 50, 100)
val originalColor3 = Color.rgb(0, 255, 0)
Expand All @@ -44,4 +44,19 @@ class ColorExtensionsTest {
assertEquals("Incorrect lightened color for originalColor2", Color.rgb(238, 193, 208), lightenedColor2)
assertEquals("Incorrect lightened color for originalColor3", Color.rgb(51, 255, 51), lightenedColor3)
}

@Test
fun testDarkenColor_withDifferentColors() {
val originalColor1 = Color.rgb(50, 70, 90)
val originalColor2 = Color.rgb(200, 50, 100)
val originalColor3 = Color.rgb(0, 255, 0)

val darkenedColor1 = originalColor1.darkenColor(0.3f)
val darkenedColor2 = originalColor2.darkenColor(0.7f)
val darkenedColor3 = originalColor3.darkenColor(0.2f)

assertEquals("Incorrect darkened color for originalColor1", Color.rgb(35, 49, 63), darkenedColor1)
assertEquals("Incorrect darkened color for originalColor2", Color.rgb(60, 15, 30), darkenedColor2)
assertEquals("Incorrect darkened color for originalColor3", Color.rgb(0, 204, 0), darkenedColor3)
}
}
53 changes: 15 additions & 38 deletions android-color/src/main/java/dev/vladleesi/color/ColorExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,15 @@

package dev.vladleesi.color

import android.graphics.Color
import androidx.annotation.ColorInt
import androidx.annotation.FloatRange
import androidx.core.graphics.ColorUtils

/**
* Created by Vladislav Kochetov on 7/28/2023.
*/

/**
* Represents a fully opaque alpha value (255) for a color in the ColorInt format.
*/
private const val ALPHA_FULLY_OPAQUE = 0xFF

/**
* Number of bits to shift to extract the green component from an RGB color.
*/
private const val GREEN_SHIFT = 8

/**
* Number of bits to shift to extract the red component from an RGB color.
*/
private const val RED_SHIFT = 16

/**
* Number of bits to shift to position the alpha component in the ColorInt format.
*/
private const val ALPHA_SHIFT = 24

/**
* Maximum value for an 8-bit color channel (0xFF).
*/
private const val COLOR_CHANNEL_MAX = 255

/**
* Lightens an RGB color by a given percentage.
*
Expand All @@ -43,17 +20,17 @@ private const val COLOR_CHANNEL_MAX = 255
* @return The lightened color as an integer value representing ARGB (Alpha, Red, Green, Blue) components.
*/
@ColorInt
fun @receiver:ColorInt Int.lightenColor(@FloatRange(from = 0.0, to = 1.0) percentage: Float): Int {
// Extract the red, green, and blue components from the RGB color
val red = (this shr RED_SHIFT) and ALPHA_FULLY_OPAQUE
val green = (this shr GREEN_SHIFT) and ALPHA_FULLY_OPAQUE
val blue = this and ALPHA_FULLY_OPAQUE

// Calculate the new RGB values after lightening
val newRed = (red + (COLOR_CHANNEL_MAX - red) * percentage).toInt()
val newGreen = (green + (COLOR_CHANNEL_MAX - green) * percentage).toInt()
val newBlue = (blue + (COLOR_CHANNEL_MAX - blue) * percentage).toInt()
fun @receiver:ColorInt Int.lightenColor(@FloatRange(from = 0.0, to = 1.0) percentage: Float): Int =
ColorUtils.blendARGB(this, Color.WHITE, percentage)

// Combine the new RGB values to create the final color
return (ALPHA_FULLY_OPAQUE shl ALPHA_SHIFT) or (newRed shl RED_SHIFT) or (newGreen shl GREEN_SHIFT) or newBlue
}
/**
* Darkens an RGB color by a given percentage.
*
* @param percentage The percentage by which to darken the color. Should be a value between 0.0 and 1.0.
* 0.0 represents no change, and 1.0 represents the maximum darkening effect.
*
* @return The darkened color as an integer value representing ARGB (Alpha, Red, Green, Blue) components.
*/
@ColorInt
fun @receiver:ColorInt Int.darkenColor(@FloatRange(from = 0.0, to = 1.0) percentage: Float): Int =
ColorUtils.blendARGB(this, Color.BLACK, percentage)
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ kotlin.code.style=official
android.nonTransitiveRClass=true

publication.groupId=dev.vladleesi.kutilicious
publication.version=0.8.1
publication.version=0.8.2

pom.url=https://github.com/vladleesi/kutilicious
pom.name=kutilicious
Expand Down

0 comments on commit caa32b8

Please sign in to comment.