-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add GradientOffset for rotating gradients by 45 degrees
- Loading branch information
1 parent
8867819
commit c97e823
Showing
1 changed file
with
85 additions
and
0 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
extendedcolors/src/main/java/com/smarttoolfactory/extendedcolors/GradientOffset.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.smarttoolfactory.extendedcolors | ||
|
||
import androidx.compose.ui.geometry.Offset | ||
|
||
/** | ||
* | ||
* Get a [GradientOffset] that rotate a gradient clockwise with specified angle in degrees. | ||
* Default value for [GradientOffset] is [GradientAngle.CW0] which is 0 degrees | ||
* that returns a horizontal gradient. | ||
* | ||
* Get start and end offsets that are limited between [0f, Float.POSITIVE_INFINITY] in x and | ||
* y axes wrapped in [GradientOffset]. | ||
* Infinity is converted to Composable width on x axis, height on y axis in shader. | ||
* | ||
* Default angle for [Brush.linearGradient] when no offset is 0 degrees in Compose , | ||
* [Brush.verticalGradient] is [Brush.linearGradient] with 90 degrees. | ||
* | ||
* ``` | ||
* 0 degrees | ||
* start = Offset(0f,0f), | ||
* end = Offset(Float.POSITIVE_INFINITY,0f) | ||
* | ||
* 45 degrees | ||
* start = Offset(0f, Float.POSITIVE_INFINITY), | ||
* end = Offset(Float.POSITIVE_INFINITY, 0f) | ||
* | ||
* 90 degrees | ||
* start = Offset(0f, Float.POSITIVE_INFINITY), | ||
* end = Offset.Zero | ||
* | ||
* 135 degrees | ||
* start = Offset.Infinity, | ||
* end = Offset.Zero | ||
* | ||
* 180 degrees | ||
* start = Offset(Float.POSITIVE_INFINITY, 0f), | ||
* end = Offset.Zero, | ||
* | ||
* ``` | ||
*/ | ||
fun GradientOffset(angle: GradientAngle = GradientAngle.CW0): GradientOffset { | ||
return when (angle) { | ||
GradientAngle.CW45 -> GradientOffset( | ||
start = Offset.Zero, | ||
end = Offset.Infinite | ||
) | ||
GradientAngle.CW90 -> GradientOffset( | ||
start = Offset.Zero, | ||
end = Offset(0f, Float.POSITIVE_INFINITY) | ||
) | ||
GradientAngle.CW135 -> GradientOffset( | ||
start = Offset(Float.POSITIVE_INFINITY, 0f), | ||
end = Offset(0f, Float.POSITIVE_INFINITY) | ||
) | ||
GradientAngle.CW180 -> GradientOffset( | ||
start = Offset(Float.POSITIVE_INFINITY, 0f), | ||
end = Offset.Zero, | ||
) | ||
GradientAngle.CW225 -> GradientOffset( | ||
start = Offset.Infinite, | ||
end = Offset.Zero | ||
) | ||
GradientAngle.CW270 -> GradientOffset( | ||
start = Offset(0f, Float.POSITIVE_INFINITY), | ||
end = Offset.Zero | ||
) | ||
GradientAngle.CW315 -> GradientOffset( | ||
start = Offset(0f, Float.POSITIVE_INFINITY), | ||
end = Offset(Float.POSITIVE_INFINITY, 0f) | ||
) | ||
else -> GradientOffset( | ||
start = Offset.Zero, | ||
end = Offset(Float.POSITIVE_INFINITY, 0f) | ||
) | ||
} | ||
} | ||
|
||
/** | ||
* Offset for [Brush.linearGradient] to rotate gradient depending on [start] and [end] offsets. | ||
*/ | ||
data class GradientOffset(val start: Offset, val end: Offset) | ||
|
||
enum class GradientAngle { | ||
CW0, CW45, CW90, CW135, CW180, CW225, CW270, CW315 | ||
} |