Skip to content

Pallete api #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ androidx-dynamicanimation = "androidx.dynamicanimation:dynamicanimation-ktx:1.0.
androidx-media3-common = { module = "androidx.media3:media3-common", version.ref = "media3" }
androidx-media3-exoplayer = { module = "androidx.media3:media3-exoplayer", version.ref = "media3" }
androidx-media3-ui = { module = "androidx.media3:media3-ui", version.ref = "media3" }
androidx-palette = { module = "androidx.palette:palette", version = "1.0.0" }

glide = "com.github.bumptech.glide:glide:4.15.1"

Expand Down
6 changes: 4 additions & 2 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ Demonstrates how to implement data access auditing for your app to identify
This sample demonstrates displaying an UltraHDR image.
- [Displaying UltraHDR (Glide)](graphics/ultrahdr/src/main/java/com/example/platform/graphics/ultrahdr/display/DisplayingUltraHDRUsingGlide.kt):
This sample demonstrates using the Glide image loading library to detect the
- [Editing UltraHDR](graphics/ultrahdr/src/main/java/com/example/platform/graphics/ultrahdr/edit/EditingUltraHDR.kt):
This sample demonstrates editing an UltraHDR image. Spatial edit operations like crop, rotate, scale are supported.
- [Downloadable Fonts](user-interface/text/src/main/java/com/example/platform/ui/text/DownloadableFonts.kt):
Download fonts instead of bundling them in the app resources.
- [Drag and Drop](user-interface/draganddrop/src/main/java/com/example/platform/ui/draganddrop/DragAndDrop.kt):
Demonstrates basic Drag and Drop functionality.
- [Editing UltraHDR](graphics/ultrahdr/src/main/java/com/example/platform/graphics/ultrahdr/edit/EditingUltraHDR.kt):
This sample demonstrates editing an UltraHDR image and the resulting gainmap as well. Spatial edit operations like crop, rotate, scale are supported
- [Find devices sample](connectivity/bluetooth/ble/src/main/java/com/example/platform/connectivity/bluetooth/ble/FindBLEDevicesSample.kt):
This example will demonstrate how to scanning for Low Energy Devices
- [Haptics - 1. Vibration effects](user-interface/haptics/src/main/java/com/example/platform/ui/haptics/Haptics.kt):
Expand Down Expand Up @@ -68,6 +68,8 @@ Basic motion example using referenced ConstraintLayout files
Shows the recommended flow to request multiple RELATED runtime permissions
- [Package Visibility](privacy/data/src/main/java/com/example/platform/privacy/data/PackageVisibility.kt):
A sample that showcase how the package visibility queries affects the available packages
- [Palette API](graphics/pallete/src/main/java/com/example/platform/graphics/pallete/PalleteSampleScreen.kt):
This sample demonstrates using an Palette API.
- [PdfRenderer](graphics/pdf/src/main/java/com/example/platform/graphics/pdf/PdfRenderer.kt):
Demonstrates how to use PdfRenderer to display PDF documents on the screen.
- [Permissionless](privacy/permissions/src/main/java/com/example/platform/privacy/permissions/Permissionless.kt):
Expand Down
1 change: 1 addition & 0 deletions samples/graphics/pallete/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
27 changes: 27 additions & 0 deletions samples/graphics/pallete/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

plugins {
id("com.example.platform.sample")
}

android {
namespace = "com.example.platform.pallete"
}

dependencies {
implementation(libs.androidx.palette)
}
Empty file.
21 changes: 21 additions & 0 deletions samples/graphics/pallete/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
20 changes: 20 additions & 0 deletions samples/graphics/pallete/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2023 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ https://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android">

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.platform.graphics.pallete

import android.graphics.BitmapFactory
import androidx.annotation.DrawableRes
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.palette.graphics.Palette
import com.example.platform.pallete.R
import com.google.android.catalog.framework.annotations.Sample

@Sample(
name = "Palette API",
description = "This sample demonstrates using an Palette API.",
documentation = "https://developer.android.com/develop/ui/views/graphics/palette-colors#kts",
tags = ["Graphics"],
)
@Composable
fun PaletteSampleScreen() {
val scrollState = rememberScrollState()

Column(
modifier = Modifier
.fillMaxSize()
.verticalScroll(scrollState),
) {
PaletteViewer(imageId = R.drawable.img_carles_rabada_unsplash)
PaletteViewer(imageId = R.drawable.img_adam_birkett_unsplash)
}
}

@Composable
fun PaletteViewer(
@DrawableRes imageId: Int,
) {
var profileState: Palette? by remember { mutableStateOf(null) }

BitmapFactory.decodeResource(
LocalContext.current.resources,
imageId,
).let { bitmap ->
Palette.from(bitmap).generate { palette ->
profileState = palette
}
}

Column {
profileState?.let { palette ->
Text(
text = "This text and background color is from vibrant swatch.",
style = MaterialTheme.typography.titleMedium.copy(
color = Color(palette.vibrantSwatch?.titleTextColor ?: 0),
),
modifier = Modifier
.clickable {
}
.fillMaxWidth()
.background(
color = Color(palette.vibrantSwatch?.rgb ?: 0),
),
)
Row {
Image(
painter = painterResource(id = imageId),
contentDescription = null,
modifier = Modifier.fillMaxWidth(0.5f),
)
Column {
palette.lightVibrantSwatch?.let { swatch ->
Text(
text = "Light Vibrant",
style = MaterialTheme.typography.titleMedium.copy(
color = Color(swatch.titleTextColor),
),
modifier = Modifier
.fillMaxWidth()
.background(
color = Color(swatch.rgb),
)
.padding(5.dp),
)
}

palette.vibrantSwatch?.let { swatch ->
Text(
text = "Vibrant",
style = MaterialTheme.typography.titleMedium.copy(
color = Color(swatch.titleTextColor),
),
modifier = Modifier
.fillMaxWidth()
.background(
color = Color(swatch.rgb),
)
.padding(5.dp),
)
}

palette.darkVibrantSwatch?.let { swatch ->
Text(
text = "Dark Vibrant",
style = MaterialTheme.typography.titleMedium.copy(
color = Color(swatch.titleTextColor),
),
modifier = Modifier
.fillMaxWidth()
.background(
color = Color(swatch.rgb),
)
.padding(5.dp),
)
}

palette.lightMutedSwatch?.let { swatch ->
Text(
text = "Light Muted",
style = MaterialTheme.typography.titleMedium.copy(
color = Color(swatch.titleTextColor),
),
modifier = Modifier
.fillMaxWidth()
.background(
color = Color(swatch.rgb),
)
.padding(5.dp),
)
}

palette.mutedSwatch?.let { swatch ->
Text(
text = "Muted",
style = MaterialTheme.typography.titleMedium.copy(
color = Color(swatch.titleTextColor),
),
modifier = Modifier
.fillMaxWidth()
.background(
color = Color(swatch.rgb),
)
.padding(5.dp),
)
}

palette.darkMutedSwatch?.let { swatch ->
Text(
text = "Dark Muted",
style = MaterialTheme.typography.titleMedium.copy(
color = Color(swatch.titleTextColor),
),
modifier = Modifier
.fillMaxWidth()
.background(
color = Color(swatch.rgb),
)
.padding(5.dp),
)
}
}
}
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.