better way to brighten colors (#13631)

* better `Color.brighten()` and `Color.darken()` but this time fixed!

* bring the previous behavior for `darken` back

* revert comment
This commit is contained in:
Md. Touhidur Rahman 2025-07-18 15:26:29 +06:00 committed by GitHub
parent 6a652bbe9d
commit f6e583250a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -91,14 +91,21 @@ fun colorFromHex(hexColor: Int): Color {
/** Create a new [Color] instance from [r]/[g]/[b] given as Integers in the range 0..255 */ /** Create a new [Color] instance from [r]/[g]/[b] given as Integers in the range 0..255 */
fun colorFromRGB(r: Int, g: Int, b: Int) = Color(r / 255f, g / 255f, b / 255f, 1f) fun colorFromRGB(r: Int, g: Int, b: Int) = Color(r / 255f, g / 255f, b / 255f, 1f)
/** Create a new [Color] instance from r/g/b given as Integers in the range 0..255 in the form of a 3-element List [rgb] */ /** Create a new [Color] instance from r/g/b given as Integers in the range 0..255 in the form of a 3-element List [rgb] */
fun colorFromRGB(rgb: List<Int>) = colorFromRGB(rgb[0], rgb[1], rgb[2]) fun colorFromRGB(rgb: List<Int>) = colorFromRGB(rgb[0], rgb[1], rgb[2])
/** Linearly interpolates between this [Color] and [BLACK][ImageGetter.CHARCOAL] by [t] which is in the range [[0,1]]. /** Linearly interpolates between this [Color] and [BLACK][ImageGetter.CHARCOAL] by [t] which is in the range [[0,1]].
* The result is returned as a new instance. */ * The result is returned as a new instance. */
fun Color.darken(t: Float): Color = Color(this).lerp(Color.BLACK, t) fun Color.darken(t: Float): Color = Color(this).lerp(Color.BLACK, t)
/** Linearly interpolates between this [Color] and [WHITE][Color.WHITE] by [t] which is in the range [[0,1]].
* The result is returned as a new instance. */ /** Linearly interpolates between this [Color] and [WHITE][Color.WHITE] by [t] which is in the range [[0,1]],
fun Color.brighten(t: Float): Color = Color(this).lerp(Color.WHITE, t) * preserving color ratio in RGB. The result is returned as a new instance. */
fun Color.brighten(t: Float): Color = Color(this).let {
val lightness = maxOf(r, g, b)
val targetRatio = (lightness + t * (1 - lightness)) / lightness
return it.mul(targetRatio)
}
fun Actor.centerX(parent: Actor) { x = parent.width / 2 - width / 2 } fun Actor.centerX(parent: Actor) { x = parent.width / 2 - width / 2 }
@ -242,7 +249,7 @@ fun <T : Actor> Table.addCell(actor: T): Table {
return this return this
} }
/** Shortcut for [Cell].[pad][com.badlogic.gdx.scenes.scene2d.ui.Cell.pad] with top=bottom and left=right */ /** Shortcut for [Cell].[pad][Cell.pad] with top=bottom and left=right */
fun <T : Actor> Cell<T>.pad(vertical: Float, horizontal: Float): Cell<T> { fun <T : Actor> Cell<T>.pad(vertical: Float, horizontal: Float): Cell<T> {
return pad(vertical, horizontal, vertical, horizontal) return pad(vertical, horizontal, vertical, horizontal)
} }