Skip to content

Commit

Permalink
Library: Use different shadow colors as theme
Browse files Browse the repository at this point in the history
  • Loading branch information
fornewid committed May 9, 2020
1 parent 9e9a080 commit 39a2734
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 59 deletions.
14 changes: 9 additions & 5 deletions neumorphism/src/main/java/soup/neumorphism/NeumorphButton.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package soup.neumorphism

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.util.Log
import androidx.annotation.ColorInt
import androidx.appcompat.widget.AppCompatButton
import soup.neumorphism.internal.util.NeumorphResources

class NeumorphButton @JvmOverloads constructor(
context: Context,
Expand All @@ -29,11 +29,15 @@ class NeumorphButton @JvmOverloads constructor(
val shadowElevation = a.getDimension(
R.styleable.NeumorphButton_neumorph_shadowElevation, 0f
)
val shadowColorLight = a.getColor(
R.styleable.NeumorphButton_neumorph_shadowColorLight, Color.WHITE
val shadowColorLight = NeumorphResources.getColor(
context, a,
R.styleable.NeumorphButton_neumorph_shadowColorLight,
R.color.design_default_color_shadow_light
)
val shadowColorDark = a.getColor(
R.styleable.NeumorphButton_neumorph_shadowColorDark, Color.BLACK
val shadowColorDark = NeumorphResources.getColor(
context, a,
R.styleable.NeumorphButton_neumorph_shadowColorDark,
R.color.design_default_color_shadow_dark
)
a.recycle()

Expand Down
14 changes: 9 additions & 5 deletions neumorphism/src/main/java/soup/neumorphism/NeumorphCardView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package soup.neumorphism

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.util.Log
import android.widget.FrameLayout
import androidx.annotation.ColorInt
import soup.neumorphism.internal.util.NeumorphResources

class NeumorphCardView @JvmOverloads constructor(
context: Context,
Expand All @@ -29,11 +29,15 @@ class NeumorphCardView @JvmOverloads constructor(
val shadowElevation = a.getDimension(
R.styleable.NeumorphCardView_neumorph_shadowElevation, 0f
)
val shadowColorLight = a.getColor(
R.styleable.NeumorphCardView_neumorph_shadowColorLight, Color.WHITE
val shadowColorLight = NeumorphResources.getColor(
context, a,
R.styleable.NeumorphCardView_neumorph_shadowColorLight,
R.color.design_default_color_shadow_light
)
val shadowColorDark = a.getColor(
R.styleable.NeumorphCardView_neumorph_shadowColorDark, Color.BLACK
val shadowColorDark = NeumorphResources.getColor(
context, a,
R.styleable.NeumorphCardView_neumorph_shadowColorDark,
R.color.design_default_color_shadow_dark
)
a.recycle()

Expand Down
14 changes: 9 additions & 5 deletions neumorphism/src/main/java/soup/neumorphism/NeumorphEditText.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package soup.neumorphism
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Canvas
import android.graphics.Color
import android.util.AttributeSet
import androidx.annotation.ColorInt
import androidx.appcompat.widget.AppCompatEditText
import soup.neumorphism.internal.blur.BlurProvider
import soup.neumorphism.internal.util.NeumorphResources
import soup.neumorphism.internal.util.withClip
import soup.neumorphism.internal.util.withTranslation

Expand Down Expand Up @@ -37,11 +37,15 @@ class NeumorphEditText @JvmOverloads constructor(
val shadowElevation = a.getDimension(
R.styleable.NeumorphEditText_neumorph_shadowElevation, 0f
)
val shadowColorLight = a.getColor(
R.styleable.NeumorphEditText_neumorph_shadowColorLight, Color.WHITE
val shadowColorLight = NeumorphResources.getColor(
context, a,
R.styleable.NeumorphEditText_neumorph_shadowColorLight,
R.color.design_default_color_shadow_light
)
val shadowColorDark = a.getColor(
R.styleable.NeumorphEditText_neumorph_shadowColorDark, Color.BLACK
val shadowColorDark = NeumorphResources.getColor(
context, a,
R.styleable.NeumorphEditText_neumorph_shadowColorDark,
R.color.design_default_color_shadow_dark
)
a.recycle()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package soup.neumorphism

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.util.Log
import androidx.annotation.ColorInt
import androidx.appcompat.widget.AppCompatImageButton
import soup.neumorphism.internal.util.NeumorphResources

class NeumorphFloatingActionButton @JvmOverloads constructor(
context: Context,
Expand All @@ -30,11 +30,15 @@ class NeumorphFloatingActionButton @JvmOverloads constructor(
val shadowElevation = a.getDimension(
R.styleable.NeumorphFloatingActionButton_neumorph_shadowElevation, 0f
)
val shadowColorLight = a.getColor(
R.styleable.NeumorphFloatingActionButton_neumorph_shadowColorLight, Color.WHITE
val shadowColorLight = NeumorphResources.getColor(
context, a,
R.styleable.NeumorphFloatingActionButton_neumorph_shadowColorLight,
R.color.design_default_color_shadow_light
)
val shadowColorDark = a.getColor(
R.styleable.NeumorphFloatingActionButton_neumorph_shadowColorDark, Color.BLACK
val shadowColorDark = NeumorphResources.getColor(
context, a,
R.styleable.NeumorphFloatingActionButton_neumorph_shadowColorDark,
R.color.design_default_color_shadow_dark
)
a.recycle()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package soup.neumorphism

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.util.Log
import androidx.annotation.ColorInt
import androidx.appcompat.widget.AppCompatImageButton
import androidx.appcompat.widget.AppCompatImageView
import soup.neumorphism.internal.util.NeumorphResources

class NeumorphImageButton @JvmOverloads constructor(
context: Context,
Expand All @@ -30,11 +29,15 @@ class NeumorphImageButton @JvmOverloads constructor(
val shadowElevation = a.getDimension(
R.styleable.NeumorphImageButton_neumorph_shadowElevation, 0f
)
val shadowColorLight = a.getColor(
R.styleable.NeumorphImageButton_neumorph_shadowColorLight, Color.WHITE
val shadowColorLight = NeumorphResources.getColor(
context, a,
R.styleable.NeumorphImageButton_neumorph_shadowColorLight,
R.color.design_default_color_shadow_light
)
val shadowColorDark = a.getColor(
R.styleable.NeumorphImageButton_neumorph_shadowColorDark, Color.BLACK
val shadowColorDark = NeumorphResources.getColor(
context, a,
R.styleable.NeumorphImageButton_neumorph_shadowColorDark,
R.color.design_default_color_shadow_dark
)
a.recycle()

Expand Down
14 changes: 9 additions & 5 deletions neumorphism/src/main/java/soup/neumorphism/NeumorphImageView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package soup.neumorphism

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.util.Log
import androidx.annotation.ColorInt
import androidx.appcompat.widget.AppCompatImageView
import soup.neumorphism.internal.util.NeumorphResources

class NeumorphImageView @JvmOverloads constructor(
context: Context,
Expand All @@ -29,11 +29,15 @@ class NeumorphImageView @JvmOverloads constructor(
val shadowElevation = a.getDimension(
R.styleable.NeumorphImageView_neumorph_shadowElevation, 0f
)
val shadowColorLight = a.getColor(
R.styleable.NeumorphImageView_neumorph_shadowColorLight, Color.WHITE
val shadowColorLight = NeumorphResources.getColor(
context, a,
R.styleable.NeumorphImageView_neumorph_shadowColorLight,
R.color.design_default_color_shadow_light
)
val shadowColorDark = a.getColor(
R.styleable.NeumorphImageView_neumorph_shadowColorDark, Color.BLACK
val shadowColorDark = NeumorphResources.getColor(
context, a,
R.styleable.NeumorphImageView_neumorph_shadowColorDark,
R.color.design_default_color_shadow_dark
)
a.recycle()

Expand Down
19 changes: 12 additions & 7 deletions neumorphism/src/main/java/soup/neumorphism/NeumorphTextView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.text.StaticLayout
import android.text.TextPaint
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
import soup.neumorphism.internal.util.NeumorphResources
import kotlin.math.max

class NeumorphTextView @JvmOverloads constructor(
Expand All @@ -30,14 +31,18 @@ class NeumorphTextView @JvmOverloads constructor(
val a = context.obtainStyledAttributes(
attrs, R.styleable.NeumorphTextView, defStyleAttr, defStyleRes
)
shadowElevation = a.getDimensionPixelSize(
R.styleable.NeumorphTextView_neumorph_shadowElevation, 0
).toFloat()
shadowColorLight = a.getColor(
R.styleable.NeumorphTextView_neumorph_shadowColorLight, Color.WHITE
shadowElevation = a.getDimension(
R.styleable.NeumorphTextView_neumorph_shadowElevation, 0f
)
shadowColorDark = a.getColor(
R.styleable.NeumorphTextView_neumorph_shadowColorDark, Color.BLACK
shadowColorLight = NeumorphResources.getColor(
context, a,
R.styleable.NeumorphTextView_neumorph_shadowColorLight,
R.color.design_default_color_shadow_light
)
shadowColorDark = NeumorphResources.getColor(
context, a,
R.styleable.NeumorphTextView_neumorph_shadowColorDark,
R.color.design_default_color_shadow_dark
)
a.recycle()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package soup.neumorphism.internal.util

import android.content.Context
import android.content.res.TypedArray
import androidx.annotation.ColorRes
import androidx.annotation.StyleableRes
import androidx.core.content.ContextCompat

internal object NeumorphResources {

fun getColor(
context: Context,
attributes: TypedArray,
@StyleableRes index: Int,
@ColorRes defaultColor: Int
): Int {
return try {
if (attributes.hasValue(index)) {
val resourceId = attributes.getResourceId(index, 0)
if (resourceId != 0) {
return ContextCompat.getColor(context, resourceId)
}
}
attributes.getColor(index, ContextCompat.getColor(context, defaultColor))
} catch (e: Exception) {
ContextCompat.getColor(context, defaultColor)
}
}
}
5 changes: 0 additions & 5 deletions neumorphism/src/main/res/values-night/values.xml

This file was deleted.

3 changes: 3 additions & 0 deletions neumorphism/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<attr name="colorShadowLight" format="color"/>
<attr name="colorShadowDark" format="color"/>

<attr name="neumorph_backgroundColor" format="color" />
<attr name="neumorph_strokeColor" format="color" />
<attr name="neumorph_strokeWidth" format="dimension" />
Expand Down
28 changes: 14 additions & 14 deletions neumorphism/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
<item name="android:clickable">true</item>
<item name="android:gravity">center_vertical|center_horizontal</item>
<item name="neumorph_shadowElevation">6dp</item>
<item name="neumorph_shadowColorLight">@color/default_shadow_light</item>
<item name="neumorph_shadowColorDark">@color/default_shadow_dark</item>
<item name="neumorph_shadowColorLight">?colorShadowLight</item>
<item name="neumorph_shadowColorDark">?colorShadowDark</item>
<item name="neumorph_shapeType">flat</item>
<item name="neumorph_shapeAppearance">@style/ShapeAppearance.Neumorph.Button</item>
</style>

<style name="Widget.Neumorph.CardView" parent="android:Widget">
<item name="neumorph_shadowElevation">6dp</item>
<item name="neumorph_shadowColorLight">@color/default_shadow_light</item>
<item name="neumorph_shadowColorDark">@color/default_shadow_dark</item>
<item name="neumorph_shadowColorLight">?colorShadowLight</item>
<item name="neumorph_shadowColorDark">?colorShadowDark</item>
<item name="neumorph_shapeType">flat</item>
<item name="neumorph_shapeAppearance">@style/ShapeAppearance.Neumorph.CardView</item>
</style>
Expand All @@ -33,16 +33,16 @@
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
<item name="neumorph_shadowElevation">6dp</item>
<item name="neumorph_shadowColorLight">@color/default_shadow_light</item>
<item name="neumorph_shadowColorDark">@color/default_shadow_dark</item>
<item name="neumorph_shadowColorLight">?colorShadowLight</item>
<item name="neumorph_shadowColorDark">?colorShadowDark</item>
<item name="neumorph_shapeType">flat</item>
<item name="neumorph_shapeAppearance">@style/ShapeAppearance.Neumorph.FAB</item>
</style>

<style name="Widget.Neumorph.EditText" parent="Widget.AppCompat.EditText">
<item name="neumorph_shadowElevation">6dp</item>
<item name="neumorph_shadowColorLight">@color/default_shadow_light</item>
<item name="neumorph_shadowColorDark">@color/default_shadow_dark</item>
<item name="neumorph_shadowColorLight">?colorShadowLight</item>
<item name="neumorph_shadowColorDark">?colorShadowDark</item>
<item name="neumorph_shapeType">flat</item>
<item name="neumorph_shapeAppearance">@style/ShapeAppearance.Neumorph.EditText</item>
<item name="android:paddingStart">16dp</item>
Expand All @@ -52,8 +52,8 @@

<style name="Widget.Neumorph.ImageView" parent="android:Widget">
<item name="neumorph_shadowElevation">6dp</item>
<item name="neumorph_shadowColorLight">@color/default_shadow_light</item>
<item name="neumorph_shadowColorDark">@color/default_shadow_dark</item>
<item name="neumorph_shadowColorLight">?colorShadowLight</item>
<item name="neumorph_shadowColorDark">?colorShadowDark</item>
<item name="neumorph_shapeType">flat</item>
<item name="neumorph_shapeAppearance">@style/ShapeAppearance.Neumorph.ImageView</item>
</style>
Expand All @@ -64,16 +64,16 @@
<item name="android:clickable">true</item>
<item name="android:scaleType">center</item>
<item name="neumorph_shadowElevation">6dp</item>
<item name="neumorph_shadowColorLight">@color/default_shadow_light</item>
<item name="neumorph_shadowColorDark">@color/default_shadow_dark</item>
<item name="neumorph_shadowColorLight">?colorShadowLight</item>
<item name="neumorph_shadowColorDark">?colorShadowDark</item>
<item name="neumorph_shapeType">flat</item>
<item name="neumorph_shapeAppearance">@style/ShapeAppearance.Neumorph.ImageView</item>
</style>

<style name="Widget.Neumorph.TextView" parent="android:Widget">
<item name="neumorph_shadowElevation">2dp</item>
<item name="neumorph_shadowColorLight">@color/default_shadow_light</item>
<item name="neumorph_shadowColorDark">@color/default_shadow_dark</item>
<item name="neumorph_shadowColorLight">?colorShadowLight</item>
<item name="neumorph_shadowColorDark">?colorShadowDark</item>
</style>

<style name="ShapeAppearance" />
Expand Down
9 changes: 9 additions & 0 deletions neumorphism/src/main/res/values/themes_base_bridge.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
<style name="Platform.Neumorph.Light" parent="Theme.AppCompat.Light" />

<style name="Base.V21.Theme.Neumorph.Bridge" parent="Platform.Neumorph">
<item name="colorShadowLight">@color/design_dark_default_color_shadow_light</item>
<item name="colorShadowDark">@color/design_dark_default_color_shadow_dark</item>

<item name="neumorphButtonStyle">@style/Widget.Neumorph.Button</item>
<item name="neumorphCardViewStyle">@style/Widget.Neumorph.CardView</item>
<item name="neumorphEditTextStyle">@style/Widget.Neumorph.EditText</item>
Expand All @@ -16,6 +19,9 @@
</style>

<style name="Base.V21.Theme.Neumorph.Light.Bridge" parent="Platform.Neumorph.Light">
<item name="colorShadowLight">@color/design_default_color_shadow_light</item>
<item name="colorShadowDark">@color/design_default_color_shadow_dark</item>

<item name="neumorphButtonStyle">@style/Widget.Neumorph.Button</item>
<item name="neumorphCardViewStyle">@style/Widget.Neumorph.CardView</item>
<item name="neumorphEditTextStyle">@style/Widget.Neumorph.EditText</item>
Expand All @@ -26,6 +32,9 @@
</style>

<style name="Base.V21.Theme.Neumorph.Light.DarkActionBar.Bridge" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorShadowLight">@color/design_default_color_shadow_light</item>
<item name="colorShadowDark">@color/design_default_color_shadow_dark</item>

<item name="neumorphButtonStyle">@style/Widget.Neumorph.Button</item>
<item name="neumorphCardViewStyle">@style/Widget.Neumorph.CardView</item>
<item name="neumorphEditTextStyle">@style/Widget.Neumorph.EditText</item>
Expand Down
Loading

0 comments on commit 39a2734

Please sign in to comment.