├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── themes.xml │ │ │ │ └── colors.xml │ │ │ ├── mipmap-hdpi │ │ │ │ ├── ic_launcher.webp │ │ │ │ └── ic_launcher_round.webp │ │ │ ├── mipmap-mdpi │ │ │ │ ├── ic_launcher.webp │ │ │ │ └── ic_launcher_round.webp │ │ │ ├── mipmap-xhdpi │ │ │ │ ├── ic_launcher.webp │ │ │ │ └── ic_launcher_round.webp │ │ │ ├── mipmap-xxhdpi │ │ │ │ ├── ic_launcher.webp │ │ │ │ └── ic_launcher_round.webp │ │ │ ├── mipmap-xxxhdpi │ │ │ │ ├── ic_launcher.webp │ │ │ │ └── ic_launcher_round.webp │ │ │ ├── mipmap-anydpi-v26 │ │ │ │ ├── ic_launcher.xml │ │ │ │ └── ic_launcher_round.xml │ │ │ ├── drawable │ │ │ │ ├── ic_baseline_content_copy_24.xml │ │ │ │ └── ic_launcher_background.xml │ │ │ └── drawable-v24 │ │ │ │ └── ic_launcher_foreground.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── smarttoolfactory │ │ │ │ └── composecolorsextended │ │ │ │ ├── ui │ │ │ │ └── theme │ │ │ │ │ ├── Color.kt │ │ │ │ │ ├── Shape.kt │ │ │ │ │ ├── Type.kt │ │ │ │ │ └── Theme.kt │ │ │ │ ├── demo │ │ │ │ ├── M2ColorSelectionDemo.kt │ │ │ │ ├── M3ColorShadeSelectionDemo.kt │ │ │ │ ├── GradientAngleDemo.kt │ │ │ │ └── ColorModeConversionDemo.kt │ │ │ │ ├── MainActivity.kt │ │ │ │ ├── M2ListColorPicker.kt │ │ │ │ └── M3ColorPicker.kt │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── smarttoolfactory │ │ │ └── composecolorsextended │ │ │ └── ExampleUnitTest.kt │ └── androidTest │ │ └── java │ │ └── com │ │ └── smarttoolfactory │ │ └── composecolorsextended │ │ └── ExampleInstrumentedTest.kt ├── proguard-rules.pro └── build.gradle ├── extendedcolors ├── .gitignore ├── src │ └── main │ │ └── java │ │ └── com │ │ └── smarttoolfactory │ │ └── extendedcolors │ │ ├── parser │ │ ├── RGBData.kt │ │ └── ColorNameParser.kt │ │ ├── util │ │ ├── ColorSwatchUtil.kt │ │ ├── RoundngUtil.kt │ │ ├── HexUtil.kt │ │ ├── MaterialYouToneUtils.kt │ │ ├── HSLUtil.kt │ │ ├── HSVUtil.kt │ │ ├── ColorUtil.kt │ │ └── RGBUtil.kt │ │ ├── model │ │ └── ColorItem.kt │ │ ├── md3 │ │ ├── utils │ │ │ ├── StringUtils.java │ │ │ ├── MathUtils.java │ │ │ └── ColorUtils.java │ │ ├── palettes │ │ │ ├── CorePalette.java │ │ │ └── TonalPalette.java │ │ └── hct │ │ │ ├── Hct.java │ │ │ ├── ViewingConditions.java │ │ │ └── Cam16.java │ │ ├── GradientOffset.kt │ │ └── ColorSwatch.kt ├── proguard-rules.pro └── build.gradle ├── jitpack.yml ├── screenshots ├── m2_palette.png ├── m3_tones.png ├── gradient_rotation.gif ├── material_design2.gif └── material_design3.gif ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── gradle.properties ├── settings.gradle ├── gradlew.bat ├── gradlew └── LICENSE.md /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /extendedcolors/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /jitpack.yml: -------------------------------------------------------------------------------- 1 | jdk: 2 | - openjdk17 -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Compose Colors Extended 3 | -------------------------------------------------------------------------------- /screenshots/m2_palette.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartToolFactory/Compose-Extended-Colors/HEAD/screenshots/m2_palette.png -------------------------------------------------------------------------------- /screenshots/m3_tones.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartToolFactory/Compose-Extended-Colors/HEAD/screenshots/m3_tones.png -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartToolFactory/Compose-Extended-Colors/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /screenshots/gradient_rotation.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartToolFactory/Compose-Extended-Colors/HEAD/screenshots/gradient_rotation.gif -------------------------------------------------------------------------------- /screenshots/material_design2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartToolFactory/Compose-Extended-Colors/HEAD/screenshots/material_design2.gif -------------------------------------------------------------------------------- /screenshots/material_design3.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartToolFactory/Compose-Extended-Colors/HEAD/screenshots/material_design3.gif -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartToolFactory/Compose-Extended-Colors/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartToolFactory/Compose-Extended-Colors/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartToolFactory/Compose-Extended-Colors/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/* 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | .cxx 10 | local.properties 11 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartToolFactory/Compose-Extended-Colors/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartToolFactory/Compose-Extended-Colors/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartToolFactory/Compose-Extended-Colors/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartToolFactory/Compose-Extended-Colors/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartToolFactory/Compose-Extended-Colors/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartToolFactory/Compose-Extended-Colors/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SmartToolFactory/Compose-Extended-Colors/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /extendedcolors/src/main/java/com/smarttoolfactory/extendedcolors/parser/RGBData.kt: -------------------------------------------------------------------------------- 1 | package com.smarttoolfactory.extendedcolors.parser 2 | 3 | internal data class RGBData(val x: Int, val y: Int, val z: Int, val label: String) 4 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | kotlin.code.style=official 2 | 3 | org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 4 | org.gradle.parallel=true 5 | org.gradle.caching=true 6 | 7 | android.useAndroidX=true 8 | android.nonTransitiveRClass=true -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sat May 21 20:06:04 TRT 2022 2 | distributionBase=GRADLE_USER_HOME 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip 4 | distributionPath=wrapper/dists 5 | zipStorePath=wrapper/dists 6 | zipStoreBase=GRADLE_USER_HOME 7 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/java/com/smarttoolfactory/composecolorsextended/ui/theme/Color.kt: -------------------------------------------------------------------------------- 1 | package com.smarttoolfactory.composecolorsextended.ui.theme 2 | 3 | import androidx.compose.ui.graphics.Color 4 | 5 | val Purple200 = Color(0xFFBB86FC) 6 | val Purple500 = Color(0xFF6200EE) 7 | val Purple700 = Color(0xFF3700B3) 8 | val Teal200 = Color(0xFF03DAC5) -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/values/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | -------------------------------------------------------------------------------- /app/src/main/java/com/smarttoolfactory/composecolorsextended/ui/theme/Shape.kt: -------------------------------------------------------------------------------- 1 | package com.smarttoolfactory.composecolorsextended.ui.theme 2 | 3 | import androidx.compose.foundation.shape.RoundedCornerShape 4 | import androidx.compose.material.Shapes 5 | import androidx.compose.ui.unit.dp 6 | 7 | val Shapes = Shapes( 8 | small = RoundedCornerShape(4.dp), 9 | medium = RoundedCornerShape(4.dp), 10 | large = RoundedCornerShape(0.dp) 11 | ) -------------------------------------------------------------------------------- /app/src/main/java/com/smarttoolfactory/composecolorsextended/demo/M2ColorSelectionDemo.kt: -------------------------------------------------------------------------------- 1 | package com.smarttoolfactory.composecolorsextended.demo 2 | 3 | import androidx.compose.runtime.Composable 4 | import androidx.compose.ui.graphics.Color 5 | import com.smarttoolfactory.composecolorsextended.M2ListColorPicker 6 | 7 | @Composable 8 | fun M2ColorSelectionDemo(onColorChange: (Color) -> Unit) { 9 | M2ListColorPicker(onColorChange = onColorChange) 10 | } 11 | -------------------------------------------------------------------------------- /app/src/main/java/com/smarttoolfactory/composecolorsextended/demo/M3ColorShadeSelectionDemo.kt: -------------------------------------------------------------------------------- 1 | package com.smarttoolfactory.composecolorsextended.demo 2 | 3 | import androidx.compose.runtime.Composable 4 | import androidx.compose.ui.graphics.Color 5 | import com.smarttoolfactory.composecolorsextended.M3ColorPicker 6 | 7 | @Composable 8 | fun M3ColorShadeSelectionDemo(onColorChange: (Color) -> Unit) { 9 | M3ColorPicker(onColorChange = onColorChange) 10 | } 11 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #FFBB86FC 4 | #FF6200EE 5 | #FF3700B3 6 | #FF03DAC5 7 | #FF018786 8 | #FF000000 9 | #FFFFFFFF 10 | #F44336 11 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | pluginManagement { 2 | repositories { 3 | gradlePluginPortal() 4 | google() 5 | mavenCentral() 6 | } 7 | } 8 | dependencyResolutionManagement { 9 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) 10 | repositories { 11 | google() 12 | mavenCentral() 13 | maven { url 'https://jitpack.io' } 14 | } 15 | } 16 | rootProject.name = "Compose Colors Extended" 17 | include ':app' 18 | include ':extendedcolors' 19 | -------------------------------------------------------------------------------- /app/src/test/java/com/smarttoolfactory/composecolorsextended/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package com.smarttoolfactory.composecolorsextended 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_baseline_content_copy_24.xml: -------------------------------------------------------------------------------- 1 | 7 | 10 | 11 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /extendedcolors/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /app/src/androidTest/java/com/smarttoolfactory/composecolorsextended/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.smarttoolfactory.composecolorsextended 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.smarttoolfactory.composecolorsextended", appContext.packageName) 23 | } 24 | } -------------------------------------------------------------------------------- /app/src/main/java/com/smarttoolfactory/composecolorsextended/ui/theme/Type.kt: -------------------------------------------------------------------------------- 1 | package com.smarttoolfactory.composecolorsextended.ui.theme 2 | 3 | import androidx.compose.material.Typography 4 | import androidx.compose.ui.text.TextStyle 5 | import androidx.compose.ui.text.font.FontFamily 6 | import androidx.compose.ui.text.font.FontWeight 7 | import androidx.compose.ui.unit.sp 8 | 9 | // Set of Material typography styles to start with 10 | val Typography = Typography( 11 | body1 = TextStyle( 12 | fontFamily = FontFamily.Default, 13 | fontWeight = FontWeight.Normal, 14 | fontSize = 16.sp 15 | ) 16 | /* Other default text styles to override 17 | button = TextStyle( 18 | fontFamily = FontFamily.Default, 19 | fontWeight = FontWeight.W500, 20 | fontSize = 14.sp 21 | ), 22 | caption = TextStyle( 23 | fontFamily = FontFamily.Default, 24 | fontWeight = FontWeight.Normal, 25 | fontSize = 12.sp 26 | ) 27 | */ 28 | ) -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /extendedcolors/src/main/java/com/smarttoolfactory/extendedcolors/util/ColorSwatchUtil.kt: -------------------------------------------------------------------------------- 1 | package com.smarttoolfactory.extendedcolors.util 2 | 3 | import androidx.compose.ui.graphics.Color 4 | import kotlin.math.roundToInt 5 | 6 | /** 7 | * Creates Material Design2 color swatch from a [Color] 8 | */ 9 | fun createMaterialSwatch(color: Color): Map { 10 | 11 | val colorSwatch = linkedMapOf() 12 | 13 | val variants = mutableListOf(.05) 14 | for (i in 1 until 10) { 15 | variants.add(0.1 * i) 16 | } 17 | 18 | val red: Int = color.red.fractionToRGBRange() 19 | val green: Int = color.green.fractionToRGBRange() 20 | val blue: Int = color.blue.fractionToRGBRange() 21 | 22 | for (variant in variants) { 23 | val ds: Double = 0.5 - variant 24 | val newRed: Int = red + ((if (ds < 0) red else 255 - red) * ds).roundToInt() 25 | val newGreen: Int = green + ((if (ds < 0) green else 255 - green) * ds).roundToInt() 26 | val newBlue: Int = blue + ((if (ds < 0) blue else 255 - blue) * ds).roundToInt() 27 | 28 | colorSwatch[(variant * 1000).roundToInt()] = Color(newRed, newGreen, newBlue) 29 | } 30 | 31 | return colorSwatch 32 | } -------------------------------------------------------------------------------- /extendedcolors/src/main/java/com/smarttoolfactory/extendedcolors/model/ColorItem.kt: -------------------------------------------------------------------------------- 1 | package com.smarttoolfactory.extendedcolors.model 2 | 3 | import androidx.compose.ui.graphics.Color 4 | import com.smarttoolfactory.extendedcolors.util.* 5 | 6 | /** 7 | * Data class that wraps [Color] and contains extend information about this color such 8 | * as HSL, HSV, RGB, HEX counterparts. 9 | */ 10 | data class ColorItem(var color: Color) { 11 | val hexARGB 12 | get() = ColorUtil.colorToHexAlpha(color) 13 | 14 | val hex 15 | get() = ColorUtil.colorToHex(color) 16 | 17 | val hsvArray 18 | get() = ColorUtil.colorToHSV(color) 19 | 20 | val hslArray 21 | get() = ColorUtil.colorToHSL(color) 22 | 23 | val rgb 24 | get() = ColorUtil.colorToARGBArray(color) 25 | 26 | val alpha:Float 27 | get() = color.alpha 28 | 29 | val red: Int 30 | get() = color.red.fractionToRGBRange() 31 | 32 | val green: Int 33 | get() = color.green.fractionToRGBRange() 34 | 35 | val blue: Int 36 | get() = color.blue.fractionToRGBRange() 37 | 38 | var label: String = Unspecified 39 | 40 | companion object { 41 | const val Unspecified = "?????" 42 | } 43 | } -------------------------------------------------------------------------------- /extendedcolors/src/main/java/com/smarttoolfactory/extendedcolors/util/RoundngUtil.kt: -------------------------------------------------------------------------------- 1 | package com.smarttoolfactory.extendedcolors.util 2 | 3 | import kotlin.math.roundToInt 4 | 5 | /** 6 | * Converts alpha, red, green or blue values from range of [0f-1f] to [0-255]. 7 | */ 8 | fun Float.fractionToRGBRange() = (this * 255.0f).toInt() 9 | 10 | /** 11 | * Converts alpha, red, green or blue values from range of [0f-1f] to [0-255] and returns 12 | * it as [String]. 13 | */ 14 | fun Float.fractionToRGBString() = this.fractionToRGBRange().toString() 15 | 16 | /** 17 | * Rounds this [Float] to another with 2 significant numbers 18 | * 0.1234 is rounded to 0.12 19 | * 0.127 is rounded to 0.13 20 | */ 21 | fun Float.roundToTwoDigits() = (this * 100.0f).roundToInt() / 100.0f 22 | 23 | /** 24 | * Rounds this [Float] to closest int. 25 | */ 26 | fun Float.round() = this.roundToInt() 27 | 28 | /** 29 | * Converts **HSV** or **HSL** colors that are in range of [0f-1f] to [0-100] range in [Integer] 30 | * with [Float.roundToInt] 31 | */ 32 | fun Float.fractionToPercent() = (this * 100.0f).roundToInt() 33 | 34 | /** 35 | * Converts **HSV** or **HSL** colors that are in range of [0f-1f] to [0-100] range in [Integer] 36 | * with [Float.toInt] 37 | */ 38 | fun Float.fractionToIntPercent() = (this * 100.0f).toInt() 39 | -------------------------------------------------------------------------------- /extendedcolors/src/main/java/com/smarttoolfactory/extendedcolors/md3/utils/StringUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.smarttoolfactory.extendedcolors.md3.utils; 18 | 19 | /** Utility methods for string representations of colors. */ 20 | final class StringUtils { 21 | private StringUtils() {} 22 | 23 | /** 24 | * Hex string representing color, ex. #ff0000 for red. 25 | * 26 | * @param argb ARGB representation of a color. 27 | */ 28 | public static String hexFromArgb(int argb) { 29 | int red = ColorUtils.redFromArgb(argb); 30 | int blue = ColorUtils.blueFromArgb(argb); 31 | int green = ColorUtils.greenFromArgb(argb); 32 | return String.format("#%02x%02x%02x", red, green, blue); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /app/src/main/java/com/smarttoolfactory/composecolorsextended/ui/theme/Theme.kt: -------------------------------------------------------------------------------- 1 | package com.smarttoolfactory.composecolorsextended.ui.theme 2 | 3 | import androidx.compose.foundation.isSystemInDarkTheme 4 | import androidx.compose.material.MaterialTheme 5 | import androidx.compose.material.darkColors 6 | import androidx.compose.material.lightColors 7 | import androidx.compose.runtime.Composable 8 | 9 | private val DarkColorPalette = darkColors( 10 | primary = Purple200, 11 | primaryVariant = Purple700, 12 | secondary = Teal200 13 | ) 14 | 15 | private val LightColorPalette = lightColors( 16 | primary = Purple500, 17 | primaryVariant = Purple700, 18 | secondary = Teal200 19 | 20 | /* Other default colors to override 21 | background = Color.White, 22 | surface = Color.White, 23 | onPrimary = Color.White, 24 | onSecondary = Color.Black, 25 | onBackground = Color.Black, 26 | onSurface = Color.Black, 27 | */ 28 | ) 29 | 30 | @Composable 31 | fun ComposeColorsExtendedTheme( 32 | darkTheme: Boolean = isSystemInDarkTheme(), 33 | content: @Composable () -> Unit 34 | ) { 35 | val colors = if (darkTheme) { 36 | DarkColorPalette 37 | } else { 38 | LightColorPalette 39 | } 40 | 41 | MaterialTheme( 42 | colors = colors, 43 | typography = Typography, 44 | shapes = Shapes, 45 | content = content 46 | ) 47 | } -------------------------------------------------------------------------------- /extendedcolors/src/main/java/com/smarttoolfactory/extendedcolors/util/HexUtil.kt: -------------------------------------------------------------------------------- 1 | package com.smarttoolfactory.extendedcolors.util 2 | 3 | import androidx.compose.ui.graphics.Color 4 | import com.smarttoolfactory.extendedcolors.util.ColorUtil.colorIntToARGBArray 5 | import com.smarttoolfactory.extendedcolors.util.ColorUtil.colorIntToRGBArray 6 | 7 | object HexUtil { 8 | /* 9 | HEX-ColorInt Conversion 10 | */ 11 | fun hexToColorInt(colorString: String): Int { 12 | val completeColorString = if (colorString.first() == '#') colorString else "#$colorString" 13 | return android.graphics.Color.parseColor(completeColorString) 14 | } 15 | 16 | /* 17 | HEX-RGB Conversion 18 | */ 19 | fun hexToRGB(colorString: String): IntArray { 20 | val colorInt = hexToColorInt(colorString) 21 | return colorIntToRGBArray(colorInt) 22 | } 23 | 24 | fun hexToRGB(colorString: String, rgbIn: IntArray) { 25 | val colorInt = hexToColorInt(colorString) 26 | colorIntToRGBArray(colorInt, rgbIn) 27 | } 28 | 29 | fun hexToARGB(colorString: String): IntArray { 30 | val colorInt = hexToColorInt(colorString) 31 | return colorIntToARGBArray(colorInt) 32 | } 33 | 34 | fun hexToARGB(colorString: String, argbIn: IntArray) { 35 | val colorInt = hexToColorInt(colorString) 36 | colorIntToARGBArray(colorInt, argbIn) 37 | } 38 | 39 | 40 | fun hexToColor(colorString: String): Color { 41 | return Color(hexToColorInt(colorString)) 42 | } 43 | 44 | } -------------------------------------------------------------------------------- /extendedcolors/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.library' 3 | id 'org.jetbrains.kotlin.android' 4 | id 'maven-publish' 5 | } 6 | 7 | android { 8 | namespace "com.smarttoolfactory.extendedcolors" 9 | compileSdk 33 10 | defaultConfig { 11 | minSdk 21 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | compileOptions { 20 | sourceCompatibility JavaVersion.VERSION_17 21 | targetCompatibility JavaVersion.VERSION_17 22 | } 23 | kotlin { 24 | jvmToolchain(17) 25 | } 26 | buildFeatures { 27 | compose true 28 | } 29 | composeOptions { 30 | kotlinCompilerExtensionVersion '1.4.4' 31 | } 32 | packagingOptions { 33 | resources { 34 | excludes += '/META-INF/{AL2.0,LGPL2.1}' 35 | } 36 | } 37 | } 38 | 39 | afterEvaluate { 40 | publishing { 41 | publications { 42 | release(MavenPublication) { 43 | from components.release 44 | 45 | groupId = 'com.smarttoolfactory' 46 | artifactId = 'extendedcolors' 47 | version = '0.0.1' 48 | } 49 | } 50 | } 51 | } 52 | 53 | dependencies { 54 | implementation 'androidx.core:core-ktx:1.10.0' 55 | 56 | implementation "androidx.compose.ui:ui:$compose_version" 57 | implementation "androidx.compose.ui:ui-tooling:$compose_version" 58 | implementation "androidx.compose.runtime:runtime:$compose_version" 59 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /extendedcolors/src/main/java/com/smarttoolfactory/extendedcolors/md3/palettes/CorePalette.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.smarttoolfactory.extendedcolors.md3.palettes; 18 | 19 | 20 | import static java.lang.Math.max; 21 | 22 | import com.smarttoolfactory.extendedcolors.md3.hct.Hct; 23 | 24 | /** 25 | * An intermediate concept between the key color for a UI theme, and a full color scheme. 5 sets of 26 | * tones are generated, all except one use the same hue as the key color, and all vary in chroma. 27 | */ 28 | public final class CorePalette { 29 | public TonalPalette a1; 30 | public TonalPalette a2; 31 | public TonalPalette a3; 32 | public TonalPalette n1; 33 | public TonalPalette n2; 34 | public TonalPalette error; 35 | 36 | /** 37 | * Create key tones from a color. 38 | * 39 | * @param argb ARGB representation of a color 40 | */ 41 | public static CorePalette of(int argb) { 42 | return new CorePalette(argb); 43 | } 44 | 45 | private CorePalette(int argb) { 46 | Hct hct = Hct.fromInt(argb); 47 | double hue = hct.getHue(); 48 | this.a1 = TonalPalette.fromHueAndChroma(hue, max(48., hct.getChroma())); 49 | this.a2 = TonalPalette.fromHueAndChroma(hue, 16.); 50 | this.a3 = TonalPalette.fromHueAndChroma(hue + 60., 24.); 51 | this.n1 = TonalPalette.fromHueAndChroma(hue, 4.); 52 | this.n2 = TonalPalette.fromHueAndChroma(hue, 8.); 53 | this.error = TonalPalette.fromHueAndChroma(25, 84.); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /extendedcolors/src/main/java/com/smarttoolfactory/extendedcolors/util/MaterialYouToneUtils.kt: -------------------------------------------------------------------------------- 1 | package com.smarttoolfactory.extendedcolors.util 2 | 3 | import androidx.compose.ui.graphics.Color 4 | import androidx.compose.ui.graphics.toArgb 5 | import com.smarttoolfactory.extendedcolors.md3.hct.Cam16 6 | import com.smarttoolfactory.extendedcolors.md3.palettes.TonalPalette 7 | import kotlin.math.max 8 | 9 | val material3ToneRange = listOf( 10 | 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 95, 99, 100 11 | ) 12 | 13 | /** 14 | * Get 15 | * [Material Design3 Colors](https://m3.material.io/styles/color/the-color-system/key-colors-tones) 16 | * from [color] as Map that contains tone numbers as keys and [Color]s as values 17 | * between 0 and 100 tones in total 13 tones. 18 | * 19 | * 20 | */ 21 | fun getColorTonesMap(color: Color): Map { 22 | val palette: TonalPalette = TonalPalette.fromInt(color.toArgb()) 23 | val toneMap = linkedMapOf() 24 | 25 | material3ToneRange.forEach { shade -> 26 | toneMap[shade] = Color(palette.tone(shade)) 27 | } 28 | 29 | return toneMap 30 | } 31 | 32 | 33 | // TODO Material3 colors don't return correct values opened an issue 34 | /** 35 | * Get 36 | * [Material Design3 Colors](https://m3.material.io/styles/color/the-color-system/key-colors-tones) 37 | * from [color] as list between 0 and 100 tones in total 13 tones. 38 | * 39 | */ 40 | //fun getColorTonesList(color: Color): List< Color> { 41 | // 42 | // val palette: TonalPalette = TonalPalette.fromInt(color.toArgb()) 43 | // val toneList = mutableListOf() 44 | // 45 | // material3ToneRange.forEach { shade -> 46 | // toneList.add(Color(palette.tone(shade))) 47 | // } 48 | // 49 | // return toneList 50 | //} 51 | 52 | fun getColorTonesList(color: Color): List< Color> { 53 | 54 | val camColor = Cam16.fromInt(color.toArgb()) 55 | val palette: TonalPalette = TonalPalette.fromHueAndChroma(camColor.hue, max(48.0,camColor.chroma)) 56 | val toneList = mutableListOf() 57 | 58 | material3ToneRange.forEach { shade -> 59 | toneList.add(Color(palette.tone(shade))) 60 | } 61 | 62 | return toneList 63 | } -------------------------------------------------------------------------------- /extendedcolors/src/main/java/com/smarttoolfactory/extendedcolors/parser/ColorNameParser.kt: -------------------------------------------------------------------------------- 1 | package com.smarttoolfactory.extendedcolors.parser 2 | 3 | import androidx.compose.runtime.Composable 4 | import androidx.compose.runtime.remember 5 | import androidx.compose.ui.graphics.Color 6 | import com.smarttoolfactory.extendedcolors.model.ColorItem 7 | import com.smarttoolfactory.extendedcolors.util.* 8 | import kotlin.math.sqrt 9 | 10 | @Composable 11 | fun rememberColorParser(): ColorNameParser { 12 | return remember { 13 | ColorNameParser() 14 | } 15 | } 16 | 17 | /** 18 | * Parses color name from [Color] 19 | */ 20 | class ColorNameParser internal constructor() { 21 | 22 | private val rbgData: List by lazy { 23 | colorNameMap.map { entry: Map.Entry -> 24 | val rgbArray = HexUtil.hexToRGB(entry.key) 25 | val label = entry.value 26 | RGBData( 27 | x = rgbArray[0], 28 | y = rgbArray[1], 29 | z = rgbArray[2], 30 | label = label 31 | ) 32 | } 33 | } 34 | 35 | /** 36 | * Parse name of [Color] 37 | */ 38 | fun parseColorName(color: Color): String { 39 | val rgbArray = ColorUtil.colorToRGBArray(color) 40 | 41 | val red: Int = rgbArray[0] 42 | val green: Int = rgbArray[1] 43 | val blue: Int = rgbArray[2] 44 | 45 | var distance: Int=Int.MAX_VALUE 46 | 47 | var colorId = -1 48 | 49 | rbgData.forEachIndexed { index, rgbData -> 50 | val currentDistance = sqrt( 51 | ( 52 | (rgbData.x - red) * (rgbData.x - red) + 53 | (rgbData.y - green) * (rgbData.y - green) + 54 | (rgbData.z - blue) * (rgbData.z - blue) 55 | ).toDouble() 56 | ).toInt() 57 | 58 | if (currentDistance < distance) { 59 | distance = currentDistance 60 | colorId = index 61 | } 62 | } 63 | 64 | return if (colorId >= 0) { 65 | rbgData[colorId].label 66 | } else ColorItem.Unspecified 67 | 68 | } 69 | } -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.application' 3 | id 'org.jetbrains.kotlin.android' 4 | } 5 | 6 | android { 7 | compileSdk 31 8 | 9 | defaultConfig { 10 | applicationId "com.smarttoolfactory.composecolorsextended" 11 | minSdk 21 12 | targetSdk 31 13 | versionCode 1 14 | versionName "1.0" 15 | 16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 17 | vectorDrawables { 18 | useSupportLibrary true 19 | } 20 | } 21 | 22 | buildTypes { 23 | release { 24 | minifyEnabled false 25 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 26 | } 27 | } 28 | compileOptions { 29 | sourceCompatibility JavaVersion.VERSION_1_8 30 | targetCompatibility JavaVersion.VERSION_1_8 31 | } 32 | kotlinOptions { 33 | jvmTarget = '1.8' 34 | } 35 | buildFeatures { 36 | compose true 37 | } 38 | composeOptions { 39 | kotlinCompilerExtensionVersion compose_version 40 | } 41 | packagingOptions { 42 | resources { 43 | excludes += '/META-INF/{AL2.0,LGPL2.1}' 44 | } 45 | } 46 | } 47 | 48 | dependencies { 49 | 50 | implementation 'androidx.core:core-ktx:1.7.0' 51 | 52 | implementation 'com.github.SmartToolFactory:Compose-Extended-Colors:1.0.0-alpha07' 53 | 54 | implementation "androidx.compose.ui:ui:$compose_version" 55 | implementation "androidx.compose.material:material:$compose_version" 56 | implementation "androidx.compose.ui:ui-tooling-preview:$compose_version" 57 | 58 | def accompanist_version = "0.24.6-alpha" 59 | // Accompanist 60 | implementation "com.google.accompanist:accompanist-systemuicontroller:$accompanist_version" 61 | implementation "com.google.accompanist:accompanist-pager:$accompanist_version" 62 | implementation "com.google.accompanist:accompanist-drawablepainter:$accompanist_version" 63 | 64 | implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.1' 65 | implementation 'androidx.activity:activity-compose:1.4.0' 66 | testImplementation 'junit:junit:4.13.2' 67 | androidTestImplementation 'androidx.test.ext:junit:1.1.3' 68 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' 69 | androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version" 70 | debugImplementation "androidx.compose.ui:ui-tooling:$compose_version" 71 | } -------------------------------------------------------------------------------- /extendedcolors/src/main/java/com/smarttoolfactory/extendedcolors/md3/palettes/TonalPalette.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.smarttoolfactory.extendedcolors.md3.palettes; 18 | 19 | import com.smarttoolfactory.extendedcolors.md3.hct.Hct; 20 | 21 | import java.util.HashMap; 22 | import java.util.Map; 23 | 24 | 25 | /** 26 | * A convenience class for retrieving colors that are constant in hue and chroma, but vary in tone. 27 | */ 28 | public final class TonalPalette { 29 | Map cache; 30 | double hue; 31 | double chroma; 32 | 33 | /** 34 | * Create tones using the HCT hue and chroma from a color. 35 | * 36 | * @param argb ARGB representation of a color 37 | * @return Tones matching that color's hue and chroma. 38 | */ 39 | public static final TonalPalette fromInt(int argb) { 40 | Hct hct = Hct.fromInt(argb); 41 | return TonalPalette.fromHueAndChroma(hct.getHue(), hct.getChroma()); 42 | } 43 | 44 | /** 45 | * Create tones from a defined HCT hue and chroma. 46 | * 47 | * @param hue HCT hue 48 | * @param chroma HCT chroma 49 | * @return Tones matching hue and chroma. 50 | */ 51 | public static final TonalPalette fromHueAndChroma(double hue, double chroma) { 52 | return new TonalPalette(hue, chroma); 53 | } 54 | 55 | private TonalPalette(double hue, double chroma) { 56 | cache = new HashMap<>(); 57 | this.hue = hue; 58 | this.chroma = chroma; 59 | } 60 | 61 | /** 62 | * Create an ARGB color with HCT hue and chroma of this Tones instance, and the provided HCT tone. 63 | * 64 | * @param tone HCT tone, measured from 0 to 100. 65 | * @return ARGB representation of a color with that tone. 66 | */ 67 | // AndroidJdkLibsChecker is higher priority than ComputeIfAbsentUseValue (b/119581923) 68 | @SuppressWarnings("ComputeIfAbsentUseValue") 69 | public int tone(int tone) { 70 | Integer color = cache.get(tone); 71 | if (color == null) { 72 | color = Hct.from(this.hue, this.chroma, tone).toInt(); 73 | cache.put(tone, color); 74 | } 75 | return color; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /extendedcolors/src/main/java/com/smarttoolfactory/extendedcolors/GradientOffset.kt: -------------------------------------------------------------------------------- 1 | package com.smarttoolfactory.extendedcolors 2 | 3 | import androidx.compose.ui.geometry.Offset 4 | 5 | /** 6 | * 7 | * Get a [GradientOffset] that rotate a gradient clockwise with specified angle in degrees. 8 | * Default value for [GradientOffset] is [GradientAngle.CW0] which is 0 degrees 9 | * that returns a horizontal gradient. 10 | * 11 | * Get start and end offsets that are limited between [0f, Float.POSITIVE_INFINITY] in x and 12 | * y axes wrapped in [GradientOffset]. 13 | * Infinity is converted to Composable width on x axis, height on y axis in shader. 14 | * 15 | * Default angle for [Brush.linearGradient] when no offset is 0 degrees in Compose , 16 | * [Brush.verticalGradient] is [Brush.linearGradient] with 90 degrees. 17 | * 18 | * ``` 19 | * 0 degrees 20 | * start = Offset(0f,0f), 21 | * end = Offset(Float.POSITIVE_INFINITY,0f) 22 | * 23 | * 45 degrees 24 | * start = Offset(0f, Float.POSITIVE_INFINITY), 25 | * end = Offset(Float.POSITIVE_INFINITY, 0f) 26 | * 27 | * 90 degrees 28 | * start = Offset(0f, Float.POSITIVE_INFINITY), 29 | * end = Offset.Zero 30 | * 31 | * 135 degrees 32 | * start = Offset.Infinity, 33 | * end = Offset.Zero 34 | * 35 | * 180 degrees 36 | * start = Offset(Float.POSITIVE_INFINITY, 0f), 37 | * end = Offset.Zero, 38 | * 39 | * ``` 40 | */ 41 | fun GradientOffset(angle: GradientAngle = GradientAngle.CW0): GradientOffset { 42 | return when (angle) { 43 | GradientAngle.CW45 -> GradientOffset( 44 | start = Offset.Zero, 45 | end = Offset.Infinite 46 | ) 47 | GradientAngle.CW90 -> GradientOffset( 48 | start = Offset.Zero, 49 | end = Offset(0f, Float.POSITIVE_INFINITY) 50 | ) 51 | GradientAngle.CW135 -> GradientOffset( 52 | start = Offset(Float.POSITIVE_INFINITY, 0f), 53 | end = Offset(0f, Float.POSITIVE_INFINITY) 54 | ) 55 | GradientAngle.CW180 -> GradientOffset( 56 | start = Offset(Float.POSITIVE_INFINITY, 0f), 57 | end = Offset.Zero, 58 | ) 59 | GradientAngle.CW225 -> GradientOffset( 60 | start = Offset.Infinite, 61 | end = Offset.Zero 62 | ) 63 | GradientAngle.CW270 -> GradientOffset( 64 | start = Offset(0f, Float.POSITIVE_INFINITY), 65 | end = Offset.Zero 66 | ) 67 | GradientAngle.CW315 -> GradientOffset( 68 | start = Offset(0f, Float.POSITIVE_INFINITY), 69 | end = Offset(Float.POSITIVE_INFINITY, 0f) 70 | ) 71 | else -> GradientOffset( 72 | start = Offset.Zero, 73 | end = Offset(Float.POSITIVE_INFINITY, 0f) 74 | ) 75 | } 76 | } 77 | 78 | /** 79 | * Offset for [Brush.linearGradient] to rotate gradient depending on [start] and [end] offsets. 80 | */ 81 | data class GradientOffset(val start: Offset, val end: Offset) 82 | 83 | enum class GradientAngle { 84 | CW0, CW45, CW90, CW135, CW180, CW225, CW270, CW315 85 | } -------------------------------------------------------------------------------- /extendedcolors/src/main/java/com/smarttoolfactory/extendedcolors/md3/utils/MathUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file is automatically generated. Do not modify it. 18 | 19 | package com.smarttoolfactory.extendedcolors.md3.utils; 20 | 21 | /** Utility methods for mathematical operations. */ 22 | public class MathUtils { 23 | private MathUtils() {} 24 | 25 | /** 26 | * The signum function. 27 | * 28 | * @return 1 if num > 0, -1 if num < 0, and 0 if num = 0 29 | */ 30 | public static int signum(double num) { 31 | if (num < 0) { 32 | return -1; 33 | } else if (num == 0) { 34 | return 0; 35 | } else { 36 | return 1; 37 | } 38 | } 39 | 40 | /** 41 | * The linear interpolation function. 42 | * 43 | * @return start if amount = 0 and stop if amount = 1 44 | */ 45 | public static double lerp(double start, double stop, double amount) { 46 | return (1.0 - amount) * start + amount * stop; 47 | } 48 | 49 | /** 50 | * Clamps an integer between two integers. 51 | * 52 | * @return input when min <= input <= max, and either min or max otherwise. 53 | */ 54 | public static int clampInt(int min, int max, int input) { 55 | if (input < min) { 56 | return min; 57 | } else if (input > max) { 58 | return max; 59 | } 60 | 61 | return input; 62 | } 63 | 64 | /** 65 | * Clamps an integer between two floating-point numbers. 66 | * 67 | * @return input when min <= input <= max, and either min or max otherwise. 68 | */ 69 | public static double clampDouble(double min, double max, double input) { 70 | if (input < min) { 71 | return min; 72 | } else if (input > max) { 73 | return max; 74 | } 75 | 76 | return input; 77 | } 78 | 79 | /** 80 | * Sanitizes a degree measure as an integer. 81 | * 82 | * @return a degree measure between 0 (inclusive) and 360 (exclusive). 83 | */ 84 | public static int sanitizeDegreesInt(int degrees) { 85 | degrees = degrees % 360; 86 | if (degrees < 0) { 87 | degrees = degrees + 360; 88 | } 89 | return degrees; 90 | } 91 | 92 | /** 93 | * Sanitizes a degree measure as a floating-point number. 94 | * 95 | * @return a degree measure between 0.0 (inclusive) and 360.0 (exclusive). 96 | */ 97 | public static double sanitizeDegreesDouble(double degrees) { 98 | degrees = degrees % 360.0; 99 | if (degrees < 0) { 100 | degrees = degrees + 360.0; 101 | } 102 | return degrees; 103 | } 104 | 105 | /** Distance of two points on a circle, represented using degrees. */ 106 | public static double differenceDegrees(double a, double b) { 107 | return 180.0 - Math.abs(Math.abs(a - b) - 180.0); 108 | } 109 | 110 | /** Multiplies a 1x3 row vector with a 3x3 matrix. */ 111 | public static double[] matrixMultiply(double[] row, double[][] matrix) { 112 | double a = row[0] * matrix[0][0] + row[1] * matrix[0][1] + row[2] * matrix[0][2]; 113 | double b = row[0] * matrix[1][0] + row[1] * matrix[1][1] + row[2] * matrix[1][2]; 114 | double c = row[0] * matrix[2][0] + row[1] * matrix[2][1] + row[2] * matrix[2][2]; 115 | return new double[] {a, b, c}; 116 | } 117 | 118 | } 119 | 120 | -------------------------------------------------------------------------------- /app/src/main/java/com/smarttoolfactory/composecolorsextended/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.smarttoolfactory.composecolorsextended 2 | 3 | import android.os.Bundle 4 | import androidx.activity.ComponentActivity 5 | import androidx.activity.compose.setContent 6 | import androidx.compose.animation.ExperimentalAnimationApi 7 | import androidx.compose.foundation.layout.Column 8 | import androidx.compose.foundation.layout.fillMaxSize 9 | import androidx.compose.foundation.layout.offset 10 | import androidx.compose.material.* 11 | import androidx.compose.runtime.* 12 | import androidx.compose.ui.Modifier 13 | import androidx.compose.ui.graphics.Color 14 | import androidx.compose.ui.unit.dp 15 | import com.google.accompanist.pager.ExperimentalPagerApi 16 | import com.google.accompanist.pager.HorizontalPager 17 | import com.google.accompanist.pager.PagerState 18 | import com.google.accompanist.pager.rememberPagerState 19 | import com.google.accompanist.systemuicontroller.rememberSystemUiController 20 | import com.smarttoolfactory.composecolorsextended.demo.ColorModelConversionDemo 21 | import com.smarttoolfactory.composecolorsextended.demo.GradientAngleDemo 22 | import com.smarttoolfactory.composecolorsextended.demo.M2ColorSelectionDemo 23 | import com.smarttoolfactory.composecolorsextended.demo.M3ColorShadeSelectionDemo 24 | import com.smarttoolfactory.composecolorsextended.ui.theme.ComposeColorsExtendedTheme 25 | import com.smarttoolfactory.extendedcolors.util.ColorUtil 26 | import kotlinx.coroutines.launch 27 | 28 | @OptIn(ExperimentalPagerApi::class) 29 | class MainActivity : ComponentActivity() { 30 | 31 | override fun onCreate(savedInstanceState: Bundle?) { 32 | super.onCreate(savedInstanceState) 33 | setContent { 34 | ComposeColorsExtendedTheme { 35 | 36 | Modifier.offset() 37 | // A surface container using the 'background' color from the theme 38 | Surface( 39 | modifier = Modifier.fillMaxSize(), 40 | color = MaterialTheme.colors.background 41 | ) { 42 | Column(modifier = Modifier.fillMaxSize()) { 43 | HomeContent() 44 | } 45 | } 46 | } 47 | } 48 | } 49 | } 50 | 51 | @ExperimentalPagerApi 52 | @OptIn(ExperimentalAnimationApi::class) 53 | @Composable 54 | private fun HomeContent() { 55 | val systemUiController = rememberSystemUiController() 56 | 57 | val pagerState: PagerState = rememberPagerState(initialPage = 0) 58 | var backgroundColor by remember { mutableStateOf(Color(0xffF44336)) } 59 | val lightness = ColorUtil.colorToHSL(backgroundColor)[2] 60 | val contentColor = if (lightness < .6f) Color.White else Color.Black 61 | 62 | systemUiController.setStatusBarColor( 63 | color = backgroundColor 64 | ) 65 | 66 | val coroutineScope = rememberCoroutineScope() 67 | 68 | ScrollableTabRow( 69 | backgroundColor = backgroundColor, 70 | contentColor = contentColor, 71 | edgePadding = 8.dp, 72 | // Our selected tab is our current page 73 | selectedTabIndex = pagerState.currentPage, 74 | // Override the indicator, using the provided pagerTabIndicatorOffset modifier 75 | indicator = {} 76 | ) { 77 | // Add tabs for all of our pages 78 | tabList.forEachIndexed { index, title -> 79 | Tab( 80 | text = { Text(title) }, 81 | selected = pagerState.currentPage == index, 82 | onClick = { 83 | coroutineScope.launch { 84 | pagerState.animateScrollToPage(index) 85 | } 86 | } 87 | ) 88 | } 89 | } 90 | 91 | HorizontalPager( 92 | state = pagerState, 93 | count = tabList.size 94 | ) { page: Int -> 95 | 96 | when (page) { 97 | 0 -> M2ColorSelectionDemo { 98 | backgroundColor = it 99 | } 100 | 1 -> M3ColorShadeSelectionDemo { 101 | backgroundColor = it 102 | } 103 | 2 -> GradientAngleDemo() 104 | else -> ColorModelConversionDemo() 105 | } 106 | } 107 | } 108 | 109 | internal val tabList = 110 | listOf( 111 | "Material Design2", 112 | "Material You/3", 113 | "Gradient Angles", 114 | "Model Conversion", 115 | ) -------------------------------------------------------------------------------- /extendedcolors/src/main/java/com/smarttoolfactory/extendedcolors/md3/hct/Hct.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.smarttoolfactory.extendedcolors.md3.hct; 18 | 19 | import com.smarttoolfactory.extendedcolors.md3.utils.ColorUtils; 20 | 21 | 22 | /** 23 | * A color system built using CAM16 hue and chroma, and L* from L*a*b*. 24 | * 25 | *

Using L* creates a link between the color system, contrast, and thus accessibility. Contrast 26 | * ratio depends on relative luminance, or Y in the XYZ color space. L*, or perceptual luminance can 27 | * be calculated from Y. 28 | * 29 | *

Unlike Y, L* is linear to human perception, allowing trivial creation of accurate color tones. 30 | * 31 | *

Unlike contrast ratio, measuring contrast in L* is linear, and simple to calculate. A 32 | * difference of 40 in HCT tone guarantees a contrast ratio >= 3.0, and a difference of 50 33 | * guarantees a contrast ratio >= 4.5. 34 | */ 35 | 36 | 37 | /** 38 | * HCT, hue, chroma, and tone. A color system that provides a perceptually accurate color 39 | * measurement system that can also accurately render what colors will appear as in different 40 | * lighting environments. 41 | */ 42 | public final class Hct { 43 | private double hue; 44 | private double chroma; 45 | private double tone; 46 | private int argb; 47 | 48 | /** 49 | * Create an HCT color from hue, chroma, and tone. 50 | * 51 | * @param hue 0 <= hue < 360; invalid values are corrected. 52 | * @param chroma 0 <= chroma < ?; Informally, colorfulness. The color returned may be lower than 53 | * the requested chroma. Chroma has a different maximum for any given hue and tone. 54 | * @param tone 0 <= tone <= 100; invalid values are corrected. 55 | * @return HCT representation of a color in default viewing conditions. 56 | */ 57 | public static Hct from(double hue, double chroma, double tone) { 58 | int argb = CamSolver.solveToInt(hue, chroma, tone); 59 | return new Hct(argb); 60 | } 61 | 62 | /** 63 | * Create an HCT color from a color. 64 | * 65 | * @param argb ARGB representation of a color. 66 | * @return HCT representation of a color in default viewing conditions 67 | */ 68 | public static Hct fromInt(int argb) { 69 | return new Hct(argb); 70 | } 71 | 72 | private Hct(int argb) { 73 | setInternalState(argb); 74 | } 75 | 76 | public double getHue() { 77 | return hue; 78 | } 79 | 80 | public double getChroma() { 81 | return chroma; 82 | } 83 | 84 | public double getTone() { 85 | return tone; 86 | } 87 | 88 | public int toInt() { 89 | return argb; 90 | } 91 | 92 | /** 93 | * Set the hue of this color. Chroma may decrease because chroma has a different maximum for any 94 | * given hue and tone. 95 | * 96 | * @param newHue 0 <= newHue < 360; invalid values are corrected. 97 | */ 98 | public void setHue(double newHue) { 99 | setInternalState(CamSolver.solveToInt(newHue, chroma, tone)); 100 | } 101 | 102 | /** 103 | * Set the chroma of this color. Chroma may decrease because chroma has a different maximum for 104 | * any given hue and tone. 105 | * 106 | * @param newChroma 0 <= newChroma < ? 107 | */ 108 | public void setChroma(double newChroma) { 109 | setInternalState(CamSolver.solveToInt(hue, newChroma, tone)); 110 | } 111 | 112 | /** 113 | * Set the tone of this color. Chroma may decrease because chroma has a different maximum for any 114 | * given hue and tone. 115 | * 116 | * @param newTone 0 <= newTone <= 100; invalid valids are corrected. 117 | */ 118 | public void setTone(double newTone) { 119 | setInternalState(CamSolver.solveToInt(hue, chroma, newTone)); 120 | } 121 | 122 | private void setInternalState(int argb) { 123 | this.argb = argb; 124 | Cam16 cam = Cam16.fromInt(argb); 125 | hue = cam.getHue(); 126 | chroma = cam.getChroma(); 127 | this.tone = ColorUtils.lstarFromArgb(argb); 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /app/src/main/java/com/smarttoolfactory/composecolorsextended/demo/GradientAngleDemo.kt: -------------------------------------------------------------------------------- 1 | package com.smarttoolfactory.composecolorsextended.demo 2 | 3 | import androidx.compose.foundation.Canvas 4 | import androidx.compose.foundation.layout.* 5 | import androidx.compose.material.Slider 6 | import androidx.compose.material.Text 7 | import androidx.compose.runtime.* 8 | import androidx.compose.ui.Alignment 9 | import androidx.compose.ui.Modifier 10 | import androidx.compose.ui.graphics.Brush 11 | import androidx.compose.ui.graphics.Color 12 | import androidx.compose.ui.graphics.drawscope.DrawScope 13 | import androidx.compose.ui.text.font.FontWeight 14 | import androidx.compose.ui.unit.dp 15 | import androidx.compose.ui.unit.sp 16 | import com.smarttoolfactory.extendedcolors.GradientAngle 17 | import com.smarttoolfactory.extendedcolors.GradientOffset 18 | import com.smarttoolfactory.extendedcolors.MaterialColor.Blue400 19 | import kotlin.math.roundToInt 20 | 21 | /** 22 | * Demo for creating gradients for different type of pickers or sliders 23 | */ 24 | @Composable 25 | fun GradientAngleDemo() { 26 | 27 | val canvasModifier = Modifier.size(300.dp) 28 | 29 | // Offsets for gradients based on selected angle 30 | var gradientOffset by remember { 31 | mutableStateOf(GradientOffset(GradientAngle.CW0)) 32 | } 33 | 34 | var angleSelection by remember { mutableStateOf(0f) } 35 | var angleText by remember { mutableStateOf("0 Degrees") } 36 | 37 | 38 | 39 | Column( 40 | modifier = Modifier 41 | .fillMaxSize() 42 | .padding(8.dp), 43 | horizontalAlignment = Alignment.CenterHorizontally 44 | ) { 45 | 46 | Text( 47 | text = angleText, 48 | color = Blue400, 49 | modifier = Modifier 50 | .padding(8.dp), 51 | fontSize = 18.sp, 52 | fontWeight = FontWeight.Bold 53 | ) 54 | 55 | Slider( 56 | modifier = Modifier.height(50.dp), 57 | value = angleSelection, 58 | onValueChange = { 59 | angleSelection = it 60 | 61 | gradientOffset = when (angleSelection.roundToInt()) { 62 | 0 -> { 63 | angleText = "0 Degrees" 64 | GradientOffset(GradientAngle.CW0) 65 | } 66 | 1 -> { 67 | angleText = "45 Degrees" 68 | GradientOffset(GradientAngle.CW45) 69 | } 70 | 2 -> { 71 | angleText = "90 Degrees" 72 | GradientOffset(GradientAngle.CW90) 73 | } 74 | 3 -> { 75 | angleText = "135 Degrees" 76 | GradientOffset(GradientAngle.CW135) 77 | } 78 | 4 -> { 79 | angleText = "180 Degrees" 80 | GradientOffset(GradientAngle.CW180) 81 | } 82 | 83 | 5 -> { 84 | angleText = "225 Degrees" 85 | GradientOffset(GradientAngle.CW225) 86 | } 87 | 6 -> { 88 | angleText = "270 Degrees" 89 | GradientOffset(GradientAngle.CW270) 90 | } 91 | else -> { 92 | angleText = "315 Degrees" 93 | GradientOffset(GradientAngle.CW315) 94 | } 95 | } 96 | }, 97 | steps = 6, 98 | valueRange = 0f..7f 99 | ) 100 | 101 | CanvasWithTitle( 102 | modifier = canvasModifier, 103 | text = "Gradient Angle" 104 | ) { 105 | val redGreenGradient = Brush.linearGradient( 106 | colors = listOf(Color.Red, Color.Green, Color.Blue), 107 | start = gradientOffset.start, 108 | end = gradientOffset.end 109 | ) 110 | drawRect(redGreenGradient) 111 | } 112 | 113 | } 114 | } 115 | 116 | @Composable 117 | private fun CanvasWithTitle( 118 | modifier: Modifier = Modifier, 119 | text: String, 120 | onDraw: DrawScope.() -> Unit 121 | ) { 122 | Column( 123 | modifier = Modifier 124 | .wrapContentWidth() 125 | ) { 126 | 127 | Text( 128 | text = text, 129 | color = Blue400, 130 | modifier = Modifier 131 | .padding(8.dp), 132 | fontSize = 18.sp, 133 | fontWeight = FontWeight.Bold 134 | ) 135 | 136 | Canvas(modifier = modifier, onDraw = onDraw) 137 | } 138 | } -------------------------------------------------------------------------------- /app/src/main/java/com/smarttoolfactory/composecolorsextended/M2ListColorPicker.kt: -------------------------------------------------------------------------------- 1 | package com.smarttoolfactory.composecolorsextended 2 | 3 | import androidx.compose.foundation.background 4 | import androidx.compose.foundation.clickable 5 | import androidx.compose.foundation.layout.* 6 | import androidx.compose.foundation.lazy.LazyColumn 7 | import androidx.compose.foundation.lazy.itemsIndexed 8 | import androidx.compose.foundation.shape.CircleShape 9 | import androidx.compose.foundation.shape.RoundedCornerShape 10 | import androidx.compose.material.Divider 11 | import androidx.compose.material.Text 12 | import androidx.compose.runtime.* 13 | import androidx.compose.ui.Alignment 14 | import androidx.compose.ui.Modifier 15 | import androidx.compose.ui.draw.clip 16 | import androidx.compose.ui.draw.shadow 17 | import androidx.compose.ui.graphics.Color 18 | import androidx.compose.ui.graphics.graphicsLayer 19 | import androidx.compose.ui.text.font.FontWeight 20 | import androidx.compose.ui.unit.dp 21 | import androidx.compose.ui.unit.sp 22 | import com.smarttoolfactory.extendedcolors.ColorSwatch 23 | import com.smarttoolfactory.extendedcolors.util.ColorUtil 24 | 25 | @Composable 26 | fun M2ListColorPicker(onColorChange: (Color) -> Unit) { 27 | 28 | var headerIndex by remember { mutableStateOf(0) } 29 | var selectedColorIndex by remember { mutableStateOf(-1) } 30 | 31 | Row(modifier = Modifier.fillMaxSize()) { 32 | LazyColumn( 33 | verticalArrangement = Arrangement.spacedBy(8.dp), 34 | contentPadding = PaddingValues(horizontal = 8.dp, vertical = 8.dp) 35 | ) { 36 | itemsIndexed(ColorSwatch.primaryHeaderColors) { index: Int, item: Color -> 37 | ColorDisplay( 38 | modifier = Modifier 39 | .padding(horizontal = 2.dp) 40 | .clip(CircleShape) 41 | .size(60.dp) 42 | .clickable { 43 | headerIndex = index 44 | }, color = item 45 | ) 46 | } 47 | } 48 | 49 | val colorSwatch: LinkedHashMap = ColorSwatch.primaryColorSwatches[headerIndex] 50 | 51 | val keys: MutableList = colorSwatch.keys.toMutableList() 52 | val colors: MutableList = colorSwatch.values.toMutableList() 53 | 54 | val result: Result> = 55 | runCatching { ColorSwatch.accentColorSwatches[headerIndex] } 56 | 57 | if (result.isSuccess) { 58 | result.getOrNull()?.let { accentColorSwatch: LinkedHashMap -> 59 | val accentKeys = accentColorSwatch.keys.toList() 60 | val accentColors = accentColorSwatch.values.toList() 61 | keys.addAll(accentKeys) 62 | colors.addAll(accentColors) 63 | } 64 | } 65 | Divider( 66 | modifier = Modifier 67 | .fillMaxHeight() 68 | .width(1.dp) 69 | .background(Color.LightGray) 70 | ) 71 | 72 | LazyColumn( 73 | modifier = Modifier.fillMaxWidth(), 74 | verticalArrangement = Arrangement.spacedBy(10.dp), 75 | contentPadding = PaddingValues(horizontal = 10.dp, vertical = 8.dp) 76 | ) { 77 | itemsIndexed(colors) { index: Int, item: Color -> 78 | Column { 79 | if (index == 0 || index == 10) { 80 | Text( 81 | modifier = Modifier.padding(8.dp), 82 | text = if (index == 0) "Primary" else "Accent", 83 | fontSize = 24.sp, 84 | fontWeight = FontWeight.Bold 85 | ) 86 | } 87 | ColorRowWithInfo( 88 | modifier = 89 | Modifier 90 | .graphicsLayer { 91 | scaleY = if (selectedColorIndex == index) 1.03f else 1f 92 | scaleX = if (selectedColorIndex == index) 1.03f else 1f 93 | } 94 | .shadow(2.dp, RoundedCornerShape(4.dp)) 95 | .fillMaxWidth() 96 | .clickable { 97 | selectedColorIndex = index 98 | onColorChange(item) 99 | }, 100 | title = keys[index].toString(), 101 | color = item, 102 | textColor = if (index < 5 || index > 9) Color.Black else Color.White 103 | ) 104 | } 105 | } 106 | } 107 | } 108 | } 109 | 110 | @Composable 111 | fun ColorDisplay(modifier: Modifier, color: Color) { 112 | Box( 113 | modifier = modifier 114 | .background(color) 115 | ) 116 | } 117 | 118 | @Composable 119 | fun ColorRowWithInfo( 120 | modifier: Modifier, 121 | title: String, 122 | color: Color, 123 | textColor: Color 124 | ) { 125 | Row( 126 | modifier 127 | .background(color) 128 | .padding(16.dp), 129 | verticalAlignment = Alignment.CenterVertically 130 | ) { 131 | Text(text = title, color = textColor, fontSize = 22.sp) 132 | Spacer(modifier = Modifier.weight(1f)) 133 | Text( 134 | text = ColorUtil.colorToHex(color), 135 | color = textColor, 136 | fontSize = 22.sp, 137 | fontWeight = FontWeight.Bold 138 | ) 139 | } 140 | } -------------------------------------------------------------------------------- /extendedcolors/src/main/java/com/smarttoolfactory/extendedcolors/util/HSLUtil.kt: -------------------------------------------------------------------------------- 1 | package com.smarttoolfactory.extendedcolors.util 2 | 3 | import androidx.compose.ui.graphics.Color 4 | import androidx.core.graphics.ColorUtils 5 | import com.smarttoolfactory.extendedcolors.util.ColorUtil.colorIntToRGBArray 6 | 7 | object HSLUtil { 8 | 9 | /** 10 | * Convert HSL components(hue-saturation-lightness) to HSV 11 | * (hue-saturation-value) components. 12 | * @param hue in [0..360f] 13 | * @param saturation in [0..1f] 14 | * @param lightness in [0..1f] 15 | */ 16 | fun hslToHSV( 17 | hue: Float, 18 | saturation: Float, 19 | lightness: Float 20 | ): FloatArray { 21 | val value = lightness + saturation * lightness.coerceAtMost(1 - lightness) 22 | val saturationHSV = if (value == 0f) 0f else 2 * (1 - lightness / value) 23 | return floatArrayOf(hue, saturationHSV.coerceIn(0f, 1f), value.coerceIn(0f, 1f)) 24 | } 25 | 26 | /** 27 | * Convert HSL components(hue-saturation-lightness) to HSV 28 | * (hue-saturation-value) components. 29 | * 30 | * ``` 31 | * hsv[0] is Hue [0 .. 360) 32 | * hsv[1] is Saturation [0...1] 33 | * hsv[2] is Value [0...1] 34 | * ``` 35 | * ``` 36 | * hsl[0] is Hue [0 .. 360) 37 | * hsl[1] is Saturation [0...1] 38 | * hsl[2] is Lightness [0...1] 39 | * ``` 40 | */ 41 | fun hslToHSV(hslIn: FloatArray): FloatArray { 42 | return hslToHSV(hslIn[0], hslIn[1], hslIn[2]) 43 | } 44 | 45 | /* 46 | HSL-ColorInt Conversions 47 | */ 48 | 49 | /** 50 | * Convert HSL (hue-saturation-lightness) components to a RGB color in [Integer] format. 51 | * 52 | * For instance, red =255, green =0, blue=0 is -65536 53 | * ``` 54 | * hsl[0] is Hue [0 .. 360) 55 | * hsl[1] is Saturation [0...1] 56 | * hsl[2] is Lightness [0...1] 57 | * ``` 58 | */ 59 | fun hslToColorInt(hslIn: FloatArray): Int { 60 | return hslToColorInt(hslIn[0], hslIn[1], hslIn[2]) 61 | } 62 | 63 | /** 64 | * Convert HSL (hue-saturation-lightness) components to a RGB color in [Integer] format. 65 | * @param hue in [0..360f] 66 | * @param saturation in [0..1f] 67 | * @param lightness in [0..1f] 68 | */ 69 | fun hslToColorInt( 70 | hue: Float, 71 | saturation: Float, 72 | lightness: Float 73 | ): Int { 74 | return ColorUtils.HSLToColor(floatArrayOf(hue, saturation, lightness)) 75 | } 76 | 77 | 78 | /* 79 | HSL-RGB Conversions 80 | */ 81 | 82 | /** 83 | * Convert HSL (hue-saturation-lightness) components to a RGB red, green blue array. 84 | * ``` 85 | * hsl[0] is Hue [0 .. 360) 86 | * hsl[1] is Saturation [0...1] 87 | * hsl[2] is Lightness [0...1] 88 | * ``` 89 | * ``` 90 | * rgb[0] is Red [0 .. 255] 91 | * rgb[1] is Green [0...255] 92 | * rgb[2] is Blue [0...255] 93 | * ``` 94 | * @param hslIn 3 element array which holds the input HSL components. 95 | */ 96 | fun hslToRGB(hslIn: FloatArray): IntArray { 97 | return colorIntToRGBArray(hslToColorInt(hslIn)) 98 | } 99 | 100 | /** 101 | * Convert HSL (hue-saturation-lightness) components to a RGB red, green blue array. 102 | * ``` 103 | * Hue is [0 .. 360) 104 | * Saturation is [0...1] 105 | * Lightness is [0...1] 106 | * ``` 107 | * 108 | * ``` 109 | * rgb[0] is Red [0 .. 255] 110 | * rgb[1] is Green [0...255] 111 | * rgb[2] is Blue [0...255] 112 | * ``` 113 | */ 114 | fun hslToRGB( 115 | hue: Float, 116 | saturation: Float, 117 | lightness: Float 118 | ): IntArray { 119 | return colorIntToRGBArray( 120 | color = hslToColorInt(hue, saturation, lightness) 121 | ) 122 | } 123 | 124 | /** 125 | * Convert HSL (hue-saturation-lightness) components to a RGB red, green blue array. 126 | * ``` 127 | * rgb[0] is Red [0 .. 255] 128 | * rgb[1] is Green [0...255] 129 | * rgb[2] is Blue [0...255] 130 | * ``` 131 | * @param hue in [0..360f] 132 | * @param saturation in [0..1f] 133 | * @param lightness in [0..1f] 134 | * @param rgbIn 3 element array which holds the input RGB components. 135 | */ 136 | fun hslToRGB( 137 | hue: Float, 138 | saturation: Float, 139 | lightness: Float, 140 | rgbIn: IntArray 141 | ) { 142 | colorIntToRGBArray(hslToColorInt(hue, saturation, lightness), rgbIn) 143 | } 144 | 145 | 146 | /** 147 | * Convert HSL (hue-saturation-lightness) to RGB red, green, blue components in [0f..1f] range. 148 | * ``` 149 | * rgb[0] is Red [0f .. 1f) 150 | * rgb[1] is Green [0f...1f] 151 | * rgb[2] is Blue [0f...1f] 152 | * ``` 153 | * @param hue in [0..360f] 154 | * @param saturation in [0..1f] 155 | * @param lightness in [0..1f] 156 | * 157 | * @return 3 element array which holds the output RGB components. 158 | */ 159 | fun hslToRGBFloat( 160 | hue: Float, 161 | saturation: Float, 162 | lightness: Float 163 | ): FloatArray { 164 | val color = Color.hsl(hue, saturation, lightness) 165 | return floatArrayOf(color.red, color.green, color.blue) 166 | } 167 | 168 | /** 169 | * Convert HSL (hue-saturation-lightness) to RGB red, green, blue components in [0f..1f] range. 170 | * ``` 171 | * rgb[0] is Red [0f .. 1f) 172 | * rgb[1] is Green [0f...1f] 173 | * rgb[2] is Blue [0f...1f] 174 | * ``` 175 | * @param hue in [0..360f] 176 | * @param saturation in [0..1f] 177 | * @param lightness in [0..1f] 178 | * @param rgbIn 3 element array which holds the output RGB components. 179 | */ 180 | fun hslToRGBFloat( 181 | hue: Float, 182 | saturation: Float, 183 | lightness: Float, 184 | rgbIn: FloatArray 185 | ) { 186 | val color = Color.hsl(hue, saturation, lightness) 187 | rgbIn[0] = color.red 188 | rgbIn[1] = color.green 189 | rgbIn[2] = color.blue 190 | } 191 | 192 | 193 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | 86 | # Determine the Java command to use to start the JVM. 87 | if [ -n "$JAVA_HOME" ] ; then 88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 89 | # IBM's JDK on AIX uses strange locations for the executables 90 | JAVACMD="$JAVA_HOME/jre/sh/java" 91 | else 92 | JAVACMD="$JAVA_HOME/bin/java" 93 | fi 94 | if [ ! -x "$JAVACMD" ] ; then 95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 96 | 97 | Please set the JAVA_HOME variable in your environment to match the 98 | location of your Java installation." 99 | fi 100 | else 101 | JAVACMD="java" 102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 103 | 104 | Please set the JAVA_HOME variable in your environment to match the 105 | location of your Java installation." 106 | fi 107 | 108 | # Increase the maximum file descriptors if we can. 109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 110 | MAX_FD_LIMIT=`ulimit -H -n` 111 | if [ $? -eq 0 ] ; then 112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 113 | MAX_FD="$MAX_FD_LIMIT" 114 | fi 115 | ulimit -n $MAX_FD 116 | if [ $? -ne 0 ] ; then 117 | warn "Could not set maximum file descriptor limit: $MAX_FD" 118 | fi 119 | else 120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 121 | fi 122 | fi 123 | 124 | # For Darwin, add options to specify how the application appears in the dock 125 | if $darwin; then 126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 127 | fi 128 | 129 | # For Cygwin or MSYS, switch paths to Windows format before running java 130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then 131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 133 | 134 | JAVACMD=`cygpath --unix "$JAVACMD"` 135 | 136 | # We build the pattern for arguments to be converted via cygpath 137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 138 | SEP="" 139 | for dir in $ROOTDIRSRAW ; do 140 | ROOTDIRS="$ROOTDIRS$SEP$dir" 141 | SEP="|" 142 | done 143 | OURCYGPATTERN="(^($ROOTDIRS))" 144 | # Add a user-defined pattern to the cygpath arguments 145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 147 | fi 148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 149 | i=0 150 | for arg in "$@" ; do 151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 153 | 154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 156 | else 157 | eval `echo args$i`="\"$arg\"" 158 | fi 159 | i=`expr $i + 1` 160 | done 161 | case $i in 162 | 0) set -- ;; 163 | 1) set -- "$args0" ;; 164 | 2) set -- "$args0" "$args1" ;; 165 | 3) set -- "$args0" "$args1" "$args2" ;; 166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;; 167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 172 | esac 173 | fi 174 | 175 | # Escape application args 176 | save () { 177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 178 | echo " " 179 | } 180 | APP_ARGS=`save "$@"` 181 | 182 | # Collect all arguments for the java command, following the shell quoting and substitution rules 183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 184 | 185 | exec "$JAVACMD" "$@" 186 | -------------------------------------------------------------------------------- /extendedcolors/src/main/java/com/smarttoolfactory/extendedcolors/md3/hct/ViewingConditions.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.smarttoolfactory.extendedcolors.md3.hct; 18 | 19 | 20 | import com.smarttoolfactory.extendedcolors.md3.utils.ColorUtils; 21 | import com.smarttoolfactory.extendedcolors.md3.utils.MathUtils; 22 | 23 | /** 24 | * In traditional color spaces, a color can be identified solely by the observer's measurement of 25 | * the color. Color appearance models such as CAM16 also use information about the environment where 26 | * the color was observed, known as the viewing conditions. 27 | * 28 | *

For example, white under the traditional assumption of a midday sun white point is accurately 29 | * measured as a slightly chromatic blue by CAM16. (roughly, hue 203, chroma 3, lightness 100) 30 | * 31 | *

This class caches intermediate values of the CAM16 conversion process that depend only on 32 | * viewing conditions, enabling speed ups. 33 | */ 34 | public final class ViewingConditions { 35 | /** sRGB-like viewing conditions. */ 36 | public static final ViewingConditions DEFAULT = 37 | ViewingConditions.make( 38 | new double[] { 39 | ColorUtils.whitePointD65()[0], 40 | ColorUtils.whitePointD65()[1], 41 | ColorUtils.whitePointD65()[2] 42 | }, 43 | (200.0 / Math.PI * ColorUtils.yFromLstar(50.0) / 100.f), 44 | 50.0, 45 | 2.0, 46 | false); 47 | 48 | private final double aw; 49 | private final double nbb; 50 | private final double ncb; 51 | private final double c; 52 | private final double nc; 53 | private final double n; 54 | private final double[] rgbD; 55 | private final double fl; 56 | private final double flRoot; 57 | private final double z; 58 | 59 | public double getAw() { 60 | return aw; 61 | } 62 | 63 | public double getN() { 64 | return n; 65 | } 66 | 67 | public double getNbb() { 68 | return nbb; 69 | } 70 | 71 | double getNcb() { 72 | return ncb; 73 | } 74 | 75 | double getC() { 76 | return c; 77 | } 78 | 79 | double getNc() { 80 | return nc; 81 | } 82 | 83 | public double[] getRgbD() { 84 | return rgbD; 85 | } 86 | 87 | double getFl() { 88 | return fl; 89 | } 90 | 91 | public double getFlRoot() { 92 | return flRoot; 93 | } 94 | 95 | double getZ() { 96 | return z; 97 | } 98 | 99 | /** 100 | * Create ViewingConditions from a simple, physically relevant, set of parameters. 101 | * 102 | * @param whitePoint White point, measured in the XYZ color space. default = D65, or sunny day 103 | * afternoon 104 | * @param adaptingLuminance The luminance of the adapting field. Informally, how bright it is in 105 | * the room where the color is viewed. Can be calculated from lux by multiplying lux by 106 | * 0.0586. default = 11.72, or 200 lux. 107 | * @param backgroundLstar The lightness of the area surrounding the color. measured by L* in 108 | * L*a*b*. default = 50.0 109 | * @param surround A general description of the lighting surrounding the color. 0 is pitch dark, 110 | * like watching a movie in a theater. 1.0 is a dimly light room, like watching TV at home at 111 | * night. 2.0 means there is no difference between the lighting on the color and around it. 112 | * default = 2.0 113 | * @param discountingIlluminant Whether the eye accounts for the tint of the ambient lighting, 114 | * such as knowing an apple is still red in green light. default = false, the eye does not 115 | * perform this process on self-luminous objects like displays. 116 | */ 117 | static ViewingConditions make( 118 | double[] whitePoint, 119 | double adaptingLuminance, 120 | double backgroundLstar, 121 | double surround, 122 | boolean discountingIlluminant) { 123 | // Transform white point XYZ to 'cone'/'rgb' responses 124 | double[][] matrix = Cam16.XYZ_TO_CAM16RGB; 125 | double[] xyz = whitePoint; 126 | double rW = (xyz[0] * matrix[0][0]) + (xyz[1] * matrix[0][1]) + (xyz[2] * matrix[0][2]); 127 | double gW = (xyz[0] * matrix[1][0]) + (xyz[1] * matrix[1][1]) + (xyz[2] * matrix[1][2]); 128 | double bW = (xyz[0] * matrix[2][0]) + (xyz[1] * matrix[2][1]) + (xyz[2] * matrix[2][2]); 129 | double f = 0.8 + (surround / 10.0); 130 | double c = 131 | (f >= 0.9) 132 | ? MathUtils.lerp(0.59, 0.69, ((f - 0.9) * 10.0)) 133 | : MathUtils.lerp(0.525, 0.59, ((f - 0.8) * 10.0)); 134 | double d = 135 | discountingIlluminant 136 | ? 1.0 137 | : f * (1.0 - ((1.0 / 3.6) * Math.exp((-adaptingLuminance - 42.0) / 92.0))); 138 | d = MathUtils.clampDouble(0.0, 1.0, d); 139 | double nc = f; 140 | double[] rgbD = 141 | new double[] { 142 | d * (100.0 / rW) + 1.0 - d, d * (100.0 / gW) + 1.0 - d, d * (100.0 / bW) + 1.0 - d 143 | }; 144 | double k = 1.0 / (5.0 * adaptingLuminance + 1.0); 145 | double k4 = k * k * k * k; 146 | double k4F = 1.0 - k4; 147 | double fl = (k4 * adaptingLuminance) + (0.1 * k4F * k4F * Math.cbrt(5.0 * adaptingLuminance)); 148 | double n = (ColorUtils.yFromLstar(backgroundLstar) / whitePoint[1]); 149 | double z = 1.48 + Math.sqrt(n); 150 | double nbb = 0.725 / Math.pow(n, 0.2); 151 | double ncb = nbb; 152 | double[] rgbAFactors = 153 | new double[] { 154 | Math.pow(fl * rgbD[0] * rW / 100.0, 0.42), 155 | Math.pow(fl * rgbD[1] * gW / 100.0, 0.42), 156 | Math.pow(fl * rgbD[2] * bW / 100.0, 0.42) 157 | }; 158 | 159 | double[] rgbA = 160 | new double[] { 161 | (400.0 * rgbAFactors[0]) / (rgbAFactors[0] + 27.13), 162 | (400.0 * rgbAFactors[1]) / (rgbAFactors[1] + 27.13), 163 | (400.0 * rgbAFactors[2]) / (rgbAFactors[2] + 27.13) 164 | }; 165 | 166 | double aw = ((2.0 * rgbA[0]) + rgbA[1] + (0.05 * rgbA[2])) * nbb; 167 | return new ViewingConditions(n, aw, nbb, ncb, c, nc, rgbD, fl, Math.pow(fl, 0.25), z); 168 | } 169 | 170 | /** 171 | * Parameters are intermediate values of the CAM16 conversion process. Their names are shorthand 172 | * for technical color science terminology, this class would not benefit from documenting them 173 | * individually. A brief overview is available in the CAM16 specification, and a complete overview 174 | * requires a color science textbook, such as Fairchild's Color Appearance Models. 175 | */ 176 | private ViewingConditions( 177 | double n, 178 | double aw, 179 | double nbb, 180 | double ncb, 181 | double c, 182 | double nc, 183 | double[] rgbD, 184 | double fl, 185 | double flRoot, 186 | double z) { 187 | this.n = n; 188 | this.aw = aw; 189 | this.nbb = nbb; 190 | this.ncb = ncb; 191 | this.c = c; 192 | this.nc = nc; 193 | this.rgbD = rgbD; 194 | this.fl = fl; 195 | this.flRoot = flRoot; 196 | this.z = z; 197 | } 198 | } 199 | -------------------------------------------------------------------------------- /extendedcolors/src/main/java/com/smarttoolfactory/extendedcolors/util/HSVUtil.kt: -------------------------------------------------------------------------------- 1 | package com.smarttoolfactory.extendedcolors.util 2 | 3 | import androidx.compose.ui.graphics.Color 4 | import com.smarttoolfactory.extendedcolors.util.ColorUtil.colorIntToRGBArray 5 | 6 | 7 | object HSVUtil { 8 | /* 9 | HSV-HSL Conversions 10 | */ 11 | /** 12 | * Convert [HSV](https://en.wikipedia.org/wiki/HSL_and_HSV) 13 | * components(hue-saturation-value) to HSL 14 | * (hue-saturation-lightness) components. 15 | * 16 | * @param hue in [0..360f] 17 | * @param saturation in [0..1f] 18 | * @param value in [0..1f] 19 | * @return float array that contains hue, saturation and lightness 20 | */ 21 | fun hsvToHSL( 22 | hue: Float, 23 | saturation: Float, 24 | value: Float 25 | ): FloatArray { 26 | val lightness = (2 - saturation) * value / 2 27 | var saturationHSL = saturation 28 | 29 | if (lightness != 0f) { 30 | saturationHSL = when { 31 | lightness == 1f -> { 32 | 0f 33 | } 34 | lightness < 0.5f -> { 35 | saturationHSL * value / (lightness * 2) 36 | } 37 | else -> { 38 | saturationHSL * value / (2 - lightness * 2) 39 | } 40 | } 41 | } 42 | 43 | return floatArrayOf(hue, saturationHSL.coerceIn(0f, 1f), lightness.coerceIn(0f, 1f)) 44 | } 45 | 46 | /** 47 | * Convert [HSV](https://en.wikipedia.org/wiki/HSL_and_HSV) 48 | * components(hue-saturation-value) to HSL 49 | * (hue-saturation-lightness) components and apply them to [hslOut]. 50 | * @param hue in [0..360f] 51 | * @param saturation in [0..1f] 52 | * @param value in [0..1f] 53 | * @param hslOut array contains hue, saturation and lightness properties 54 | * 55 | */ 56 | fun hsvToHSL( 57 | hue: Float, 58 | saturation: Float, 59 | value: Float, 60 | hslOut: FloatArray 61 | ) { 62 | val lightness = (2 - saturation) * value / 2 63 | var saturationHSL = saturation 64 | 65 | if (lightness != 0f) { 66 | saturationHSL = when { 67 | lightness == 1f -> { 68 | 0f 69 | } 70 | lightness < 0.5f -> { 71 | saturationHSL * value / (lightness * 2) 72 | } 73 | else -> { 74 | saturationHSL * value / (2 - lightness * 2) 75 | } 76 | } 77 | } 78 | 79 | hslOut[0] = hue 80 | hslOut[1] = saturationHSL.coerceIn(0f, 1f) 81 | hslOut[2] = lightness.coerceIn(0f, 1f) 82 | } 83 | 84 | /** 85 | * Convert HSV components(hue-saturation-value) to HSL 86 | * (hue-saturation-lightness) components. 87 | * 88 | * ``` 89 | * hsv[0] is Hue [0 .. 360) 90 | * hsv[1] is Saturation [0...1] 91 | * hsv[2] is Value [0...1] 92 | * ``` 93 | * ``` 94 | * hsl[0] is Hue [0 .. 360) 95 | * hsl[1] is Saturation [0...1] 96 | * hsl[2] is Lightness [0...1] 97 | * ``` 98 | */ 99 | fun hsvToHSL(hsvIn: FloatArray): FloatArray { 100 | return hsvToHSL(hsvIn[0], hsvIn[1], hsvIn[2]) 101 | } 102 | 103 | /** 104 | * Convert HSV components(hue-saturation-value) to HSL 105 | * (hue-saturation-lightness) components. 106 | * 107 | * ``` 108 | * hsv[0] is Hue [0 .. 360) 109 | * hsv[1] is Saturation [0...1] 110 | * hsv[2] is Value [0...1] 111 | * ``` 112 | * ``` 113 | * hsl[0] is Hue [0 .. 360) 114 | * hsl[1] is Saturation [0...1] 115 | * hsl[2] is Lightness [0...1] 116 | * ``` 117 | */ 118 | fun hsvToHSL(hsvIn: FloatArray, hslOut: FloatArray) { 119 | hsvToHSL(hsvIn[0], hsvIn[1], hsvIn[2], hslOut) 120 | } 121 | 122 | 123 | /* 124 | HSV-ColorInt Conversions 125 | */ 126 | 127 | /** 128 | * Convert HSV (hue-saturation-value) components to a RGB color in [Integer] format. 129 | * 130 | * * For instance, red =255, green =0, blue=0 is -65536 131 | * @param hue in [0..360f] 132 | * @param saturation in [0..1f] 133 | * @param value in [0..1f] 134 | */ 135 | fun hsvToColorInt( 136 | hue: Float, 137 | saturation: Float, 138 | value: Float 139 | ): Int { 140 | return hsvToColorInt(floatArrayOf(hue, saturation, value)) 141 | } 142 | 143 | 144 | /** 145 | * Convert HSV (hue-saturation-value) components to a RGB color in [Integer] form. 146 | * 147 | * * For instance, red =255, green =0, blue=0 is -65536 148 | * ``` 149 | * hsv[0] is Hue [0 .. 360) 150 | * hsv[1] is Saturation [0...1] 151 | * hsv[2] is Value [0...1] 152 | * ``` 153 | * @param hsvIn 3 element array which holds the input HSV components. 154 | */ 155 | fun hsvToColorInt(hsvIn: FloatArray): Int { 156 | return android.graphics.Color.HSVToColor(hsvIn) 157 | } 158 | 159 | 160 | /* 161 | HSV-Color Conversions 162 | */ 163 | 164 | /** 165 | * Convert HSV (hue-saturation-value) components to Jetpack Compose [Color]. 166 | * @param hue in [0..360f] 167 | * @param saturation in [0..1f] 168 | * @param value in [0..1f] 169 | * @param alpha in [0..1f] 170 | */ 171 | fun hsvToColor( 172 | hue: Float, 173 | saturation: Float, 174 | value: Float, 175 | alpha: Float 176 | ): Color { 177 | return Color.hsv(hue, saturation, value, alpha) 178 | } 179 | 180 | 181 | /* 182 | HSV-RGB Conversions 183 | */ 184 | 185 | /** 186 | * Convert HSV (hue-saturation-value) components to a RGB red, green blue array. 187 | * ``` 188 | * rgb[0] is Red [0 .. 255] 189 | * rgb[1] is Green [0...255] 190 | * rgb[2] is Blue [0...255] 191 | * ``` 192 | * @param hue in [0..360f] 193 | * @param saturation in [0..1f] 194 | * @param value in [0..1f] 195 | */ 196 | fun hsvToRGB( 197 | hue: Float, 198 | saturation: Float, 199 | value: Float 200 | ): IntArray { 201 | return colorIntToRGBArray( 202 | hsvToColorInt(hue, saturation, value) 203 | ) 204 | } 205 | 206 | /** 207 | * Convert HSV (hue-saturation-value) components to a RGB red, green blue array. 208 | * ``` 209 | * hsv[0] is Hue [0 .. 360) 210 | * hsv[1] is Saturation [0...1] 211 | * hsv[2] is Value [0...1] 212 | * ``` 213 | * ``` 214 | * rgb[0] is Red [0 .. 255] 215 | * rgb[1] is Green [0...255] 216 | * rgb[2] is Blue [0...255] 217 | * ``` 218 | * @param hsvIn 3 element array which holds the input HSV components. 219 | */ 220 | fun hsvToRGB(hsvIn: FloatArray): IntArray { 221 | return colorIntToRGBArray( 222 | hsvToColorInt(hsvIn) 223 | ) 224 | } 225 | 226 | /** 227 | * Convert HSV (hue-saturation-value) components to a RGB red, green blue array. 228 | * ``` 229 | * rgb[0] is Red [0 .. 255] 230 | * rgb[1] is Green [0...255] 231 | * rgb[2] is Blue [0...255] 232 | * ``` 233 | * @param hue in [0..360f] 234 | * @param saturation in [0..1f] 235 | * @param value in [0..1f] 236 | * @param rgbIn 3 element array which holds the input RGB components. 237 | */ 238 | fun hsvToRGB( 239 | hue: Float, 240 | saturation: Float, 241 | value: Float, 242 | rgbIn: IntArray 243 | ) { 244 | colorIntToRGBArray( 245 | color = hsvToColorInt(hue, saturation, value), 246 | rgbIn = rgbIn 247 | ) 248 | } 249 | 250 | /** 251 | * Convert HSV (hue-saturation-value) to RGB red, green, blue components in [0f..1f] range. 252 | * ``` 253 | * rgb[0] is Red [0f .. 1f) 254 | * rgb[1] is Green [0f...1f] 255 | * rgb[2] is Blue [0f...1f] 256 | * ``` 257 | * @param hue in [0..360f] 258 | * @param saturation in [0..1f] 259 | * @param value in [0..1f] 260 | * 261 | * @return 3 element array which holds the output RGB components. 262 | */ 263 | fun hsvToRGBFloat( 264 | hue: Float, 265 | saturation: Float, 266 | value: Float 267 | ): FloatArray { 268 | val color = Color.hsv(hue, saturation, value) 269 | return floatArrayOf(color.red, color.green, color.blue) 270 | } 271 | 272 | /** 273 | * Convert HSV (hue-saturation-value) to RGB red, green, blue components in [0f..1f] range. 274 | * ``` 275 | * rgb[0] is Red [0f .. 1f) 276 | * rgb[1] is Green [0f...1f] 277 | * rgb[2] is Blue [0f...1f] 278 | * ``` 279 | * @param hue in [0..360f] 280 | * @param saturation in [0..1f] 281 | * @param value in [0..1f] 282 | * @param rgbIn 3 element array which holds the output RGB components. 283 | */ 284 | fun hsvToRGBFloat( 285 | hue: Float, 286 | saturation: Float, 287 | value: Float, 288 | rgbIn: FloatArray 289 | ) { 290 | val color = Color.hsv(hue, saturation, value) 291 | rgbIn[0] = color.red 292 | rgbIn[1] = color.green 293 | rgbIn[2] = color.blue 294 | } 295 | 296 | } -------------------------------------------------------------------------------- /extendedcolors/src/main/java/com/smarttoolfactory/extendedcolors/util/ColorUtil.kt: -------------------------------------------------------------------------------- 1 | package com.smarttoolfactory.extendedcolors.util 2 | 3 | import androidx.compose.ui.graphics.Color 4 | import androidx.compose.ui.graphics.toArgb 5 | import androidx.core.graphics.ColorUtils 6 | import com.smarttoolfactory.extendedcolors.util.RGBUtil.argbToHex 7 | import com.smarttoolfactory.extendedcolors.util.RGBUtil.rgbToHSL 8 | import com.smarttoolfactory.extendedcolors.util.RGBUtil.rgbToHSV 9 | import com.smarttoolfactory.extendedcolors.util.RGBUtil.rgbToHex 10 | 11 | 12 | object ColorUtil { 13 | 14 | /** 15 | * Convert Jetpack Compose [Color] to HSV (hue-saturation-value) components. 16 | * ``` 17 | * Hue is [0 .. 360) 18 | * Saturation is [0...1] 19 | * Value is [0...1] 20 | * ``` 21 | * @param hslIn 3 element array which holds the input HSL components. 22 | */ 23 | fun colorToHSV(color: Color, hslIn: FloatArray) { 24 | val rgbArray: IntArray = colorIntToRGBArray(color.toArgb()) 25 | val red = rgbArray[0] 26 | val green = rgbArray[1] 27 | val blue = rgbArray[2] 28 | rgbToHSV(red, green, blue, hslIn) 29 | } 30 | 31 | /** 32 | * Convert Jetpack Compose [Color] to HSV (hue-saturation-value) components. 33 | * ``` 34 | * Hue is [0 .. 360) 35 | * Saturation is [0...1] 36 | * Value is [0...1] 37 | * ``` 38 | */ 39 | fun colorToHSV(color: Color): FloatArray { 40 | val rgbArray: IntArray = colorIntToRGBArray(color.toArgb()) 41 | val red = rgbArray[0] 42 | val green = rgbArray[1] 43 | val blue = rgbArray[2] 44 | return rgbToHSV(red, green, blue) 45 | } 46 | 47 | /** 48 | * Convert Jetpack Compose [Color] to HSV (hue-saturation-value) components. 49 | * ``` 50 | * hsl[0] is Hue [0 .. 360) 51 | * hsl[1] is Saturation [0...1] 52 | * hsl[2] is Lightness [0...1] 53 | * ``` 54 | * @param hslIn 3 element array which holds the input HSL components. 55 | */ 56 | fun colorToHSL(color: Color, hslIn: FloatArray) { 57 | val rgbArray: IntArray = colorIntToRGBArray(color.toArgb()) 58 | val red = rgbArray[0] 59 | val green = rgbArray[1] 60 | val blue = rgbArray[2] 61 | rgbToHSL(red, green, blue, hslIn) 62 | } 63 | 64 | /** 65 | * Convert Jetpack Compose [Color] to HSV (hue-saturation-value) components. 66 | * ``` 67 | * hsl[0] is Hue [0 .. 360) 68 | * hsl[1] is Saturation [0...1] 69 | * hsl[2] is Lightness [0...1] 70 | * ``` 71 | */ 72 | fun colorToHSL(color: Color): FloatArray { 73 | val rgbArray: IntArray = colorIntToRGBArray(color.toArgb()) 74 | val red = rgbArray[0] 75 | val green = rgbArray[1] 76 | val blue = rgbArray[2] 77 | return rgbToHSL(red, green, blue) 78 | } 79 | 80 | /* 81 | COLOR-RGB Conversions 82 | */ 83 | 84 | /** 85 | * Convert Jetpack [Color] into 3 element array of red, green, and blue 86 | *``` 87 | * rgb[0] is Red [0 .. 255] 88 | * rgb[1] is Green [0...255] 89 | * rgb[2] is Blue [0...255] 90 | * ``` 91 | * @param color Jetpack Compose [Color] 92 | * @return 3 element array which holds the input RGB components. 93 | */ 94 | fun colorToARGBArray(color: Color): IntArray { 95 | return colorIntToRGBArray(color.toArgb()) 96 | } 97 | 98 | /** 99 | * Convert Jetpack [Color] into 3 element array of red, green, and blue 100 | *``` 101 | * rgb[0] is Red [0 .. 255] 102 | * rgb[1] is Green [0...255] 103 | * rgb[2] is Blue [0...255] 104 | * ``` 105 | * @param color Jetpack Compose [Color] 106 | * @return 3 element array which holds the input RGB components. 107 | */ 108 | fun colorToRGBArray(color: Color): IntArray { 109 | return colorIntToRGBArray(color.toArgb()) 110 | } 111 | 112 | /** 113 | * Convert Jetpack [Color] into 3 element array of red, green, and blue 114 | *``` 115 | * rgb[0] is Red [0 .. 255] 116 | * rgb[1] is Green [0...255] 117 | * rgb[2] is Blue [0...255] 118 | * ``` 119 | * @param color Jetpack Compose [Color] 120 | * @param rgbIn 3 element array which holds the input RGB components. 121 | */ 122 | fun colorToRGBArray(color: Color, rgbIn: IntArray) { 123 | val argbArray = colorIntToRGBArray(color.toArgb()) 124 | rgbIn[0] = argbArray[0] 125 | rgbIn[1] = argbArray[1] 126 | rgbIn[2] = argbArray[2] 127 | } 128 | 129 | 130 | fun colorToHex(color: Color): String { 131 | return rgbToHex(color.red, color.green, color.blue) 132 | } 133 | 134 | fun colorToHexAlpha(color: Color): String { 135 | return argbToHex(color.alpha, color.red, color.green, color.blue) 136 | } 137 | 138 | /** 139 | * Convert a RGB color in [Integer] form to HSV (hue-saturation-value) components. 140 | * * For instance, red =255, green =0, blue=0 is -65536 141 | * ``` 142 | * hsv[0] is Hue [0 .. 360) 143 | * hsv[1] is Saturation [0...1] 144 | * hsv[2] is Value [0...1] 145 | * ``` 146 | */ 147 | fun colorIntToHSV(color: Int): FloatArray { 148 | val hsvOut = floatArrayOf(0f, 0f, 0f) 149 | android.graphics.Color.colorToHSV(color, hsvOut) 150 | return hsvOut 151 | } 152 | 153 | /** 154 | * Convert a RGB color in [Integer] form to HSV (hue-saturation-value) components. 155 | * * For instance, red =255, green =0, blue=0 is -65536 156 | * 157 | * ``` 158 | * hsv[0] is Hue [0 .. 360) 159 | * hsv[1] is Saturation [0...1] 160 | * hsv[2] is Value [0...1] 161 | * ``` 162 | * @param hsvIn 3 element array which holds the input HSV components. 163 | */ 164 | fun colorIntToHSV(color: Int, hsvIn: FloatArray) { 165 | android.graphics.Color.colorToHSV(color, hsvIn) 166 | } 167 | 168 | 169 | /** 170 | * Convert RGB color [Integer] to HSL (hue-saturation-lightness) components. 171 | * ``` 172 | * hsl[0] is Hue [0 .. 360) 173 | * hsl[1] is Saturation [0...1] 174 | * hsl[2] is Lightness [0...1] 175 | * ``` 176 | */ 177 | fun colorIntToHSL(color: Int): FloatArray { 178 | val hslOut = floatArrayOf(0f, 0f, 0f) 179 | ColorUtils.colorToHSL(color, hslOut) 180 | return hslOut 181 | } 182 | 183 | /** 184 | * Convert RGB color [Integer] to HSL (hue-saturation-lightness) components. 185 | * ``` 186 | * hsl[0] is Hue [0 .. 360) 187 | * hsl[1] is Saturation [0...1] 188 | * hsl[2] is Lightness [0...1] 189 | * ``` 190 | */ 191 | fun colorIntToHSL(color: Int, hslIn: FloatArray) { 192 | ColorUtils.colorToHSL(color, hslIn) 193 | } 194 | 195 | 196 | /* 197 | ColorInt-RGB Conversions 198 | */ 199 | /** 200 | * Convert Color [Integer] into 3 element array of red, green, and blue 201 | *``` 202 | * rgb[0] is Red [0 .. 255] 203 | * rgb[1] is Green [0...255] 204 | * rgb[2] is Blue [0...255] 205 | * ``` 206 | * @return 3 element array which holds the input RGB components. 207 | */ 208 | fun colorIntToRGBArray(color: Int): IntArray { 209 | val red = android.graphics.Color.red(color) 210 | val green = android.graphics.Color.green(color) 211 | val blue = android.graphics.Color.blue(color) 212 | return intArrayOf(red, green, blue) 213 | } 214 | 215 | /** 216 | * Convert Color [Integer] into 3 element array of red, green, and blue 217 | *``` 218 | * rgb[0] is Red [0 .. 255] 219 | * rgb[1] is Green [0...255] 220 | * rgb[2] is Blue [0...255] 221 | * ``` 222 | * @param rgbIn 3 element array which holds the input RGB components. 223 | */ 224 | fun colorIntToRGBArray(color: Int, rgbIn: IntArray) { 225 | val red = android.graphics.Color.red(color) 226 | val green = android.graphics.Color.green(color) 227 | val blue = android.graphics.Color.blue(color) 228 | 229 | rgbIn[0] = red 230 | rgbIn[1] = green 231 | rgbIn[2] = blue 232 | } 233 | 234 | /** 235 | * Convert Color [Integer] into 4 element array of alpha red, green, and blue 236 | *``` 237 | * rgb[0] is Alpha [0 .. 255] 238 | * rgb[1] is Red [0 .. 255] 239 | * rgb[2] is Green [0...255] 240 | * rgb[3] is Blue [0...255] 241 | * ``` 242 | * @return 4 element array which holds the input ARGB components. 243 | */ 244 | fun colorIntToARGBArray(color: Int): IntArray { 245 | val alpha = android.graphics.Color.alpha(color) 246 | val red = android.graphics.Color.red(color) 247 | val green = android.graphics.Color.green(color) 248 | val blue = android.graphics.Color.blue(color) 249 | return intArrayOf(alpha, red, green, blue) 250 | } 251 | 252 | /** 253 | * Convert Color [Integer] into 4 element array of alpha red, green, and blue 254 | *``` 255 | * rgb[0] is Alpha [0 .. 255] 256 | * rgb[1] is Red [0 .. 255] 257 | * rgb[2] is Green [0...255] 258 | * rgb[3] is Blue [0...255] 259 | * ``` 260 | * @param argbIn 4 element array which holds the input ARGB components. 261 | */ 262 | fun colorIntToARGBArray(color: Int, argbIn: IntArray) { 263 | val alpha = android.graphics.Color.alpha(color) 264 | val red = android.graphics.Color.red(color) 265 | val green = android.graphics.Color.green(color) 266 | val blue = android.graphics.Color.blue(color) 267 | 268 | argbIn[0] = alpha 269 | argbIn[1] = red 270 | argbIn[2] = green 271 | argbIn[3] = blue 272 | } 273 | } -------------------------------------------------------------------------------- /extendedcolors/src/main/java/com/smarttoolfactory/extendedcolors/md3/utils/ColorUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file is automatically generated. Do not modify it. 18 | 19 | package com.smarttoolfactory.extendedcolors.md3.utils; 20 | 21 | /** 22 | * Color science utilities. 23 | * 24 | *

Utility methods for color science constants and color space conversions that aren't HCT or 25 | * CAM16. 26 | */ 27 | public class ColorUtils { 28 | private ColorUtils() {} 29 | 30 | static final double[][] SRGB_TO_XYZ = 31 | new double[][] { 32 | new double[] {0.41233895, 0.35762064, 0.18051042}, 33 | new double[] {0.2126, 0.7152, 0.0722}, 34 | new double[] {0.01932141, 0.11916382, 0.95034478}, 35 | }; 36 | 37 | static final double[][] XYZ_TO_SRGB = 38 | new double[][] { 39 | new double[] { 40 | 3.2413774792388685, -1.5376652402851851, -0.49885366846268053, 41 | }, 42 | new double[] { 43 | -0.9691452513005321, 1.8758853451067872, 0.04156585616912061, 44 | }, 45 | new double[] { 46 | 0.05562093689691305, -0.20395524564742123, 1.0571799111220335, 47 | }, 48 | }; 49 | 50 | static final double[] WHITE_POINT_D65 = new double[] {95.047, 100.0, 108.883}; 51 | 52 | /** Converts a color from RGB components to ARGB format. */ 53 | public static int argbFromRgb(int red, int green, int blue) { 54 | return (255 << 24) | ((red & 255) << 16) | ((green & 255) << 8) | (blue & 255); 55 | } 56 | 57 | /** Converts a color from linear RGB components to ARGB format. */ 58 | public static int argbFromLinrgb(double[] linrgb) { 59 | int r = delinearized(linrgb[0]); 60 | int g = delinearized(linrgb[1]); 61 | int b = delinearized(linrgb[2]); 62 | return argbFromRgb(r, g, b); 63 | } 64 | 65 | /** Returns the alpha component of a color in ARGB format. */ 66 | public static int alphaFromArgb(int argb) { 67 | return (argb >> 24) & 255; 68 | } 69 | 70 | /** Returns the red component of a color in ARGB format. */ 71 | public static int redFromArgb(int argb) { 72 | return (argb >> 16) & 255; 73 | } 74 | 75 | /** Returns the green component of a color in ARGB format. */ 76 | public static int greenFromArgb(int argb) { 77 | return (argb >> 8) & 255; 78 | } 79 | 80 | /** Returns the blue component of a color in ARGB format. */ 81 | public static int blueFromArgb(int argb) { 82 | return argb & 255; 83 | } 84 | 85 | /** Returns whether a color in ARGB format is opaque. */ 86 | public static boolean isOpaque(int argb) { 87 | return alphaFromArgb(argb) >= 255; 88 | } 89 | 90 | /** Converts a color from ARGB to XYZ. */ 91 | public static int argbFromXyz(double x, double y, double z) { 92 | double[][] matrix = XYZ_TO_SRGB; 93 | double linearR = matrix[0][0] * x + matrix[0][1] * y + matrix[0][2] * z; 94 | double linearG = matrix[1][0] * x + matrix[1][1] * y + matrix[1][2] * z; 95 | double linearB = matrix[2][0] * x + matrix[2][1] * y + matrix[2][2] * z; 96 | int r = delinearized(linearR); 97 | int g = delinearized(linearG); 98 | int b = delinearized(linearB); 99 | return argbFromRgb(r, g, b); 100 | } 101 | 102 | /** Converts a color from XYZ to ARGB. */ 103 | public static double[] xyzFromArgb(int argb) { 104 | double r = linearized(redFromArgb(argb)); 105 | double g = linearized(greenFromArgb(argb)); 106 | double b = linearized(blueFromArgb(argb)); 107 | return MathUtils.matrixMultiply(new double[] {r, g, b}, SRGB_TO_XYZ); 108 | } 109 | 110 | /** Converts a color represented in Lab color space into an ARGB integer. */ 111 | public static int argbFromLab(double l, double a, double b) { 112 | double[] whitePoint = WHITE_POINT_D65; 113 | double fy = (l + 16.0) / 116.0; 114 | double fx = a / 500.0 + fy; 115 | double fz = fy - b / 200.0; 116 | double xNormalized = labInvf(fx); 117 | double yNormalized = labInvf(fy); 118 | double zNormalized = labInvf(fz); 119 | double x = xNormalized * whitePoint[0]; 120 | double y = yNormalized * whitePoint[1]; 121 | double z = zNormalized * whitePoint[2]; 122 | return argbFromXyz(x, y, z); 123 | } 124 | 125 | /** 126 | * Converts a color from ARGB representation to L*a*b* representation. 127 | * 128 | * @param argb the ARGB representation of a color 129 | * @return a Lab object representing the color 130 | */ 131 | public static double[] labFromArgb(int argb) { 132 | double linearR = linearized(redFromArgb(argb)); 133 | double linearG = linearized(greenFromArgb(argb)); 134 | double linearB = linearized(blueFromArgb(argb)); 135 | double[][] matrix = SRGB_TO_XYZ; 136 | double x = matrix[0][0] * linearR + matrix[0][1] * linearG + matrix[0][2] * linearB; 137 | double y = matrix[1][0] * linearR + matrix[1][1] * linearG + matrix[1][2] * linearB; 138 | double z = matrix[2][0] * linearR + matrix[2][1] * linearG + matrix[2][2] * linearB; 139 | double[] whitePoint = WHITE_POINT_D65; 140 | double xNormalized = x / whitePoint[0]; 141 | double yNormalized = y / whitePoint[1]; 142 | double zNormalized = z / whitePoint[2]; 143 | double fx = labF(xNormalized); 144 | double fy = labF(yNormalized); 145 | double fz = labF(zNormalized); 146 | double l = 116.0 * fy - 16; 147 | double a = 500.0 * (fx - fy); 148 | double b = 200.0 * (fy - fz); 149 | return new double[] {l, a, b}; 150 | } 151 | 152 | /** 153 | * Converts an L* value to an ARGB representation. 154 | * 155 | * @param lstar L* in L*a*b* 156 | * @return ARGB representation of grayscale color with lightness matching L* 157 | */ 158 | public static int argbFromLstar(double lstar) { 159 | double fy = (lstar + 16.0) / 116.0; 160 | double fz = fy; 161 | double fx = fy; 162 | double kappa = 24389.0 / 27.0; 163 | double epsilon = 216.0 / 24389.0; 164 | boolean lExceedsEpsilonKappa = lstar > 8.0; 165 | double y = lExceedsEpsilonKappa ? fy * fy * fy : lstar / kappa; 166 | boolean cubeExceedEpsilon = fy * fy * fy > epsilon; 167 | double x = cubeExceedEpsilon ? fx * fx * fx : lstar / kappa; 168 | double z = cubeExceedEpsilon ? fz * fz * fz : lstar / kappa; 169 | double[] whitePoint = WHITE_POINT_D65; 170 | return argbFromXyz(x * whitePoint[0], y * whitePoint[1], z * whitePoint[2]); 171 | } 172 | 173 | /** 174 | * Computes the L* value of a color in ARGB representation. 175 | * 176 | * @param argb ARGB representation of a color 177 | * @return L*, from L*a*b*, coordinate of the color 178 | */ 179 | public static double lstarFromArgb(int argb) { 180 | double y = xyzFromArgb(argb)[1] / 100.0; 181 | double e = 216.0 / 24389.0; 182 | if (y <= e) { 183 | return 24389.0 / 27.0 * y; 184 | } else { 185 | double yIntermediate = Math.pow(y, 1.0 / 3.0); 186 | return 116.0 * yIntermediate - 16.0; 187 | } 188 | } 189 | 190 | /** 191 | * Converts an L* value to a Y value. 192 | * 193 | *

L* in L*a*b* and Y in XYZ measure the same quantity, luminance. 194 | * 195 | *

L* measures perceptual luminance, a linear scale. Y in XYZ measures relative luminance, a 196 | * logarithmic scale. 197 | * 198 | * @param lstar L* in L*a*b* 199 | * @return Y in XYZ 200 | */ 201 | public static double yFromLstar(double lstar) { 202 | double ke = 8.0; 203 | if (lstar > ke) { 204 | return Math.pow((lstar + 16.0) / 116.0, 3.0) * 100.0; 205 | } else { 206 | return lstar / (24389.0 / 27.0) * 100.0; 207 | } 208 | } 209 | 210 | /** 211 | * Linearizes an RGB component. 212 | * 213 | * @param rgbComponent 0 <= rgb_component <= 255, represents R/G/B channel 214 | * @return 0.0 <= output <= 100.0, color channel converted to linear RGB space 215 | */ 216 | public static double linearized(int rgbComponent) { 217 | double normalized = rgbComponent / 255.0; 218 | if (normalized <= 0.040449936) { 219 | return normalized / 12.92 * 100.0; 220 | } else { 221 | return Math.pow((normalized + 0.055) / 1.055, 2.4) * 100.0; 222 | } 223 | } 224 | 225 | /** 226 | * Delinearizes an RGB component. 227 | * 228 | * @param rgbComponent 0.0 <= rgb_component <= 100.0, represents linear R/G/B channel 229 | * @return 0 <= output <= 255, color channel converted to regular RGB space 230 | */ 231 | public static int delinearized(double rgbComponent) { 232 | double normalized = rgbComponent / 100.0; 233 | double delinearized = 0.0; 234 | if (normalized <= 0.0031308) { 235 | delinearized = normalized * 12.92; 236 | } else { 237 | delinearized = 1.055 * Math.pow(normalized, 1.0 / 2.4) - 0.055; 238 | } 239 | return MathUtils.clampInt(0, 255, (int) Math.round(delinearized * 255.0)); 240 | } 241 | 242 | /** 243 | * Returns the standard white point; white on a sunny day. 244 | * 245 | * @return The white point 246 | */ 247 | public static double[] whitePointD65() { 248 | return WHITE_POINT_D65; 249 | } 250 | 251 | static double labF(double t) { 252 | double e = 216.0 / 24389.0; 253 | double kappa = 24389.0 / 27.0; 254 | if (t > e) { 255 | return Math.pow(t, 1.0 / 3.0); 256 | } else { 257 | return (kappa * t + 16) / 116; 258 | } 259 | } 260 | 261 | static double labInvf(double ft) { 262 | double e = 216.0 / 24389.0; 263 | double kappa = 24389.0 / 27.0; 264 | double ft3 = ft * ft * ft; 265 | if (ft3 > e) { 266 | return ft3; 267 | } else { 268 | return (116 * ft - 16) / kappa; 269 | } 270 | } 271 | 272 | } 273 | 274 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2022 Smart Tool Factory 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /extendedcolors/src/main/java/com/smarttoolfactory/extendedcolors/util/RGBUtil.kt: -------------------------------------------------------------------------------- 1 | package com.smarttoolfactory.extendedcolors.util 2 | 3 | import androidx.core.graphics.ColorUtils 4 | import com.smarttoolfactory.extendedcolors.util.ColorUtil.colorIntToHSL 5 | import com.smarttoolfactory.extendedcolors.util.ColorUtil.colorIntToHSV 6 | import java.util.* 7 | 8 | object RGBUtil { 9 | 10 | /** 11 | * Convert RGB red, green blue to HSV (hue-saturation-value) components. 12 | * ``` 13 | * hsv[0] is Hue [0 .. 360) 14 | * hsv[1] is Saturation [0...1] 15 | * hsv[2] is Value [0...1] 16 | * ``` 17 | * @param red Red component [0..255] of the color 18 | * @param green Green component [0..255] of the color 19 | * @param blue Blue component [0..255] of the color 20 | */ 21 | fun rgbToHSV( 22 | red: Int, 23 | green: Int, 24 | blue: Int 25 | ): FloatArray { 26 | val hsvOut = floatArrayOf(0f, 0f, 0f) 27 | android.graphics.Color.RGBToHSV(red, green, blue, hsvOut) 28 | return hsvOut 29 | } 30 | 31 | /** 32 | * Convert RGB red, green blue to HSV (hue-saturation-value) components. 33 | * ``` 34 | * hsv[0] is Hue [0 .. 360) 35 | * hsv[1] is Saturation [0...1] 36 | * hsv[2] is Value [0...1] 37 | * ``` 38 | * @param red Red component [0..255] of the color 39 | * @param green Green component [0..255] of the color 40 | * @param blue Blue component [0..255] of the color 41 | * @param hsvIn 3 element array which holds the output HSV components. 42 | */ 43 | fun rgbToHSV( 44 | red: Int, 45 | green: Int, 46 | blue: Int, 47 | hsvIn: FloatArray 48 | ) { 49 | android.graphics.Color.RGBToHSV(red, green, blue, hsvIn) 50 | } 51 | 52 | 53 | /** 54 | * Convert RGB red, green blue to HSL (hue-saturation-lightness) components. 55 | * ``` 56 | * hsl[0] is Hue [0 .. 360) 57 | * hsl[1] is Saturation [0...1] 58 | * hsl[2] is Lightness [0...1] 59 | * ``` 60 | * @param red Red component [0..255] of the color 61 | * @param green Green component [0..255] of the color 62 | * @param blue Blue component [0..255] of the color 63 | */ 64 | fun rgbToHSL( 65 | red: Int, 66 | green: Int, 67 | blue: Int 68 | ): FloatArray { 69 | val outHsl = floatArrayOf(0f, 0f, 0f) 70 | ColorUtils.RGBToHSL(red, green, blue, outHsl) 71 | return outHsl 72 | } 73 | 74 | /** 75 | * Convert RGB red, green blue to HSL (hue-saturation-lightness) components. 76 | * ``` 77 | * hsl[0] is Hue [0 .. 360) 78 | * hsl[1] is Saturation [0...1] 79 | * hsl[2] is Lightness [0...1] 80 | * ``` 81 | * @param red Red component [0..255] of the color 82 | * @param green Green component [0..255] of the color 83 | * @param blue Blue component [0..255] of the color 84 | * @param hslIn 3 element array which holds the input HSL components. 85 | */ 86 | fun rgbToHSL( 87 | red: Int, 88 | green: Int, 89 | blue: Int, 90 | hslIn: FloatArray 91 | ) { 92 | ColorUtils.RGBToHSL(red, green, blue, hslIn) 93 | } 94 | 95 | /** 96 | * Convert RGB red, green, blue components in [0f..1f] range to 97 | * HSV (hue-saturation-value) components. 98 | * 99 | * @param red Red component [0..255] of the color 100 | * @param green Green component [0..255] of the color 101 | * @param blue Blue component [0..255] of the color 102 | * @return 3 element array which holds the output RGB components. 103 | */ 104 | fun rgbFloatToHSV( 105 | red: Float, 106 | green: Float, 107 | blue: Float 108 | ): FloatArray { 109 | val colorInt = rgbToColorInt(red = red, green = green, blue = blue) 110 | val outHsl = floatArrayOf(0f, 0f, 0f) 111 | colorIntToHSV(colorInt, outHsl) 112 | return outHsl 113 | } 114 | 115 | /** 116 | * Convert RGB red, green, blue components in [0f..1f] range to 117 | * HSV (hue-saturation-value) components. 118 | * 119 | * @param red Red component [0..255] of the color 120 | * @param green Green component [0..255] of the color 121 | * @param blue Blue component [0..255] of the color 122 | * @param hsvIn 3 element array which holds the output HSV components. 123 | */ 124 | fun rgbFloatToHSV( 125 | red: Float, 126 | green: Float, 127 | blue: Float, 128 | hsvIn: FloatArray 129 | ) { 130 | val colorInt = rgbToColorInt(red = red, green = green, blue = blue) 131 | colorIntToHSV(colorInt, hsvIn) 132 | } 133 | 134 | 135 | /** 136 | * Convert RGB red, green blue in [0f..1f] range to HSL (hue-saturation-lightness) components. 137 | * ``` 138 | * hsl[0] is Hue [0 .. 360) 139 | * hsl[1] is Saturation [0...1] 140 | * hsl[2] is Lightness [0...1] 141 | * ``` 142 | * @param red Red component [0f..1f] of the color 143 | * @param green Green component [0f..1f] of the color 144 | * @param blue Blue component [0f..1f] of the color 145 | */ 146 | fun rgbFloatToHSL( 147 | red: Float, 148 | green: Float, 149 | blue: Float 150 | ): FloatArray { 151 | val colorInt = rgbToColorInt(red = red, green = green, blue = blue) 152 | val outHsl = floatArrayOf(0f, 0f, 0f) 153 | colorIntToHSL(colorInt, outHsl) 154 | return outHsl 155 | } 156 | 157 | /** 158 | * Convert RGB red, green blue in [0f..1f] range to HSL (hue-saturation-lightness) components. 159 | * ``` 160 | * hsl[0] is Hue [0 .. 360) 161 | * hsl[1] is Saturation [0...1] 162 | * hsl[2] is Lightness [0...1] 163 | * ``` 164 | * @param red Red component [0f..1f] of the color 165 | * @param green Green component [0f..1f] of the color 166 | * @param blue Blue component [0f..1f] of the color 167 | * @param hslIn 3 element array which holds the input HSL components. 168 | */ 169 | fun rgbFloatToHSL( 170 | red: Float, 171 | green: Float, 172 | blue: Float, 173 | hslIn: FloatArray 174 | ) { 175 | val colorInt = rgbToColorInt(red = red, green = green, blue = blue) 176 | colorIntToHSL(colorInt, hslIn) 177 | } 178 | 179 | 180 | /** 181 | * Return a color-int from alpha, red, green, blue components. 182 | * These component values should be [0..255], but there is no range check performed, 183 | * so if they are out of range, the returned color is undefined. 184 | * 185 | * @param red Red component [0..255] of the color 186 | * @param green Green component [0..255] of the color 187 | * @param blue Blue component [0..255] of the color 188 | */ 189 | fun rgbToColorInt( 190 | red: Int, 191 | green: Int, 192 | blue: Int 193 | ): Int { 194 | return android.graphics.Color.rgb(red, green, blue) 195 | } 196 | 197 | /** 198 | * Return a color-int from alpha, red, green, blue components. 199 | * These component values should be [0f..1f], but there is no range check performed, 200 | * so if they are out of range, the returned color is undefined. 201 | * 202 | * @param red Red component [0f..1f] of the color 203 | * @param green Green component [0..1f] of the color 204 | * @param blue Blue component [0..1f] of the color 205 | * 206 | */ 207 | fun rgbToColorInt( 208 | red: Float, 209 | green: Float, 210 | blue: Float 211 | ): Int { 212 | val redInt = red.fractionToRGBRange() 213 | val greenInt = green.fractionToRGBRange() 214 | val blueInt = blue.fractionToRGBRange() 215 | return android.graphics.Color.rgb(redInt, greenInt, blueInt) 216 | } 217 | 218 | 219 | /** 220 | * Convert red, green, blue components [0..255] range in [Integer] to Hex format String 221 | */ 222 | fun rgbToHex( 223 | red: Int, 224 | green: Int, 225 | blue: Int 226 | ): String { 227 | return "#" + 228 | Integer.toHexString(red).toStringComponent() + 229 | Integer.toHexString(green).toStringComponent() + 230 | Integer.toHexString(blue).toStringComponent() 231 | } 232 | 233 | /** 234 | * Convert rgb array to Hex format String 235 | * ``` 236 | * rgb[0] is Red [0 .. 255] 237 | * rgb[1] is Green [0...255] 238 | * rgb[2] is Blue [0...255] 239 | * ``` 240 | */ 241 | fun rgbToHex(rgb: IntArray): String { 242 | return "#" + 243 | Integer.toHexString(rgb[0]).toStringComponent() + 244 | Integer.toHexString(rgb[1]).toStringComponent() + 245 | Integer.toHexString(rgb[2]).toStringComponent() 246 | } 247 | 248 | /** 249 | * Convert red, green, blue components [0f..1f] range in [Float] to Hex format String 250 | */ 251 | fun rgbToHex( 252 | red: Float, 253 | green: Float, 254 | blue: Float 255 | ): String { 256 | return "#" + 257 | Integer.toHexString(red.fractionToRGBRange()).toStringComponent() + 258 | Integer.toHexString(green.fractionToRGBRange()).toStringComponent() + 259 | Integer.toHexString(blue.fractionToRGBRange()).toStringComponent() 260 | } 261 | 262 | 263 | /** 264 | * Return a color-int from alpha, red, green, blue components. 265 | * These component values should be [0..255], but there is no range check performed, 266 | * so if they are out of range, the returned color is undefined. 267 | * 268 | * @param alpha Alpha component [0..255] of the color 269 | * @param red Red component [0..255] of the color 270 | * @param green Green component [0..255] of the color 271 | * @param blue Blue component [0..255] of the color 272 | */ 273 | fun argbToColorInt( 274 | alpha: Int, 275 | red: Int, 276 | green: Int, 277 | blue: Int 278 | ): Int { 279 | return android.graphics.Color.argb(alpha, red, green, blue) 280 | } 281 | 282 | /** 283 | * Return a color-int from alpha, red, green, blue components. 284 | * These component values should be [0f..1f], but there is no range check performed, 285 | * so if they are out of range, the returned color is undefined. 286 | * 287 | * @param alpha Alpha component [0f..1f] of the color 288 | * @param red Red component [0f..1f] of the color 289 | * @param green Green component [0..1f] of the color 290 | * @param blue Blue component [0..1f] of the color 291 | */ 292 | fun argbToColorInt( 293 | alpha: Float, 294 | red: Float, 295 | green: Float, 296 | blue: Float 297 | ): Int { 298 | val alphaInt = alpha.fractionToRGBRange() 299 | val redInt = red.fractionToRGBRange() 300 | val greenInt = green.fractionToRGBRange() 301 | val blueInt = blue.fractionToRGBRange() 302 | return android.graphics.Color.argb(alphaInt, redInt, greenInt, blueInt) 303 | } 304 | 305 | 306 | /** 307 | * Convert alpha, red, green, blue components in [0..255] range argb to Hex format String 308 | * 309 | * ``` 310 | * Alpha is [0 .. 255] 311 | * Red is [0 .. 255] 312 | * Green is [0...255] 313 | * Blue is [0...255] 314 | * ``` 315 | */ 316 | fun argbToHex( 317 | alpha: Int, 318 | red: Int, 319 | green: Int, 320 | blue: Int 321 | ): String { 322 | return "#" + 323 | Integer.toHexString(alpha).toStringComponent() + 324 | Integer.toHexString(red).toStringComponent() + 325 | Integer.toHexString(green).toStringComponent() + 326 | Integer.toHexString(blue).toStringComponent() 327 | } 328 | 329 | /** 330 | * Convert alpha, red, green, blue components in [0f..1f] range in [Float] argb to Hex format String 331 | * 332 | * ``` 333 | * Alpha is [0f .. 1f] 334 | * Red is [0f .. 1f] 335 | * Green is [0...1f] 336 | * Blue is [0...1f] 337 | * ``` 338 | */ 339 | fun argbToHex( 340 | alpha: Float, 341 | red: Float, 342 | green: Float, 343 | blue: Float 344 | ): String { 345 | return "#" + 346 | Integer.toHexString(alpha.fractionToRGBRange()).toStringComponent() + 347 | Integer.toHexString(red.fractionToRGBRange()).toStringComponent() + 348 | Integer.toHexString(green.fractionToRGBRange()).toStringComponent() + 349 | Integer.toHexString(blue.fractionToRGBRange()).toStringComponent() 350 | } 351 | 352 | /* 353 | RGB-HEX Conversions 354 | */ 355 | /** 356 | * Get hex representation of a rgb Color in [Integer] format 357 | */ 358 | fun Int.toRgbString(): String = 359 | ("#" + 360 | red.toStringComponent() + 361 | green.toStringComponent() + 362 | blue.toStringComponent()) 363 | .uppercase(Locale.getDefault()) 364 | 365 | /** 366 | * Get hex representation of a argb Color in [Integer] format 367 | */ 368 | fun Int.toArgbString(): String = 369 | ("#" + 370 | alpha.toStringComponent() + 371 | red.toStringComponent() + 372 | green.toStringComponent() + 373 | blue.toStringComponent() 374 | ).uppercase(Locale.getDefault()) 375 | 376 | private fun String.toStringComponent() = 377 | this.let { if (it.length == 1) "0${it}" else it } 378 | 379 | private fun Int.toStringComponent(): String = 380 | this.toString(16).let { if (it.length == 1) "0${it}" else it } 381 | 382 | inline val Int.alpha: Int 383 | get() = (this shr 24) and 0xFF 384 | 385 | inline val Int.red: Int 386 | get() = (this shr 16) and 0xFF 387 | 388 | inline val Int.green: Int 389 | get() = (this shr 8) and 0xFF 390 | 391 | inline val Int.blue: Int 392 | get() = this and 0xFF 393 | 394 | } -------------------------------------------------------------------------------- /app/src/main/java/com/smarttoolfactory/composecolorsextended/demo/ColorModeConversionDemo.kt: -------------------------------------------------------------------------------- 1 | package com.smarttoolfactory.composecolorsextended.demo 2 | 3 | import androidx.compose.foundation.background 4 | import androidx.compose.foundation.layout.* 5 | import androidx.compose.foundation.rememberScrollState 6 | import androidx.compose.foundation.shape.RoundedCornerShape 7 | import androidx.compose.foundation.verticalScroll 8 | import androidx.compose.material.Slider 9 | import androidx.compose.material.Text 10 | import androidx.compose.runtime.* 11 | import androidx.compose.ui.Alignment 12 | import androidx.compose.ui.Modifier 13 | import androidx.compose.ui.draw.clip 14 | import androidx.compose.ui.draw.shadow 15 | import androidx.compose.ui.graphics.Color 16 | import androidx.compose.ui.graphics.toArgb 17 | import androidx.compose.ui.text.font.FontWeight 18 | import androidx.compose.ui.unit.dp 19 | import androidx.compose.ui.unit.sp 20 | import com.smarttoolfactory.extendedcolors.util.* 21 | import com.smarttoolfactory.extendedcolors.util.RGBUtil.toArgbString 22 | 23 | 24 | /** 25 | * Converting from HSV or HSL to RGB and then from RGB to HSV or HSL doesn't bear correct 26 | * results all the time. This demo displays when it doesn't work as exptected 27 | */ 28 | @Composable 29 | fun ColorModelConversionDemo() { 30 | 31 | Column( 32 | modifier = Modifier 33 | .verticalScroll(rememberScrollState()) 34 | .fillMaxSize() 35 | .background(Color.LightGray) 36 | .padding(vertical = 10.dp), 37 | horizontalAlignment = Alignment.CenterHorizontally 38 | ) { 39 | 40 | val modifier = Modifier 41 | .padding(8.dp) 42 | .shadow(2.dp, RoundedCornerShape(8.dp)) 43 | .background(Color.White) 44 | .padding(4.dp) 45 | 46 | val sliderModifier = Modifier.padding(horizontal = 8.dp) 47 | 48 | val boxModifier = Modifier 49 | .padding(horizontal = 8.dp, vertical = 4.dp) 50 | .clip(RoundedCornerShape(8.dp)) 51 | .fillMaxWidth(.8f) 52 | .height(40.dp) 53 | 54 | 55 | // Panels that contain 0 to 4 sliders based on which callback is implemented 56 | HSVSliderDisplayPanelExample(modifier, sliderModifier, boxModifier) 57 | HSLSliderDisplayPanelExample(modifier, sliderModifier, boxModifier) 58 | } 59 | } 60 | 61 | @Composable 62 | private fun HSVSliderDisplayPanelExample( 63 | modifier: Modifier, 64 | sliderModifier: Modifier, 65 | boxModifier: Modifier 66 | ) { 67 | var hue by remember { mutableStateOf(0f) } 68 | var saturation by remember { mutableStateOf(.5f) } 69 | var value by remember { mutableStateOf(.5f) } 70 | var alpha by remember { mutableStateOf(1f) } 71 | 72 | val colorHSV = Color.hsv(hue = hue, saturation = saturation, value = value, alpha = alpha) 73 | 74 | Title( 75 | text = "Conversions from HSV" 76 | ) 77 | 78 | Column( 79 | modifier = modifier, 80 | horizontalAlignment = Alignment.CenterHorizontally 81 | ) { 82 | 83 | Box(modifier = boxModifier.background(colorHSV)) 84 | 85 | CheckColorConversionDetailsFromHSV( 86 | color = colorHSV, 87 | hue = hue, 88 | saturation = saturation, 89 | value = value, 90 | alpha = alpha 91 | ) 92 | 93 | Slider(modifier = sliderModifier, value = hue, onValueChange = { hue = it }) 94 | Slider(modifier = sliderModifier, value = saturation, onValueChange = { saturation = it }) 95 | Slider(modifier = sliderModifier, value = value, onValueChange = { value = it }) 96 | Slider(modifier = sliderModifier, value = alpha, onValueChange = { alpha = it }) 97 | } 98 | } 99 | 100 | @Composable 101 | private fun HSLSliderDisplayPanelExample( 102 | modifier: Modifier, 103 | sliderModifier: Modifier, 104 | boxModifier: Modifier 105 | ) { 106 | 107 | var hue by remember { mutableStateOf(0f) } 108 | var saturation by remember { mutableStateOf(.5f) } 109 | var lightness by remember { mutableStateOf(.5f) } 110 | var alpha by remember { mutableStateOf(1f) } 111 | 112 | val colorHSL = 113 | Color.hsl(hue = hue, saturation = saturation, lightness = lightness, alpha = alpha) 114 | 115 | Title( 116 | text = "Conversions from HSL" 117 | ) 118 | 119 | Column( 120 | modifier = modifier, 121 | horizontalAlignment = Alignment.CenterHorizontally 122 | ) { 123 | Box(modifier = boxModifier.background(colorHSL)) 124 | 125 | CheckColorConversionDetailsFromHSL( 126 | colorHSL, 127 | hue, 128 | saturation, 129 | lightness, 130 | alpha 131 | ) 132 | 133 | Slider(modifier = sliderModifier, value = hue, onValueChange = { hue = it }) 134 | Slider(modifier = sliderModifier, value = saturation, onValueChange = { saturation = it }) 135 | Slider(modifier = sliderModifier, value = lightness, onValueChange = { lightness = it }) 136 | Slider(modifier = sliderModifier, value = alpha, onValueChange = { alpha = it }) 137 | } 138 | } 139 | 140 | @Composable 141 | private fun Title(text: String) { 142 | Text( 143 | text, 144 | color = Color.Red, 145 | fontWeight = FontWeight.Bold, 146 | fontSize = 18.sp, 147 | modifier = Modifier.padding(vertical = 12.dp) 148 | ) 149 | } 150 | 151 | @Composable 152 | private fun CheckColorConversionDetailsFromHSV( 153 | color: Color, 154 | hue: Float, 155 | saturation: Float, 156 | value: Float, 157 | alpha: Float 158 | ) { 159 | val red = color.red.fractionToRGBRange() 160 | val green = color.green.fractionToRGBRange() 161 | val blue = color.blue.fractionToRGBRange() 162 | 163 | 164 | /* 165 | 🔥 Conversions 166 | */ 167 | 168 | // Convert Color into ColorInt then to alpha, red, green, blue array 169 | val colorInt = color.toArgb() 170 | val argbArrayFromColor: IntArray = ColorUtil.colorIntToARGBArray(colorInt) 171 | 172 | // Convert to RGB from HSV 173 | val rgbArray = HSVUtil.hsvToRGB(hue = hue, saturation = saturation, value = value) 174 | 175 | // Convert to HSL from HSV 176 | val hslArrayFromHSV = HSVUtil.hsvToHSL(hue, saturation, value) 177 | val hueHslFromHSV = hslArrayFromHSV[0].toInt() 178 | val saturationHslFromHSV = hslArrayFromHSV[1].fractionToPercent() 179 | val lightnessHslFromHSV = hslArrayFromHSV[2].fractionToPercent() 180 | 181 | // Convert to HSL from Color's red, green, blue 182 | // 🔥 This conversion does not give correct result all the time 183 | val hslArrayFromRGB = RGBUtil.rgbToHSL(red, green, blue) 184 | val hueHslFromRGB = hslArrayFromRGB[0].toInt() 185 | val saturationHslFromRGB = hslArrayFromRGB[1].fractionToPercent() 186 | val lightnessHslFromRGB = hslArrayFromRGB[2].fractionToPercent() 187 | 188 | // Convert to HSV from Color's red, green, blue 189 | // 🔥 This conversion does not give correct result all the time 190 | val hsvArrayFromRGB = RGBUtil.rgbToHSV(red, green, blue) 191 | val hueHsvFromRGB = hsvArrayFromRGB[0].toInt() 192 | val saturationHsvFromRGB = hsvArrayFromRGB[1].fractionToPercent() 193 | val valueHsvFromRGB = hsvArrayFromRGB[2].fractionToPercent() 194 | 195 | Text( 196 | "RAW Hue: ${hue}\n" + 197 | "RAW sat $saturation\n" + 198 | "RAW lightness: $value\n" + 199 | "HEX: ${colorInt.toArgbString()}" 200 | ) 201 | 202 | Text( 203 | "RGB from Color.toArgb()\n" + 204 | "alpha: ${argbArrayFromColor[0]}, " + 205 | "red: ${argbArrayFromColor[1]}, " + 206 | "green: ${argbArrayFromColor[2]}, " + 207 | "blue: ${argbArrayFromColor[3]}\n", 208 | ) 209 | 210 | // RGB Result Comparison 211 | val isRGBColorsSame = (red != rgbArray[0] || green != rgbArray[1] || blue != rgbArray[2]) 212 | 213 | Text( 214 | "RGB from Color\n" + 215 | "red: $red, green: $green, blue: $blue", 216 | color = if (isRGBColorsSame) Color.Red else Color.LightGray 217 | ) 218 | 219 | Text( 220 | "RGB from HSV\n" + 221 | "red: ${rgbArray[0]}, green: ${rgbArray[1]}, blue: ${rgbArray[2]}\n", 222 | color = if (isRGBColorsSame) Color.Red else Color.LightGray 223 | ) 224 | 225 | // 🔥 When RGB is Converted to HSV or HSL colors don't match with actual one 226 | // This applies to any color picker 227 | // colorpicker.me/ Check colors in this link 228 | 229 | // HSL Result Comparison 230 | val areHslColorsNotSame = 231 | (hueHslFromHSV != hueHslFromRGB || 232 | saturationHslFromHSV != saturationHslFromRGB || 233 | lightnessHslFromHSV != lightnessHslFromRGB 234 | ) 235 | 236 | Text( 237 | "HSL from HSV Conversion\n" + 238 | "hue: $hueHslFromHSV, " + 239 | "sat: $saturationHslFromHSV, " + 240 | "light: $lightnessHslFromHSV", 241 | color = if (areHslColorsNotSame) Color.Red else Color.LightGray 242 | ) 243 | 244 | Text( 245 | "HSL from RGB Conversion\n" + 246 | "hue: $hueHslFromRGB, " + 247 | "sat: $saturationHslFromRGB, " + 248 | "light: $lightnessHslFromRGB\n", 249 | color = if (areHslColorsNotSame) Color.Red else Color.LightGray 250 | ) 251 | 252 | // HSV Result Comparison 253 | val hueRounded = hue.toInt() 254 | val saturationRounded = saturation.fractionToPercent() 255 | val valueRounded = value.fractionToPercent() 256 | 257 | val areHSVColorsSame = (hueRounded == hueHsvFromRGB && 258 | saturationRounded == saturationHsvFromRGB && 259 | valueRounded == valueHsvFromRGB 260 | ) 261 | 262 | Text( 263 | "HSL RAW rounded\n" + 264 | "hue: $hueRounded, " + 265 | "sat: $saturationRounded, " + 266 | "value: $valueRounded", 267 | color = if (!areHSVColorsSame) Color.Red else Color.LightGray 268 | ) 269 | Text( 270 | "HSL from RGB Conversion\n" + 271 | "hue: $hueHsvFromRGB, " + 272 | "sat: $saturationHsvFromRGB, " + 273 | "value: $valueHsvFromRGB", 274 | color = if (!areHSVColorsSame) Color.Red else Color.LightGray 275 | ) 276 | 277 | } 278 | 279 | 280 | /** 281 | * This is an example for conversions from HSL to HSV, HSV to RGB and from RGB to HSV nd HSL 282 | * 283 | * 🔥 When RGB is Converted to HSV or HSL colors don't match with actual one 284 | * Check this online [color picker](colorpicker.me/) to cross-check results 285 | */ 286 | @Composable 287 | private fun CheckColorConversionDetailsFromHSL( 288 | color: Color, 289 | hue: Float, 290 | saturation: Float, 291 | lightness: Float, 292 | alpha: Float 293 | ) { 294 | 295 | val red = color.red.fractionToRGBRange() 296 | val green = color.green.fractionToRGBRange() 297 | val blue = color.blue.fractionToRGBRange() 298 | 299 | 300 | /* 301 | 🔥 Conversions 302 | */ 303 | 304 | // Convert Color into ColorInt then to alpha, red, green, blue array 305 | val colorInt = color.toArgb() 306 | val argbArrayFromColor: IntArray = ColorUtil.colorIntToARGBArray(colorInt) 307 | 308 | // Convert to RGB from HSL 309 | val rgbArray = HSLUtil.hslToRGB(hue = hue, saturation = saturation, lightness = lightness) 310 | 311 | // Convert to HSV from HSL 312 | val hsvArrayFromHSL = HSLUtil.hslToHSV(hue, saturation, lightness) 313 | val hueHsvFromHSL = hsvArrayFromHSL[0].toInt() 314 | val saturationHsvFromHSL = hsvArrayFromHSL[1].fractionToPercent() 315 | val valueHsvFromHSL = hsvArrayFromHSL[2].fractionToPercent() 316 | 317 | // Convert to HSV from Color's red, green, blue 318 | // 🔥 This conversion does not give correct result all the time 319 | val hsvArrayFromRGB = RGBUtil.rgbToHSV(red, green, blue) 320 | val hueHsvFromRGB = hsvArrayFromRGB[0].toInt() 321 | val saturationHsvFromRGB = hsvArrayFromRGB[1].fractionToPercent() 322 | val valueHsvFromRGB = hsvArrayFromRGB[2].fractionToPercent() 323 | 324 | // Convert to HSL from Color's red, green, blue 325 | // 🔥 This conversion does not give correct result all the time 326 | val hslArrayFromRGB = RGBUtil.rgbToHSL(red, green, blue) 327 | val hueHslFromRGB = hslArrayFromRGB[0].toInt() 328 | val saturationHslFromRGB = hslArrayFromRGB[1].fractionToPercent() 329 | val lightnessHslFromRGB = hslArrayFromRGB[2].fractionToPercent() 330 | 331 | 332 | Text( 333 | "RAW Hue: ${hue}\n" + 334 | "RAW sat $saturation\n" + 335 | "RAW lightness: $lightness\n" + 336 | "HEX: ${colorInt.toArgbString()}" 337 | ) 338 | 339 | Text( 340 | "RGB from Color.toArgb()\n" + 341 | "alpha: ${argbArrayFromColor[0]}, " + 342 | "red: ${argbArrayFromColor[1]}, " + 343 | "green: ${argbArrayFromColor[2]}, " + 344 | "blue: ${argbArrayFromColor[3]}\n", 345 | ) 346 | 347 | // RGB Result Comparison 348 | val areRGBColorsSame = (red != rgbArray[0] || green != rgbArray[1] || blue != rgbArray[2]) 349 | Text( 350 | "RGB from Color\n" + 351 | "red: $red, green: $green, blue: $blue", 352 | color = if (areRGBColorsSame) Color.Red else Color.LightGray 353 | ) 354 | 355 | Text( 356 | "RGB from HSL\n" + 357 | "red: ${rgbArray[0]}, green: ${rgbArray[1]}, blue: ${rgbArray[2]}\n", 358 | color = if (areRGBColorsSame) Color.Red else Color.LightGray 359 | ) 360 | 361 | // HSV Result Comparison 362 | val areHsvColorsSame = 363 | (hueHsvFromHSL == hueHsvFromRGB && 364 | saturationHsvFromHSL == saturationHsvFromRGB && 365 | valueHsvFromHSL == valueHsvFromRGB) 366 | 367 | Text( 368 | "HSV from HSL Conversion\n" + 369 | "hue: $hueHsvFromHSL, " + 370 | "sat: $saturationHsvFromHSL, " + 371 | "value: $valueHsvFromHSL", 372 | color = if (!areHsvColorsSame) Color.Red else Color.LightGray 373 | ) 374 | 375 | Text( 376 | "HSV from RGB Conversion\n" + 377 | "hue: $hueHsvFromRGB, " + 378 | "sat: $saturationHsvFromRGB, " + 379 | "value: $valueHsvFromRGB\n", 380 | color = if (!areHsvColorsSame) Color.Red else Color.LightGray 381 | ) 382 | 383 | // HSL Result Comparison 384 | val hueRounded = hue.toInt() 385 | val saturationRounded = saturation.fractionToPercent() 386 | val lightnessRounded = lightness.fractionToPercent() 387 | 388 | val areHSLColorsSame = (hueRounded == hueHslFromRGB && 389 | saturationRounded == saturationHslFromRGB && 390 | lightnessRounded == lightnessHslFromRGB 391 | ) 392 | 393 | Text( 394 | "HSL RAW rounded\n" + 395 | "hue: $hueRounded, " + 396 | "sat: $saturationRounded, " + 397 | "light: $lightnessRounded", 398 | color = if (!areHSLColorsSame) Color.Red else Color.LightGray 399 | ) 400 | Text( 401 | "HSL from RGB Conversion\n" + 402 | "hue: $hueHslFromRGB, " + 403 | "sat: $saturationHslFromRGB, " + 404 | "light: $lightnessHslFromRGB", 405 | color = if (!areHSLColorsSame) Color.Red else Color.LightGray 406 | ) 407 | } 408 | 409 | -------------------------------------------------------------------------------- /app/src/main/java/com/smarttoolfactory/composecolorsextended/M3ColorPicker.kt: -------------------------------------------------------------------------------- 1 | package com.smarttoolfactory.composecolorsextended 2 | 3 | import android.content.Context 4 | import android.widget.Toast 5 | import androidx.compose.foundation.background 6 | import androidx.compose.foundation.clickable 7 | import androidx.compose.foundation.layout.* 8 | import androidx.compose.foundation.lazy.grid.GridCells 9 | import androidx.compose.foundation.lazy.grid.LazyVerticalGrid 10 | import androidx.compose.foundation.lazy.grid.itemsIndexed 11 | import androidx.compose.foundation.shape.RoundedCornerShape 12 | import androidx.compose.material.* 13 | import androidx.compose.material.icons.Icons 14 | import androidx.compose.material.icons.filled.Check 15 | import androidx.compose.runtime.* 16 | import androidx.compose.ui.Alignment 17 | import androidx.compose.ui.Modifier 18 | import androidx.compose.ui.draw.clip 19 | import androidx.compose.ui.graphics.Color 20 | import androidx.compose.ui.platform.ClipboardManager 21 | import androidx.compose.ui.platform.LocalClipboardManager 22 | import androidx.compose.ui.platform.LocalContext 23 | import androidx.compose.ui.res.painterResource 24 | import androidx.compose.ui.text.AnnotatedString 25 | import androidx.compose.ui.text.font.FontWeight 26 | import androidx.compose.ui.text.style.TextAlign 27 | import androidx.compose.ui.unit.dp 28 | import androidx.compose.ui.unit.sp 29 | import com.smarttoolfactory.extendedcolors.ColorSwatch 30 | import com.smarttoolfactory.extendedcolors.parser.rememberColorParser 31 | import com.smarttoolfactory.extendedcolors.util.* 32 | import kotlinx.coroutines.Dispatchers 33 | import kotlinx.coroutines.flow.distinctUntilChanged 34 | import kotlinx.coroutines.flow.flowOn 35 | import kotlinx.coroutines.flow.mapLatest 36 | 37 | 38 | @Composable 39 | fun M3ColorPicker(onColorChange: (Color) -> Unit) { 40 | 41 | 42 | Column( 43 | modifier = Modifier 44 | .fillMaxSize() 45 | .background(Color.Black.copy(alpha = .8f)), 46 | horizontalAlignment = Alignment.CenterHorizontally 47 | ) { 48 | 49 | val colorNameParser = rememberColorParser() 50 | 51 | // Show primary or accent colors 52 | var primaryAccentSelection by remember { 53 | mutableStateOf(0) 54 | } 55 | 56 | // Index of selected list as main and index of color in that list 57 | val colorSelectionIndex = 58 | remember { ColorSelectionIndex(mainSelection = 0, subSelection = 0) } 59 | 60 | // This is the selected color swatch in top grid which is swatch/list of colors 61 | var colorSwatchIndex by remember { mutableStateOf(0) } 62 | 63 | val colorSwatch: LinkedHashMap = 64 | remember( 65 | colorSwatchIndex, 66 | primaryAccentSelection 67 | ) { 68 | if (primaryAccentSelection == 0) 69 | ColorSwatch.primaryColorSwatches[colorSwatchIndex] else 70 | ColorSwatch.accentColorSwatches[colorSwatchIndex] 71 | } 72 | 73 | // Get keys and colors for second grid to display Material Design 2 keys 74 | // and shades like 100, 200, 900 75 | val m2ColorIndices: List = colorSwatch.keys.toList() 76 | val m2ColorList: List = colorSwatch.values.toList() 77 | 78 | // Current color picked by user or initial color as Red500 79 | var currentColor by remember { mutableStateOf(ColorSwatch.primaryHeaderColors.first()) } 80 | // Always get Material Design3 tonal palette from color picked recently from 81 | // first or second grid 82 | var m3Tones by remember { mutableStateOf(getColorTonesList(currentColor)) } 83 | // Name of the color that is currently picked 84 | var colorName by remember { mutableStateOf("") } 85 | 86 | LaunchedEffect(key1 = colorNameParser) { 87 | 88 | snapshotFlow { currentColor } 89 | .distinctUntilChanged() 90 | .mapLatest { color: Color -> 91 | colorNameParser.parseColorName(color) 92 | } 93 | .flowOn(Dispatchers.Default) 94 | .collect { name: String -> 95 | colorName = name 96 | } 97 | } 98 | 99 | Spacer(modifier = Modifier.height(20.dp)) 100 | PrimaryAccentSelectionTab( 101 | modifier = Modifier.width(300.dp), 102 | selectedIndex = primaryAccentSelection 103 | ) { 104 | primaryAccentSelection = it 105 | colorSelectionIndex.mainSelection = 0 106 | colorSelectionIndex.subSelection = 0 107 | colorSwatchIndex = 0 108 | currentColor = ColorSwatch.primaryHeaderColors.first() 109 | } 110 | 111 | Spacer(modifier = Modifier.height(10.dp)) 112 | 113 | // Color Swatch Selection 114 | ColorSelectionGrid( 115 | columns = GridCells.Fixed(8), 116 | contentPadding = PaddingValues(8.dp), 117 | verticalArrangement = Arrangement.spacedBy(4.dp), 118 | horizontalArrangement = Arrangement.spacedBy(4.dp), 119 | colorSelectionList = if (primaryAccentSelection == 0) 120 | ColorSwatch.primaryHeaderColors else ColorSwatch.accentHeaderColors, 121 | selected = { index -> 122 | (colorSelectionIndex.mainSelection == 0 && 123 | colorSelectionIndex.subSelection == index) || ( 124 | primaryAccentSelection == 0 && colorSelectionIndex.mainSelection == 1 && 125 | colorSelectionIndex.subSelection == 5 && 126 | index == colorSwatchIndex 127 | ) 128 | }, 129 | tint = { Color.Unspecified }, 130 | ) { index: Int, item: Color -> 131 | currentColor = item 132 | onColorChange(item) 133 | colorSelectionIndex.mainSelection = 0 134 | colorSelectionIndex.subSelection = index 135 | colorSwatchIndex = index 136 | m3Tones = getColorTonesList(item) 137 | } 138 | 139 | Text( 140 | text = "Material Design2 Shade", 141 | modifier = Modifier 142 | .fillMaxWidth() 143 | .padding(5.dp), 144 | textAlign = TextAlign.Center, 145 | fontSize = 16.sp, 146 | fontWeight = FontWeight.Bold, 147 | color = Color.White 148 | ) 149 | 150 | // Primary/Accent Color Selection 151 | ColorSelectionGrid( 152 | modifier = Modifier.fillMaxWidth(if (primaryAccentSelection == 0) 1f else .5f), 153 | columns = GridCells.Fixed(if (primaryAccentSelection == 0) 8 else 4), 154 | contentPadding = PaddingValues(8.dp), 155 | verticalArrangement = Arrangement.spacedBy(4.dp), 156 | horizontalArrangement = Arrangement.spacedBy(4.dp), 157 | colorSelectionList = m2ColorList, 158 | colorKeys = m2ColorIndices, 159 | selected = { index -> 160 | (primaryAccentSelection == 0 && colorSelectionIndex.mainSelection == 0 && index == 5) 161 | || (colorSelectionIndex.mainSelection == 1 162 | && colorSelectionIndex.subSelection == index) 163 | }, 164 | tint = { index -> 165 | if (index < 5) Color.Black else Color.White 166 | }, 167 | 168 | ) { index: Int, item: Color -> 169 | currentColor = item 170 | colorSelectionIndex.mainSelection = 1 171 | colorSelectionIndex.subSelection = index 172 | onColorChange(item) 173 | m3Tones = getColorTonesList(item) 174 | } 175 | 176 | Text( 177 | text = "Material Design3 Tonal Palette", 178 | modifier = Modifier 179 | .fillMaxWidth() 180 | .padding(5.dp), 181 | textAlign = TextAlign.Center, 182 | fontSize = 16.sp, 183 | fontWeight = FontWeight.Bold, 184 | color = Color.White 185 | ) 186 | 187 | 188 | // M3 Tone Selection 189 | ColorSelectionGrid( 190 | columns = GridCells.Fixed(8), 191 | contentPadding = PaddingValues(8.dp), 192 | verticalArrangement = Arrangement.spacedBy(4.dp), 193 | horizontalArrangement = Arrangement.spacedBy(4.dp), 194 | colorSelectionList = m3Tones, 195 | colorKeys = material3ToneRange, 196 | selected = { index -> 197 | (colorSelectionIndex.mainSelection == 2 198 | && colorSelectionIndex.subSelection == index) 199 | }, 200 | tint = { index -> 201 | if (index < 6) Color.White else Color.Black 202 | }, 203 | ) { index: Int, item: Color -> 204 | colorSelectionIndex.mainSelection = 2 205 | colorSelectionIndex.subSelection = index 206 | currentColor = item 207 | onColorChange(item) 208 | } 209 | 210 | 211 | Spacer(modifier = Modifier.height(30.dp)) 212 | 213 | Text( 214 | text = colorName, 215 | modifier = Modifier 216 | .fillMaxWidth() 217 | .padding(2.dp), 218 | textAlign = TextAlign.Center, 219 | fontSize = 20.sp, 220 | fontWeight = FontWeight.Bold, 221 | color = Color.White 222 | ) 223 | 224 | 225 | ColorDisplayWithClipboard(currentColor) 226 | } 227 | } 228 | 229 | @Composable 230 | private fun ColorDisplayWithClipboard( 231 | currentColor: Color 232 | ) { 233 | val clipboardManager = LocalClipboardManager.current 234 | val context = LocalContext.current 235 | 236 | val lightness = ColorUtil.colorToHSL(currentColor)[2] 237 | val textColor = if (lightness < .6f) Color.White else Color.Black 238 | 239 | Row( 240 | modifier = Modifier 241 | .padding(8.dp) 242 | .background(color = currentColor, RoundedCornerShape(50)) 243 | .padding(horizontal = 20.dp, vertical = 10.dp), 244 | verticalAlignment = Alignment.CenterVertically 245 | ) { 246 | 247 | val hexText = ColorUtil.colorToHex(color = currentColor) 248 | Text( 249 | text = hexText, 250 | fontSize = 24.sp, 251 | color = textColor 252 | ) 253 | Spacer(modifier = Modifier.width(20.dp)) 254 | IconButton(onClick = { 255 | Toast.makeText(context, "Copied $hexText", Toast.LENGTH_SHORT).show() 256 | clipboardManager.setText(AnnotatedString(hexText)) 257 | }) { 258 | Icon( 259 | tint = textColor, 260 | painter = painterResource(id = R.drawable.ic_baseline_content_copy_24), 261 | contentDescription = "clipboard" 262 | ) 263 | } 264 | } 265 | } 266 | 267 | @Composable 268 | fun ColorSelectionGrid( 269 | modifier: Modifier = Modifier, 270 | columns: GridCells = GridCells.Fixed(8), 271 | contentPadding: PaddingValues = PaddingValues(8.dp), 272 | verticalArrangement: Arrangement.Vertical = Arrangement.spacedBy(4.dp), 273 | horizontalArrangement: Arrangement.Horizontal = Arrangement.spacedBy(4.dp), 274 | colorSelectionList: List, 275 | colorKeys: List? = null, 276 | selected: (Int) -> Boolean, 277 | tint: (Int) -> Color, 278 | onClick: (Int, Color) -> Unit, 279 | ) { 280 | LazyVerticalGrid( 281 | modifier = modifier, 282 | columns = columns, 283 | contentPadding = contentPadding, 284 | verticalArrangement = verticalArrangement, 285 | horizontalArrangement = horizontalArrangement, 286 | ) { 287 | itemsIndexed(colorSelectionList) { index: Int, item: Color -> 288 | 289 | ColorDisplayWithTitle( 290 | modifier = Modifier 291 | .aspectRatio(1f) 292 | .clip(RoundedCornerShape(8.dp)) 293 | .aspectRatio(1f) 294 | .clickable { 295 | onClick(index, item) 296 | }, 297 | backgroundColor = item, 298 | selected = selected(index), 299 | tint = tint(index), 300 | title = colorKeys?.get(index)?.toString() ?: "" 301 | ) 302 | } 303 | } 304 | } 305 | 306 | @Composable 307 | fun PrimaryAccentSelectionTab( 308 | modifier: Modifier = Modifier, 309 | selectedIndex: Int, 310 | onTabChange: (Int) -> Unit 311 | ) { 312 | 313 | val list = listOf("Primary", "Accent") 314 | 315 | TabRow(selectedTabIndex = selectedIndex, 316 | backgroundColor = Color.DarkGray, 317 | modifier = modifier.clip(RoundedCornerShape(10.dp)), 318 | indicator = {} 319 | ) { 320 | list.forEachIndexed { index, text -> 321 | val selected = selectedIndex == index 322 | Tab( 323 | modifier = if (selected) Modifier 324 | .clip(RoundedCornerShape(10.dp)) 325 | .background(Color(0xff6FAAEE)) 326 | else Modifier 327 | .clip(RoundedCornerShape(10.dp)) 328 | .background(Color.DarkGray), 329 | selected = selected, 330 | onClick = { 331 | onTabChange(index) 332 | }, 333 | text = { 334 | Text( 335 | text = text, color = if (selected) Color.White 336 | else Color.White.copy(.5f) 337 | ) 338 | } 339 | ) 340 | } 341 | } 342 | } 343 | 344 | 345 | @Composable 346 | fun ColorDisplayWithTitle( 347 | modifier: Modifier, 348 | title: String = "", 349 | selected: Boolean, 350 | tint: Color = Color.Unspecified, 351 | backgroundColor: Color 352 | ) { 353 | Box( 354 | contentAlignment = Alignment.Center 355 | ) { 356 | Box( 357 | modifier = modifier 358 | .background(backgroundColor) 359 | ) 360 | 361 | if (title.isNotEmpty()) { 362 | Text(text = title, color = tint, fontSize = 16.sp) 363 | } 364 | 365 | if (selected) { 366 | Icon( 367 | modifier = modifier 368 | .background(tint.copy(alpha = .5f)) 369 | .padding(4.dp), 370 | imageVector = Icons.Default.Check, 371 | contentDescription = "check", 372 | tint = Color.Green 373 | ) 374 | } 375 | } 376 | } 377 | 378 | @Composable 379 | fun ColorDisplayWithIcon( 380 | modifier: Modifier, 381 | selected: Boolean, 382 | contentColor: Color = Color.Unspecified, 383 | backgroundColor: Color 384 | ) { 385 | Box( 386 | contentAlignment = Alignment.Center 387 | ) { 388 | Box( 389 | modifier = modifier 390 | .background(backgroundColor) 391 | ) 392 | 393 | if (selected) { 394 | Icon( 395 | modifier = modifier 396 | .background(contentColor.copy(alpha = .5f)) 397 | .padding(4.dp), 398 | imageVector = Icons.Default.Check, 399 | contentDescription = "check", 400 | tint = Color.Green 401 | ) 402 | } 403 | } 404 | } 405 | 406 | data class ColorSelectionIndex(var mainSelection: Int = 0, var subSelection: Int = 0) -------------------------------------------------------------------------------- /extendedcolors/src/main/java/com/smarttoolfactory/extendedcolors/ColorSwatch.kt: -------------------------------------------------------------------------------- 1 | package com.smarttoolfactory.extendedcolors 2 | 3 | import androidx.compose.ui.graphics.Color 4 | 5 | object ColorSwatch { 6 | 7 | val primaryHeaderColors by lazy { 8 | listOf( 9 | Color(0xffF44336), 10 | Color(0xffE91E63), 11 | Color(0xff9C27B0), 12 | Color(0xff673AB7), 13 | Color(0xff3F51B5), 14 | Color(0xff2196F3), 15 | Color(0xff03A9F4), 16 | Color(0xff00BCD4), 17 | Color(0xff009688), 18 | Color(0xff4CAF50), 19 | Color(0xff8BC34A), 20 | Color(0xffCDDC39), 21 | Color(0xffFFEB3B), 22 | Color(0xffFFC107), 23 | Color(0xffFF9800), 24 | Color(0xffFF5722), 25 | Color(0xff795548), 26 | Color(0xff9E9E9E), 27 | Color(0xff607D8B) 28 | ) 29 | } 30 | 31 | val accentHeaderColors by lazy { 32 | listOf( 33 | Color(0xffF44336), 34 | Color(0xffE91E63), 35 | Color(0xff9C27B0), 36 | Color(0xff673AB7), 37 | Color(0xff3F51B5), 38 | Color(0xff2196F3), 39 | Color(0xff03A9F4), 40 | Color(0xff00BCD4), 41 | Color(0xff009688), 42 | Color(0xff4CAF50), 43 | Color(0xff8BC34A), 44 | Color(0xffCDDC39), 45 | Color(0xffFFEB3B), 46 | Color(0xffFFC107), 47 | Color(0xffFF9800), 48 | Color(0xffFF5722), 49 | ) 50 | } 51 | 52 | val primaryColorSwatches by lazy { 53 | listOf( 54 | red, 55 | pink, 56 | purple, 57 | deepPurple, 58 | indigo, 59 | blue, 60 | lightBlue, 61 | cyan, 62 | teal, 63 | green, 64 | lightGreen, 65 | lime, 66 | yellow, 67 | amber, 68 | orange, 69 | deepOrange, 70 | brown, 71 | grey, 72 | blueGrey 73 | ) 74 | } 75 | 76 | 77 | val accentColorSwatches by lazy { 78 | listOf( 79 | redAccent, 80 | pinkAccent, 81 | purpleAccent, 82 | deepPurpleAccent, 83 | indigoAccent, 84 | blueAccent, 85 | lightBlueAccent, 86 | cyanAccent, 87 | tealAccent, 88 | greenAccent, 89 | lightGreenAccent, 90 | limeAccent, 91 | yellowAccent, 92 | amberAccent, 93 | orangeAccent, 94 | deepOrangeAccent, 95 | ) 96 | } 97 | 98 | val red by lazy { 99 | linkedMapOf( 100 | 50 to Color(0xffFFEBEE), 101 | 100 to Color(0xffFFCDD2), 102 | 200 to Color(0xffEF9A9A), 103 | 300 to Color(0xffE57373), 104 | 400 to Color(0xffEF5350), 105 | 500 to Color(0xffF44336), 106 | 600 to Color(0xffE53935), 107 | 700 to Color(0xffD32F2F), 108 | 800 to Color(0xffC62828), 109 | 900 to Color(0xffB71C1C) 110 | ) 111 | } 112 | 113 | val redAccent by lazy { 114 | linkedMapOf( 115 | 100 to Color(0xffFF8A80), 116 | 200 to Color(0xffFF5252), 117 | 400 to Color(0xffFF1744), 118 | 700 to Color(0xffD50000) 119 | ) 120 | } 121 | 122 | val pink by lazy { 123 | linkedMapOf( 124 | 50 to Color(0xffFCE4EC), 125 | 100 to Color(0xffF8BBD0), 126 | 200 to Color(0xffF48FB1), 127 | 300 to Color(0xffF06292), 128 | 400 to Color(0xffEC407A), 129 | 500 to Color(0xffE91E63), 130 | 600 to Color(0xffD81B60), 131 | 700 to Color(0xffC2185B), 132 | 800 to Color(0xffAD1457), 133 | 900 to Color(0xff880E4F) 134 | ) 135 | } 136 | 137 | val pinkAccent by lazy { 138 | linkedMapOf( 139 | 100 to Color(0xffFF80AB), 140 | 200 to Color(0xffFF4081), 141 | 400 to Color(0xffF50057), 142 | 700 to Color(0xffC51162) 143 | ) 144 | } 145 | 146 | val purple by lazy { 147 | linkedMapOf( 148 | 50 to Color(0xffF3E5F5), 149 | 100 to Color(0xffE1BEE7), 150 | 200 to Color(0xffCE93D8), 151 | 300 to Color(0xffBA68C8), 152 | 400 to Color(0xffAB47BC), 153 | 500 to Color(0xff9C27B0), 154 | 600 to Color(0xff8E24AA), 155 | 700 to Color(0xff7B1FA2), 156 | 800 to Color(0xff6A1B9A), 157 | 900 to Color(0xff4A148C) 158 | ) 159 | } 160 | 161 | val purpleAccent by lazy { 162 | linkedMapOf( 163 | 100 to Color(0xffEA80FC), 164 | 200 to Color(0xffE040FB), 165 | 400 to Color(0xffD500F9), 166 | 700 to Color(0xffAA00FF) 167 | ) 168 | } 169 | 170 | val deepPurple by lazy { 171 | linkedMapOf( 172 | 50 to Color(0xffEDE7F6), 173 | 100 to Color(0xffD1C4E9), 174 | 200 to Color(0xffB39DDB), 175 | 300 to Color(0xff9575CD), 176 | 400 to Color(0xff7E57C2), 177 | 500 to Color(0xff673AB7), 178 | 600 to Color(0xff5E35B1), 179 | 700 to Color(0xff512DA8), 180 | 800 to Color(0xff4527A0), 181 | 900 to Color(0xff311B92) 182 | ) 183 | } 184 | 185 | val deepPurpleAccent by lazy { 186 | linkedMapOf( 187 | 100 to Color(0xffB388FF), 188 | 200 to Color(0xff7C4DFF), 189 | 400 to Color(0xff651FFF), 190 | 700 to Color(0xff6200EA) 191 | ) 192 | } 193 | 194 | val indigo by lazy { 195 | linkedMapOf( 196 | 50 to Color(0xffE8EAF6), 197 | 100 to Color(0xffC5CAE9), 198 | 200 to Color(0xff9FA8DA), 199 | 300 to Color(0xff7986CB), 200 | 400 to Color(0xff5C6BC0), 201 | 500 to Color(0xff3F51B5), 202 | 600 to Color(0xff3949AB), 203 | 700 to Color(0xff303F9F), 204 | 800 to Color(0xff283593), 205 | 900 to Color(0xff1A237E) 206 | ) 207 | } 208 | 209 | val indigoAccent by lazy { 210 | linkedMapOf( 211 | 100 to Color(0xff8C9EFF), 212 | 200 to Color(0xff536DFE), 213 | 400 to Color(0xff3D5AFE), 214 | 700 to Color(0xff304FFE) 215 | ) 216 | } 217 | 218 | val blue by lazy { 219 | linkedMapOf( 220 | 50 to Color(0xffE3F2FD), 221 | 100 to Color(0xffBBDEFB), 222 | 200 to Color(0xff90CAF9), 223 | 300 to Color(0xff64B5F6), 224 | 400 to Color(0xff42A5F5), 225 | 500 to Color(0xff2196F3), 226 | 600 to Color(0xff1E88E5), 227 | 700 to Color(0xff1976D2), 228 | 800 to Color(0xff1565C0), 229 | 900 to Color(0xff0D47A1) 230 | ) 231 | } 232 | 233 | val blueAccent by lazy { 234 | linkedMapOf( 235 | 100 to Color(0xff82B1FF), 236 | 200 to Color(0xff448AFF), 237 | 400 to Color(0xff2979FF), 238 | 700 to Color(0xff2962FF) 239 | ) 240 | } 241 | val lightBlue by lazy { 242 | linkedMapOf( 243 | 50 to Color(0xffE1F5FE), 244 | 100 to Color(0xffB3E5FC), 245 | 200 to Color(0xff81D4FA), 246 | 300 to Color(0xff4FC3F7), 247 | 400 to Color(0xff29B6F6), 248 | 500 to Color(0xff03A9F4), 249 | 600 to Color(0xff039BE5), 250 | 700 to Color(0xff0288D1), 251 | 800 to Color(0xff0277BD), 252 | 900 to Color(0xff01579B) 253 | ) 254 | } 255 | 256 | val lightBlueAccent by lazy { 257 | linkedMapOf( 258 | 100 to Color(0xff80D8FF), 259 | 200 to Color(0xff40C4FF), 260 | 400 to Color(0xff00B0FF), 261 | 700 to Color(0xff0091EA) 262 | ) 263 | } 264 | 265 | val cyan by lazy { 266 | linkedMapOf( 267 | 50 to Color(0xffE0F7FA), 268 | 100 to Color(0xffB2EBF2), 269 | 200 to Color(0xff80DEEA), 270 | 300 to Color(0xff4DD0E1), 271 | 400 to Color(0xff26C6DA), 272 | 500 to Color(0xff00BCD4), 273 | 600 to Color(0xff00ACC1), 274 | 700 to Color(0xff0097A7), 275 | 800 to Color(0xff00838F), 276 | 900 to Color(0xff006064) 277 | ) 278 | } 279 | 280 | val cyanAccent by lazy { 281 | linkedMapOf( 282 | 100 to Color(0xff84FFFF), 283 | 200 to Color(0xff18FFFF), 284 | 400 to Color(0xff00E5FF), 285 | 700 to Color(0xff00B8D4) 286 | ) 287 | } 288 | 289 | val teal by lazy { 290 | linkedMapOf( 291 | 50 to Color(0xffE0F2F1), 292 | 100 to Color(0xffB2DFDB), 293 | 200 to Color(0xff80CBC4), 294 | 300 to Color(0xff4DB6AC), 295 | 400 to Color(0xff26A69A), 296 | 500 to Color(0xff009688), 297 | 600 to Color(0xff00897B), 298 | 700 to Color(0xff00796B), 299 | 800 to Color(0xff00695C), 300 | 900 to Color(0xff004D40) 301 | ) 302 | } 303 | 304 | val tealAccent by lazy { 305 | linkedMapOf( 306 | 100 to Color(0xffA7FFEB), 307 | 200 to Color(0xff64FFDA), 308 | 400 to Color(0xff1DE9B6), 309 | 700 to Color(0xff00BFA5) 310 | ) 311 | } 312 | 313 | val green by lazy { 314 | linkedMapOf( 315 | 50 to Color(0xffE8F5E9), 316 | 100 to Color(0xffC8E6C9), 317 | 200 to Color(0xffA5D6A7), 318 | 300 to Color(0xff81C784), 319 | 400 to Color(0xff66BB6A), 320 | 500 to Color(0xff4CAF50), 321 | 600 to Color(0xff43A047), 322 | 700 to Color(0xff388E3C), 323 | 800 to Color(0xff2E7D32), 324 | 900 to Color(0xff1B5E20) 325 | ) 326 | } 327 | 328 | val greenAccent by lazy { 329 | linkedMapOf( 330 | 100 to Color(0xffB9F6CA), 331 | 200 to Color(0xff69F0AE), 332 | 400 to Color(0xff00E676), 333 | 700 to Color(0xff00C853) 334 | ) 335 | } 336 | 337 | val lightGreen by lazy { 338 | linkedMapOf( 339 | 50 to Color(0xffF1F8E9), 340 | 100 to Color(0xffDCEDC8), 341 | 200 to Color(0xffC5E1A5), 342 | 300 to Color(0xffAED581), 343 | 400 to Color(0xff9CCC65), 344 | 500 to Color(0xff8BC34A), 345 | 600 to Color(0xff7CB342), 346 | 700 to Color(0xff689F38), 347 | 800 to Color(0xff558B2F), 348 | 900 to Color(0xff33691E) 349 | ) 350 | } 351 | 352 | val lightGreenAccent by lazy { 353 | linkedMapOf( 354 | 100 to Color(0xffCCFF90), 355 | 200 to Color(0xffB2FF59), 356 | 400 to Color(0xff76FF03), 357 | 700 to Color(0xff64DD17) 358 | ) 359 | } 360 | 361 | val lime by lazy { 362 | linkedMapOf( 363 | 50 to Color(0xffF9FBE7), 364 | 100 to Color(0xffF0F4C3), 365 | 200 to Color(0xffE6EE9C), 366 | 300 to Color(0xffDCE775), 367 | 400 to Color(0xffD4E157), 368 | 500 to Color(0xffCDDC39), 369 | 600 to Color(0xffC0CA33), 370 | 700 to Color(0xffAFB42B), 371 | 800 to Color(0xff9E9D24), 372 | 900 to Color(0xff827717) 373 | ) 374 | } 375 | 376 | val limeAccent by lazy { 377 | linkedMapOf( 378 | 100 to Color(0xffF4FF81), 379 | 200 to Color(0xffEEFF41), 380 | 400 to Color(0xffC6FF00), 381 | 700 to Color(0xffAEEA00) 382 | ) 383 | } 384 | 385 | 386 | val yellow by lazy { 387 | linkedMapOf( 388 | 50 to Color(0xffFFFDE7), 389 | 100 to Color(0xffFFF9C4), 390 | 200 to Color(0xffFFF59D), 391 | 300 to Color(0xffFFF176), 392 | 400 to Color(0xffFFEE58), 393 | 500 to Color(0xffFFEB3B), 394 | 600 to Color(0xffFDD835), 395 | 700 to Color(0xffFBC02D), 396 | 800 to Color(0xffF9A825), 397 | 900 to Color(0xffF57F17) 398 | ) 399 | } 400 | 401 | val yellowAccent by lazy { 402 | linkedMapOf( 403 | 100 to Color(0xffFFFF8D), 404 | 200 to Color(0xffFFFF00), 405 | 400 to Color(0xffFFEA00), 406 | 700 to Color(0xffFFD600) 407 | ) 408 | } 409 | 410 | val amber by lazy { 411 | linkedMapOf( 412 | 50 to Color(0xffFFF8E1), 413 | 100 to Color(0xffFFECB3), 414 | 200 to Color(0xffFFE082), 415 | 300 to Color(0xffFFD54F), 416 | 400 to Color(0xffFFCA28), 417 | 500 to Color(0xffFFC107), 418 | 600 to Color(0xffFFB300), 419 | 700 to Color(0xffFFA000), 420 | 800 to Color(0xffFF8F00), 421 | 900 to Color(0xffFF6F00) 422 | ) 423 | } 424 | 425 | val amberAccent by lazy { 426 | linkedMapOf( 427 | 100 to Color(0xffFFE57F), 428 | 200 to Color(0xffFFD740), 429 | 400 to Color(0xffFFC400), 430 | 700 to Color(0xffFFAB00) 431 | ) 432 | } 433 | 434 | val orange by lazy { 435 | linkedMapOf( 436 | 50 to Color(0xffFFF3E0), 437 | 100 to Color(0xffFFE0B2), 438 | 200 to Color(0xffFFCC80), 439 | 300 to Color(0xffFFB74D), 440 | 400 to Color(0xffFFA726), 441 | 500 to Color(0xffFF9800), 442 | 600 to Color(0xffFB8C00), 443 | 700 to Color(0xffF57C00), 444 | 800 to Color(0xffEF6C00), 445 | 900 to Color(0xffE65100) 446 | ) 447 | } 448 | 449 | val orangeAccent by lazy { 450 | linkedMapOf( 451 | 100 to Color(0xffFFD180), 452 | 200 to Color(0xffFFAB40), 453 | 400 to Color(0xffFF9100), 454 | 700 to Color(0xffFF6D00) 455 | ) 456 | } 457 | 458 | val deepOrange by lazy { 459 | linkedMapOf( 460 | 50 to Color(0xffFBE9E7), 461 | 100 to Color(0xffFFCCBC), 462 | 200 to Color(0xffFFAB91), 463 | 300 to Color(0xffFF8A65), 464 | 400 to Color(0xffFF7043), 465 | 500 to Color(0xffFF5722), 466 | 600 to Color(0xffF4511E), 467 | 700 to Color(0xffE64A19), 468 | 800 to Color(0xffD84315), 469 | 900 to Color(0xffBF360C) 470 | ) 471 | } 472 | 473 | val deepOrangeAccent by lazy { 474 | linkedMapOf( 475 | 100 to Color(0xffFF9E80), 476 | 200 to Color(0xffFF6E40), 477 | 400 to Color(0xffFF3D00), 478 | 700 to Color(0xffDD2C00) 479 | ) 480 | } 481 | 482 | val brown by lazy { 483 | linkedMapOf( 484 | 50 to Color(0xffEFEBE9), 485 | 100 to Color(0xffD7CCC8), 486 | 200 to Color(0xffBCAAA4), 487 | 300 to Color(0xffA1887F), 488 | 400 to Color(0xff8D6E63), 489 | 500 to Color(0xff795548), 490 | 600 to Color(0xff6D4C41), 491 | 700 to Color(0xff5D4037), 492 | 800 to Color(0xff4E342E), 493 | 900 to Color(0xff3E2723) 494 | ) 495 | } 496 | 497 | val grey by lazy { 498 | linkedMapOf( 499 | 50 to Color(0xffFAFAFA), 500 | 100 to Color(0xffF5F5F5), 501 | 200 to Color(0xffEEEEEE), 502 | 300 to Color(0xffE0E0E0), 503 | 400 to Color(0xffBDBDBD), 504 | 500 to Color(0xff9E9E9E), 505 | 600 to Color(0xff757575), 506 | 700 to Color(0xff616161), 507 | 800 to Color(0xff424242), 508 | 900 to Color(0xff212121) 509 | ) 510 | } 511 | 512 | val blueGrey by lazy { 513 | linkedMapOf( 514 | 50 to Color(0xffECEFF1), 515 | 100 to Color(0xffCFD8DC), 516 | 200 to Color(0xffB0BEC5), 517 | 300 to Color(0xff90A4AE), 518 | 400 to Color(0xff78909C), 519 | 500 to Color(0xff607D8B), 520 | 600 to Color(0xff546E7A), 521 | 700 to Color(0xff455A64), 522 | 800 to Color(0xff37474F), 523 | 900 to Color(0xff263238) 524 | ) 525 | } 526 | } 527 | -------------------------------------------------------------------------------- /extendedcolors/src/main/java/com/smarttoolfactory/extendedcolors/md3/hct/Cam16.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.smarttoolfactory.extendedcolors.md3.hct; 18 | 19 | import static java.lang.Math.max; 20 | 21 | import com.smarttoolfactory.extendedcolors.md3.utils.ColorUtils; 22 | 23 | /** 24 | * CAM16, a color appearance model. Colors are not just defined by their hex code, but rather, a hex 25 | * code and viewing conditions. 26 | * 27 | *

CAM16 instances also have coordinates in the CAM16-UCS space, called J*, a*, b*, or jstar, 28 | * astar, bstar in code. CAM16-UCS is included in the CAM16 specification, and should be used when 29 | * measuring distances between colors. 30 | * 31 | *

In traditional color spaces, a color can be identified solely by the observer's measurement of 32 | * the color. Color appearance models such as CAM16 also use information about the environment where 33 | * the color was observed, known as the viewing conditions. 34 | * 35 | *

For example, white under the traditional assumption of a midday sun white point is accurately 36 | * measured as a slightly chromatic blue by CAM16. (roughly, hue 203, chroma 3, lightness 100) 37 | */ 38 | public final class Cam16 { 39 | // Transforms XYZ color space coordinates to 'cone'/'RGB' responses in CAM16. 40 | static final double[][] XYZ_TO_CAM16RGB = { 41 | {0.401288, 0.650173, -0.051461}, 42 | {-0.250268, 1.204414, 0.045854}, 43 | {-0.002079, 0.048952, 0.953127} 44 | }; 45 | 46 | // Transforms 'cone'/'RGB' responses in CAM16 to XYZ color space coordinates. 47 | static final double[][] CAM16RGB_TO_XYZ = { 48 | {1.8620678, -1.0112547, 0.14918678}, 49 | {0.38752654, 0.62144744, -0.00897398}, 50 | {-0.01584150, -0.03412294, 1.0499644} 51 | }; 52 | 53 | // CAM16 color dimensions, see getters for documentation. 54 | private final double hue; 55 | private final double chroma; 56 | private final double j; 57 | private final double q; 58 | private final double m; 59 | private final double s; 60 | 61 | // Coordinates in UCS space. Used to determine color distance, like delta E equations in L*a*b*. 62 | private final double jstar; 63 | private final double astar; 64 | private final double bstar; 65 | 66 | /** 67 | * CAM16 instances also have coordinates in the CAM16-UCS space, called J*, a*, b*, or jstar, 68 | * astar, bstar in code. CAM16-UCS is included in the CAM16 specification, and is used to measure 69 | * distances between colors. 70 | */ 71 | double distance(Cam16 other) { 72 | double dJ = getJstar() - other.getJstar(); 73 | double dA = getAstar() - other.getAstar(); 74 | double dB = getBstar() - other.getBstar(); 75 | double dEPrime = Math.sqrt(dJ * dJ + dA * dA + dB * dB); 76 | double dE = 1.41 * Math.pow(dEPrime, 0.63); 77 | return dE; 78 | } 79 | 80 | /** Hue in CAM16 */ 81 | public double getHue() { 82 | return hue; 83 | } 84 | 85 | /** Chroma in CAM16 */ 86 | public double getChroma() { 87 | return chroma; 88 | } 89 | 90 | /** Lightness in CAM16 */ 91 | public double getJ() { 92 | return j; 93 | } 94 | 95 | /** 96 | * Brightness in CAM16. 97 | * 98 | *

Prefer lightness, brightness is an absolute quantity. For example, a sheet of white paper is 99 | * much brighter viewed in sunlight than in indoor light, but it is the lightest object under any 100 | * lighting. 101 | */ 102 | public double getQ() { 103 | return q; 104 | } 105 | 106 | /** 107 | * Colorfulness in CAM16. 108 | * 109 | *

Prefer chroma, colorfulness is an absolute quantity. For example, a yellow toy car is much 110 | * more colorful outside than inside, but it has the same chroma in both environments. 111 | */ 112 | public double getM() { 113 | return m; 114 | } 115 | 116 | /** 117 | * Saturation in CAM16. 118 | * 119 | *

Colorfulness in proportion to brightness. Prefer chroma, saturation measures colorfulness 120 | * relative to the color's own brightness, where chroma is colorfulness relative to white. 121 | */ 122 | public double getS() { 123 | return s; 124 | } 125 | 126 | /** Lightness coordinate in CAM16-UCS */ 127 | public double getJstar() { 128 | return jstar; 129 | } 130 | 131 | /** a* coordinate in CAM16-UCS */ 132 | public double getAstar() { 133 | return astar; 134 | } 135 | 136 | /** b* coordinate in CAM16-UCS */ 137 | public double getBstar() { 138 | return bstar; 139 | } 140 | 141 | /** 142 | * All of the CAM16 dimensions can be calculated from 3 of the dimensions, in the following 143 | * combinations: - {j or q} and {c, m, or s} and hue - jstar, astar, bstar Prefer using a static 144 | * method that constructs from 3 of those dimensions. This constructor is intended for those 145 | * methods to use to return all possible dimensions. 146 | * 147 | * @param hue for example, red, orange, yellow, green, etc. 148 | * @param chroma informally, colorfulness / color intensity. like saturation in HSL, except 149 | * perceptually accurate. 150 | * @param j lightness 151 | * @param q brightness; ratio of lightness to white point's lightness 152 | * @param m colorfulness 153 | * @param s saturation; ratio of chroma to white point's chroma 154 | * @param jstar CAM16-UCS J coordinate 155 | * @param astar CAM16-UCS a coordinate 156 | * @param bstar CAM16-UCS b coordinate 157 | */ 158 | private Cam16( 159 | double hue, 160 | double chroma, 161 | double j, 162 | double q, 163 | double m, 164 | double s, 165 | double jstar, 166 | double astar, 167 | double bstar) { 168 | this.hue = hue; 169 | this.chroma = chroma; 170 | this.j = j; 171 | this.q = q; 172 | this.m = m; 173 | this.s = s; 174 | this.jstar = jstar; 175 | this.astar = astar; 176 | this.bstar = bstar; 177 | } 178 | 179 | /** 180 | * Create a CAM16 color from a color, assuming the color was viewed in default viewing conditions. 181 | * 182 | * @param argb ARGB representation of a color. 183 | */ 184 | public static Cam16 fromInt(int argb) { 185 | return fromIntInViewingConditions(argb, ViewingConditions.DEFAULT); 186 | } 187 | 188 | /** 189 | * Create a CAM16 color from a color in defined viewing conditions. 190 | * 191 | * @param argb ARGB representation of a color. 192 | * @param viewingConditions Information about the environment where the color was observed. 193 | */ 194 | // The RGB => XYZ conversion matrix elements are derived scientific constants. While the values 195 | // may differ at runtime due to floating point imprecision, keeping the values the same, and 196 | // accurate, across implementations takes precedence. 197 | @SuppressWarnings("FloatingPointLiteralPrecision") 198 | static Cam16 fromIntInViewingConditions(int argb, ViewingConditions viewingConditions) { 199 | // Transform ARGB int to XYZ 200 | int red = (argb & 0x00ff0000) >> 16; 201 | int green = (argb & 0x0000ff00) >> 8; 202 | int blue = (argb & 0x000000ff); 203 | double redL = ColorUtils.linearized(red); 204 | double greenL = ColorUtils.linearized(green); 205 | double blueL = ColorUtils.linearized(blue); 206 | double x = 0.41233895 * redL + 0.35762064 * greenL + 0.18051042 * blueL; 207 | double y = 0.2126 * redL + 0.7152 * greenL + 0.0722 * blueL; 208 | double z = 0.01932141 * redL + 0.11916382 * greenL + 0.95034478 * blueL; 209 | 210 | // Transform XYZ to 'cone'/'rgb' responses 211 | double[][] matrix = XYZ_TO_CAM16RGB; 212 | double rT = (x * matrix[0][0]) + (y * matrix[0][1]) + (z * matrix[0][2]); 213 | double gT = (x * matrix[1][0]) + (y * matrix[1][1]) + (z * matrix[1][2]); 214 | double bT = (x * matrix[2][0]) + (y * matrix[2][1]) + (z * matrix[2][2]); 215 | 216 | // Discount illuminant 217 | double rD = viewingConditions.getRgbD()[0] * rT; 218 | double gD = viewingConditions.getRgbD()[1] * gT; 219 | double bD = viewingConditions.getRgbD()[2] * bT; 220 | 221 | // Chromatic adaptation 222 | double rAF = Math.pow(viewingConditions.getFl() * Math.abs(rD) / 100.0, 0.42); 223 | double gAF = Math.pow(viewingConditions.getFl() * Math.abs(gD) / 100.0, 0.42); 224 | double bAF = Math.pow(viewingConditions.getFl() * Math.abs(bD) / 100.0, 0.42); 225 | double rA = Math.signum(rD) * 400.0 * rAF / (rAF + 27.13); 226 | double gA = Math.signum(gD) * 400.0 * gAF / (gAF + 27.13); 227 | double bA = Math.signum(bD) * 400.0 * bAF / (bAF + 27.13); 228 | 229 | // redness-greenness 230 | double a = (11.0 * rA + -12.0 * gA + bA) / 11.0; 231 | // yellowness-blueness 232 | double b = (rA + gA - 2.0 * bA) / 9.0; 233 | 234 | // auxiliary components 235 | double u = (20.0 * rA + 20.0 * gA + 21.0 * bA) / 20.0; 236 | double p2 = (40.0 * rA + 20.0 * gA + bA) / 20.0; 237 | 238 | // hue 239 | double atan2 = Math.atan2(b, a); 240 | double atanDegrees = Math.toDegrees(atan2); 241 | double hue = 242 | atanDegrees < 0 243 | ? atanDegrees + 360.0 244 | : atanDegrees >= 360 ? atanDegrees - 360.0 : atanDegrees; 245 | double hueRadians = Math.toRadians(hue); 246 | 247 | // achromatic response to color 248 | double ac = p2 * viewingConditions.getNbb(); 249 | 250 | // CAM16 lightness and brightness 251 | double j = 252 | 100.0 253 | * Math.pow( 254 | ac / viewingConditions.getAw(), 255 | viewingConditions.getC() * viewingConditions.getZ()); 256 | double q = 257 | 4.0 258 | / viewingConditions.getC() 259 | * Math.sqrt(j / 100.0) 260 | * (viewingConditions.getAw() + 4.0) 261 | * viewingConditions.getFlRoot(); 262 | 263 | // CAM16 chroma, colorfulness, and saturation. 264 | double huePrime = (hue < 20.14) ? hue + 360 : hue; 265 | double eHue = 0.25 * (Math.cos(Math.toRadians(huePrime) + 2.0) + 3.8); 266 | double p1 = 50000.0 / 13.0 * eHue * viewingConditions.getNc() * viewingConditions.getNcb(); 267 | double t = p1 * Math.hypot(a, b) / (u + 0.305); 268 | double alpha = 269 | Math.pow(1.64 - Math.pow(0.29, viewingConditions.getN()), 0.73) * Math.pow(t, 0.9); 270 | // CAM16 chroma, colorfulness, saturation 271 | double c = alpha * Math.sqrt(j / 100.0); 272 | double m = c * viewingConditions.getFlRoot(); 273 | double s = 274 | 50.0 * Math.sqrt((alpha * viewingConditions.getC()) / (viewingConditions.getAw() + 4.0)); 275 | 276 | // CAM16-UCS components 277 | double jstar = (1.0 + 100.0 * 0.007) * j / (1.0 + 0.007 * j); 278 | double mstar = 1.0 / 0.0228 * Math.log1p(0.0228 * m); 279 | double astar = mstar * Math.cos(hueRadians); 280 | double bstar = mstar * Math.sin(hueRadians); 281 | 282 | return new Cam16(hue, c, j, q, m, s, jstar, astar, bstar); 283 | } 284 | 285 | /** 286 | * @param j CAM16 lightness 287 | * @param c CAM16 chroma 288 | * @param h CAM16 hue 289 | */ 290 | static Cam16 fromJch(double j, double c, double h) { 291 | return fromJchInViewingConditions(j, c, h, ViewingConditions.DEFAULT); 292 | } 293 | 294 | /** 295 | * @param j CAM16 lightness 296 | * @param c CAM16 chroma 297 | * @param h CAM16 hue 298 | * @param viewingConditions Information about the environment where the color was observed. 299 | */ 300 | private static Cam16 fromJchInViewingConditions( 301 | double j, double c, double h, ViewingConditions viewingConditions) { 302 | double q = 303 | 4.0 304 | / viewingConditions.getC() 305 | * Math.sqrt(j / 100.0) 306 | * (viewingConditions.getAw() + 4.0) 307 | * viewingConditions.getFlRoot(); 308 | double m = c * viewingConditions.getFlRoot(); 309 | double alpha = c / Math.sqrt(j / 100.0); 310 | double s = 311 | 50.0 * Math.sqrt((alpha * viewingConditions.getC()) / (viewingConditions.getAw() + 4.0)); 312 | 313 | double hueRadians = Math.toRadians(h); 314 | double jstar = (1.0 + 100.0 * 0.007) * j / (1.0 + 0.007 * j); 315 | double mstar = 1.0 / 0.0228 * Math.log1p(0.0228 * m); 316 | double astar = mstar * Math.cos(hueRadians); 317 | double bstar = mstar * Math.sin(hueRadians); 318 | return new Cam16(h, c, j, q, m, s, jstar, astar, bstar); 319 | } 320 | 321 | /** 322 | * Create a CAM16 color from CAM16-UCS coordinates. 323 | * 324 | * @param jstar CAM16-UCS lightness. 325 | * @param astar CAM16-UCS a dimension. Like a* in L*a*b*, it is a Cartesian coordinate on the Y 326 | * axis. 327 | * @param bstar CAM16-UCS b dimension. Like a* in L*a*b*, it is a Cartesian coordinate on the X 328 | * axis. 329 | */ 330 | public static Cam16 fromUcs(double jstar, double astar, double bstar) { 331 | 332 | return fromUcsInViewingConditions(jstar, astar, bstar, ViewingConditions.DEFAULT); 333 | } 334 | 335 | /** 336 | * Create a CAM16 color from CAM16-UCS coordinates in defined viewing conditions. 337 | * 338 | * @param jstar CAM16-UCS lightness. 339 | * @param astar CAM16-UCS a dimension. Like a* in L*a*b*, it is a Cartesian coordinate on the Y 340 | * axis. 341 | * @param bstar CAM16-UCS b dimension. Like a* in L*a*b*, it is a Cartesian coordinate on the X 342 | * axis. 343 | * @param viewingConditions Information about the environment where the color was observed. 344 | */ 345 | public static Cam16 fromUcsInViewingConditions( 346 | double jstar, double astar, double bstar, ViewingConditions viewingConditions) { 347 | 348 | double m = Math.hypot(astar, bstar); 349 | double m2 = Math.expm1(m * 0.0228) / 0.0228; 350 | double c = m2 / viewingConditions.getFlRoot(); 351 | double h = Math.atan2(bstar, astar) * (180.0 / Math.PI); 352 | if (h < 0.0) { 353 | h += 360.0; 354 | } 355 | double j = jstar / (1. - (jstar - 100.) * 0.007); 356 | return fromJchInViewingConditions(j, c, h, viewingConditions); 357 | } 358 | 359 | /** 360 | * ARGB representation of the color. Assumes the color was viewed in default viewing conditions, 361 | * which are near-identical to the default viewing conditions for sRGB. 362 | */ 363 | public int toInt() { 364 | return viewed(ViewingConditions.DEFAULT); 365 | } 366 | 367 | /** 368 | * ARGB representation of the color, in defined viewing conditions. 369 | * 370 | * @param viewingConditions Information about the environment where the color will be viewed. 371 | * @return ARGB representation of color 372 | */ 373 | int viewed(ViewingConditions viewingConditions) { 374 | double alpha = 375 | (getChroma() == 0.0 || getJ() == 0.0) ? 0.0 : getChroma() / Math.sqrt(getJ() / 100.0); 376 | 377 | double t = 378 | Math.pow( 379 | alpha / Math.pow(1.64 - Math.pow(0.29, viewingConditions.getN()), 0.73), 1.0 / 0.9); 380 | double hRad = Math.toRadians(getHue()); 381 | 382 | double eHue = 0.25 * (Math.cos(hRad + 2.0) + 3.8); 383 | double ac = 384 | viewingConditions.getAw() 385 | * Math.pow(getJ() / 100.0, 1.0 / viewingConditions.getC() / viewingConditions.getZ()); 386 | double p1 = eHue * (50000.0 / 13.0) * viewingConditions.getNc() * viewingConditions.getNcb(); 387 | double p2 = (ac / viewingConditions.getNbb()); 388 | 389 | double hSin = Math.sin(hRad); 390 | double hCos = Math.cos(hRad); 391 | 392 | double gamma = 23.0 * (p2 + 0.305) * t / (23.0 * p1 + 11.0 * t * hCos + 108.0 * t * hSin); 393 | double a = gamma * hCos; 394 | double b = gamma * hSin; 395 | double rA = (460.0 * p2 + 451.0 * a + 288.0 * b) / 1403.0; 396 | double gA = (460.0 * p2 - 891.0 * a - 261.0 * b) / 1403.0; 397 | double bA = (460.0 * p2 - 220.0 * a - 6300.0 * b) / 1403.0; 398 | 399 | double rCBase = max(0, (27.13 * Math.abs(rA)) / (400.0 - Math.abs(rA))); 400 | double rC = 401 | Math.signum(rA) * (100.0 / viewingConditions.getFl()) * Math.pow(rCBase, 1.0 / 0.42); 402 | double gCBase = max(0, (27.13 * Math.abs(gA)) / (400.0 - Math.abs(gA))); 403 | double gC = 404 | Math.signum(gA) * (100.0 / viewingConditions.getFl()) * Math.pow(gCBase, 1.0 / 0.42); 405 | double bCBase = max(0, (27.13 * Math.abs(bA)) / (400.0 - Math.abs(bA))); 406 | double bC = 407 | Math.signum(bA) * (100.0 / viewingConditions.getFl()) * Math.pow(bCBase, 1.0 / 0.42); 408 | double rF = rC / viewingConditions.getRgbD()[0]; 409 | double gF = gC / viewingConditions.getRgbD()[1]; 410 | double bF = bC / viewingConditions.getRgbD()[2]; 411 | 412 | double[][] matrix = CAM16RGB_TO_XYZ; 413 | double x = (rF * matrix[0][0]) + (gF * matrix[0][1]) + (bF * matrix[0][2]); 414 | double y = (rF * matrix[1][0]) + (gF * matrix[1][1]) + (bF * matrix[1][2]); 415 | double z = (rF * matrix[2][0]) + (gF * matrix[2][1]) + (bF * matrix[2][2]); 416 | 417 | return ColorUtils.argbFromXyz(x, y, z); 418 | } 419 | } 420 | --------------------------------------------------------------------------------