Skip to content

Commit 151872d

Browse files
committedJul 16, 2020
[Jetsnack] Update to new semantic color palette.
Change-Id: I2f3459f8d13b53e13aa89c1296710ab45f847276
1 parent a6b6b9b commit 151872d

File tree

11 files changed

+444
-161
lines changed

11 files changed

+444
-161
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.example.jetsnack.ui.components
2+
3+
import androidx.compose.Composable
4+
import androidx.ui.core.Modifier
5+
import androidx.ui.foundation.Border
6+
import androidx.ui.graphics.Color
7+
import androidx.ui.graphics.Shape
8+
import androidx.ui.material.MaterialTheme
9+
import androidx.ui.unit.Dp
10+
import androidx.ui.unit.dp
11+
import com.example.jetsnack.ui.theme.JetsnackTheme
12+
13+
@Composable
14+
fun JetsnackCard(
15+
modifier: Modifier = Modifier,
16+
shape: Shape = MaterialTheme.shapes.medium,
17+
color: Color = JetsnackTheme.colors.uiBackground,
18+
contentColor: Color = JetsnackTheme.colors.textPrimary,
19+
border: Border? = null,
20+
elevation: Dp = 1.dp,
21+
content: @Composable () -> Unit
22+
) {
23+
JetsnackSurface(
24+
modifier = modifier,
25+
shape = shape,
26+
color = color,
27+
contentColor = contentColor,
28+
elevation = elevation,
29+
border = border,
30+
content = content
31+
)
32+
}

Diff for: ‎Jetsnack/app/src/main/java/com/example/jetsnack/ui/components/Gradient.kt

+16
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
package com.example.jetsnack.ui.components
1818

19+
import androidx.ui.animation.animate
1920
import androidx.ui.core.Modifier
21+
import androidx.ui.core.composed
2022
import androidx.ui.foundation.drawBackground
2123
import androidx.ui.foundation.drawBorder
2224
import androidx.ui.graphics.Color
@@ -53,3 +55,17 @@ fun Modifier.gradientBorder(
5355
endY = 59f
5456
)
5557
)
58+
59+
fun Modifier.fadeInGradientBorder(
60+
showBorder: Boolean,
61+
colors: List<Color>,
62+
shape: Shape
63+
) = composed {
64+
val animatedColors = List(colors.size) { i ->
65+
animate(if (showBorder) colors[i] else colors[i].copy(alpha = 0f))
66+
}
67+
gradientBorder(
68+
colors = animatedColors,
69+
shape = shape
70+
)
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.example.jetsnack.ui.components
2+
3+
import androidx.compose.Composable
4+
import androidx.compose.Providers
5+
import androidx.ui.core.Modifier
6+
import androidx.ui.core.clip
7+
import androidx.ui.core.drawShadow
8+
import androidx.ui.core.zIndex
9+
import androidx.ui.foundation.Border
10+
import androidx.ui.foundation.Box
11+
import androidx.ui.foundation.ContentColorAmbient
12+
import androidx.ui.foundation.drawBackground
13+
import androidx.ui.foundation.drawBorder
14+
import androidx.ui.graphics.Color
15+
import androidx.ui.graphics.RectangleShape
16+
import androidx.ui.graphics.Shape
17+
import androidx.ui.graphics.compositeOver
18+
import androidx.ui.unit.Dp
19+
import androidx.ui.unit.dp
20+
import com.example.jetsnack.ui.theme.JetsnackTheme
21+
import kotlin.math.ln
22+
23+
/**
24+
* An alternative to [androidx.ui.material.Surface] utilizing [com.example.jetsnack.ui.theme.JetsnackColorPalette]
25+
*/
26+
@Composable
27+
fun JetsnackSurface(
28+
modifier: Modifier = Modifier,
29+
shape: Shape = RectangleShape,
30+
color: Color = JetsnackTheme.colors.uiBackground,
31+
contentColor: Color = JetsnackTheme.colors.textPrimary,
32+
border: Border? = null,
33+
elevation: Dp = 0.dp,
34+
content: @Composable () -> Unit
35+
) {
36+
Box(
37+
modifier = modifier.drawShadow(elevation = elevation, shape = shape, clip = false)
38+
.zIndex(elevation.value)
39+
+ (if (border != null) Modifier.drawBorder(border, shape) else Modifier)
40+
.drawBackground(
41+
color = getBackgroundColorForElevation(color, elevation),
42+
shape = shape
43+
)
44+
.clip(shape)
45+
) {
46+
Providers(ContentColorAmbient provides contentColor, children = content)
47+
}
48+
}
49+
50+
@Composable
51+
private fun getBackgroundColorForElevation(color: Color, elevation: Dp): Color {
52+
return if (elevation > 0.dp //&& https://issuetracker.google.com/issues/161429530
53+
//JetsnackTheme.colors.isDark //&&
54+
//color == JetsnackTheme.colors.uiBackground
55+
) {
56+
color.withElevation(elevation)
57+
} else {
58+
color
59+
}
60+
}
61+
62+
/**
63+
* Applies a [Color.White] overlay to this color based on the [elevation]. This increases visibility
64+
* of elevation for surfaces in a dark theme.
65+
*/
66+
private fun Color.withElevation(elevation: Dp): Color {
67+
val foreground = calculateForeground(elevation)
68+
return foreground.compositeOver(this)
69+
}
70+
71+
// TODO: b/145802792 - clarify this algorithm
72+
/**
73+
* @return the alpha-modified [Color.White] to overlay on top of the surface color to produce
74+
* the resultant color.
75+
*/
76+
private fun calculateForeground(elevation: Dp): Color {
77+
val alpha = ((4.5f * ln(elevation.value + 1)) + 2f) / 100f
78+
return Color.White.copy(alpha = alpha)
79+
}

0 commit comments

Comments
 (0)