RemoteColor expressions
Three colour expressions driven by time.
Open the live, interactive demo →
Change its controls, read the remote document it produces and the diff after every change.
What it is
Colours computed on the device: blends between two colours, rotating hues or changing transparency.
Good for
- Status colours that shift smoothly
- Ambient animated backgrounds
Code
The story's source with androidx.compose.remote imports. CI compiles every snippet on this site against AndroidX.
import androidx.compose.remote.creation.compose.layout.RemoteArrangement
import androidx.compose.remote.creation.compose.layout.RemoteBox
import androidx.compose.remote.creation.compose.layout.RemoteComposable
import androidx.compose.remote.creation.compose.layout.RemoteRow
import androidx.compose.remote.creation.compose.modifier.RemoteModifier
import androidx.compose.remote.creation.compose.modifier.background
import androidx.compose.remote.creation.compose.modifier.size
import androidx.compose.remote.creation.compose.state.RemoteColor
import androidx.compose.remote.creation.compose.state.RemoteTimeVariables
import androidx.compose.remote.creation.compose.state.lerp
import androidx.compose.remote.creation.compose.state.rc
import androidx.compose.remote.creation.compose.state.rdp
import androidx.compose.remote.creation.compose.state.rf
import androidx.compose.remote.creation.compose.state.sin
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
@Composable
@RemoteComposable
fun ColorExpressions(from: Color, to: Color, speed: Float) {
val t = (sin(RemoteTimeVariables.continuousSeconds * speed.rf) + 1f.rf) / 2f.rf
RemoteRow(horizontalArrangement = RemoteArrangement.spacedBy(10.rdp)) {
// lerp between two colors, by a time-driven fraction.
RemoteBox(RemoteModifier.size(90.rdp).background(lerp(from.rc, to.rc, t)))
// A hue that rotates with time: hue, saturation and value are all in 0..1.
RemoteBox(
RemoteModifier.size(90.rdp).background(RemoteColor.hsv(t, 0.8f.rf, 0.9f.rf)),
)
// A copy of a color with a computed alpha.
RemoteBox(RemoteModifier.size(90.rdp).background(from.rc.copy(alpha = t)))
}
}
API
creation.compose.state.RemoteColor
class RemoteColor extends BaseRemoteState<java.lang.Integer>
RemoteColor RemoteColor(Color color)
RemoteColor copy(optional RemoteFloat? alpha, optional RemoteFloat? red, optional RemoteFloat? green, optional RemoteFloat? blue)
RemoteColor createNamedRemoteColor(String name, RemoteColor defaultValue, optional RemoteState.Domain domain)
RemoteColor createNamedRemoteColor(String name, int defaultValue, optional RemoteState.Domain domain)
RemoteFloat alpha
RemoteFloat blue
RemoteFloat brightness
Integer constantValue
Integer? constantValueOrNull
RemoteFloat green
RemoteFloat hue
RemoteFloat red
RemoteFloat saturation
RemoteColor.Companion Companion
RemoteColor createNamedRemoteColor(String name, RemoteColor defaultValue, optional RemoteState.Domain domain)
RemoteColor createNamedRemoteColor(String name, int defaultValue, optional RemoteState.Domain domain)
RemoteColor hsv(RemoteFloat hue, RemoteFloat saturation, RemoteFloat value, optional RemoteFloat alpha)
RemoteColor rgb(RemoteFloat red, RemoteFloat green, RemoteFloat blue, optional RemoteFloat alpha)
RemoteColor Black
RemoteColor Blue
RemoteColor Cyan
RemoteColor DarkGray
RemoteColor Gray
RemoteColor Green
RemoteColor LightGray
RemoteColor Magenta
RemoteColor Red
RemoteColor Transparent
RemoteColor White
RemoteColor Yellow
RemoteColor RemoteColor(RemoteFloat red, RemoteFloat green, RemoteFloat blue, optional RemoteFloat alpha)
RemoteColor RemoteColor(int color)creation.compose.state.lerp
RemoteColor lerp(RemoteColor from, RemoteColor to, RemoteFloat fraction)