Home > database >  How to create Color using red, green and blue values? - Jetpack Compose
How to create Color using red, green and blue values? - Jetpack Compose

Time:09-24

I am trying to create a Color using RGB values but I am getting following error. I am unable to find a solution for it.

Color(148f, 148f, 148f, 1f)

Following is the error I am getting:

java.lang.IllegalArgumentException: red = 148.0, green = 148.0, blue = 148.0, alpha = 1.0 outside the range for sRGB IEC61966-2.1 (id=0, model=Rgb)
        at androidx.compose.ui.graphics.ColorKt.Color(Color.kt:405)
        at androidx.compose.ui.graphics.ColorKt.Color$default(Color.kt:398)
        at com.template.app.util.toast.CustomToast$successView$1$1.invoke(CustomToast.kt:86)
        at com.template.app.util.toast.CustomToast$successView$1$1.invoke(CustomToast.kt:83)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
        at androidx.compose.ui.platform.ComposeView.Content(ComposeView.android.kt:384)
        at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated$1.invoke(ComposeView.android.kt:228)
        at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated$1.invoke(ComposeView.android.kt:227)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
        at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:215)
        at androidx.compose.ui.platform.CompositionLocalsKt.ProvideCommonCompositionLocals(CompositionLocals.kt:148)
        at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt$ProvideAndroidCompositionLocals$3.invoke(AndroidCompositionLocals.android.kt:114)
        at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt$ProvideAndroidCompositionLocals$3.invoke(AndroidCompositionLocals.android.kt:113)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
        at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:215)
        at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt.ProvideAndroidCompositionLocals(AndroidCompositionLocals.android.kt:106)
        at androidx.compose.ui.platform.WrappedComposition$setContent$1$1$3.invoke(Wrapper.android.kt:162)
        at androidx.compose.ui.platform.WrappedComposition$setContent$1$1$3.invoke(Wrapper.android.kt:161)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
        at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:215)
        at androidx.compose.ui.platform.WrappedComposition$setContent$1$1.invoke(Wrapper.android.kt:161)
        at androidx.compose.ui.platform.WrappedComposition$setContent$1$1.invoke(Wrapper.android.kt:144)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
        at androidx.compose.runtime.ComposerKt.invokeComposable(Composer.kt:3330)
        at androidx.compose.runtime.ComposerImpl$doCompose$2$5.invoke(Composer.kt:2577)
        at androidx.compose.runtime.ComposerImpl$doCompose$2$5.invoke(Composer.kt:2573)
        at androidx.compose.runtime.SnapshotStateKt.observeDerivedStateRecalculations(SnapshotState.kt:540)
        at androidx.compose.runtime.ComposerImpl.doCompose(Composer.kt:2566)
        at androidx.compose.runtime.ComposerImpl.composeContent$runtime_release(Composer.kt:2517)
        at androidx.compose.runtime.CompositionImpl.composeContent(Composition.kt:476)
        at androidx.compose.runtime.Recomposer.composeInitial$runtime_release(Recomposer.kt:727)
        at androidx.compose.runtime.CompositionImpl.setContent(Composition.kt:432)
        at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:144)
        at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:135)

I checked the code of the Color class and found that I need to specify ColorSpace but I am unable to find the proper ColorSpace for my requirement.

CodePudding user response:

The constructor you're using expects floats in range 0..1.

Use Int values instead:

Color(148, 148, 148, 255)

CodePudding user response:

You need to use ColorSpaces.Srgb to specify values in 0..255 range. There is a very nice table in documentation here: https://developer.android.google.cn/reference/kotlin/androidx/compose/ui/graphics/Color

So basically you should use it like

Color(red = 148f, green = 148f, blue = 148f, alpha = 1f, ColorSpace.get(ColorSpaces.Srgb))
  • Related