Skip to content

Commit

Permalink
Merge pull request #16 from Madrapps/improvement
Browse files Browse the repository at this point in the history
Improvement
  • Loading branch information
instrap authored Jun 11, 2017
2 parents 34b7028 + 17946e0 commit 19838d5
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 29 deletions.
2 changes: 1 addition & 1 deletion pikolo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apply plugin: 'com.github.dcendents.android-maven'
apply plugin: "com.jfrog.bintray"


def projectVersion = "1.1.0"
def projectVersion = "1.1.4"
def projectGroupId = "com.github.madrapps"
def siteUrl = 'https://github.com/Madrapps/Pikolo'
def gitUrl = 'https://github.com/Madrapps/Pikolo.git'
Expand Down
62 changes: 57 additions & 5 deletions pikolo/src/main/java/com/madrapps/pikolo/HSLColorPicker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.madrapps.pikolo

import android.content.Context
import android.graphics.Canvas
import android.os.Parcel
import android.os.Parcelable
import android.support.v4.graphics.ColorUtils
import android.util.AttributeSet
import android.util.TypedValue
Expand Down Expand Up @@ -45,16 +47,25 @@ class HSLColorPicker @JvmOverloads constructor(context: Context, attrs: Attribut

hueComponent = HueComponent(metrics, paints, hueArcLength, hueStartAngle)

hueComponent.strokeWidth = typedArray.getDimension(R.styleable.HSLColorPicker_hue_stroke_width, dp(5f))
hueComponent.strokeWidth = typedArray.getDimension(R.styleable.HSLColorPicker_hue_arc_width, dp(5f))
hueComponent.borderWidth = typedArray.getDimension(R.styleable.HSLColorPicker_hue_stroke_width, 0f)
hueComponent.indicatorStrokeWidth = typedArray.getDimension(R.styleable.HSLColorPicker_hue_indicator_stroke_width, dp(2f))
hueComponent.indicatorStrokeColor = typedArray.getColor(R.styleable.HSLColorPicker_hue_indicator_stroke_color, 0)
hueComponent.strokeColor = typedArray.getColor(R.styleable.HSLColorPicker_hue_stroke_color, 0)
hueComponent.indicatorRadius = typedArray.getDimension(R.styleable.HSLColorPicker_hue_indicator_radius, dp(15f))

saturationComponent.strokeWidth = typedArray.getDimension(R.styleable.HSLColorPicker_saturation_stroke_width, dp(5f))
saturationComponent.strokeWidth = typedArray.getDimension(R.styleable.HSLColorPicker_saturation_arc_width, dp(5f))
saturationComponent.borderWidth = typedArray.getDimension(R.styleable.HSLColorPicker_saturation_stroke_width, 0f)
saturationComponent.indicatorStrokeWidth = typedArray.getDimension(R.styleable.HSLColorPicker_saturation_indicator_stroke_width, dp(2f))
saturationComponent.indicatorStrokeColor = typedArray.getColor(R.styleable.HSLColorPicker_saturation_indicator_stroke_color, 0)
saturationComponent.strokeColor = typedArray.getColor(R.styleable.HSLColorPicker_saturation_stroke_color, 0)
saturationComponent.indicatorRadius = typedArray.getDimension(R.styleable.HSLColorPicker_saturation_indicator_radius, dp(15f))

lightnessComponent.strokeWidth = typedArray.getDimension(R.styleable.HSLColorPicker_lightness_stroke_width, dp(5f))
lightnessComponent.strokeWidth = typedArray.getDimension(R.styleable.HSLColorPicker_lightness_arc_width, dp(5f))
lightnessComponent.borderWidth = typedArray.getDimension(R.styleable.HSLColorPicker_lightness_stroke_width, 0f)
lightnessComponent.indicatorStrokeWidth = typedArray.getDimension(R.styleable.HSLColorPicker_lightness_indicator_stroke_width, dp(2f))
lightnessComponent.indicatorStrokeColor = typedArray.getColor(R.styleable.HSLColorPicker_lightness_indicator_stroke_color, 0)
lightnessComponent.strokeColor = typedArray.getColor(R.styleable.HSLColorPicker_lightness_stroke_color, 0)
lightnessComponent.indicatorRadius = typedArray.getDimension(R.styleable.HSLColorPicker_lightness_indicator_radius, dp(15f))

hueRadiusOffset = typedArray.getDimension(R.styleable.HSLColorPicker_hue_radius_offset, dp(1f))
Expand Down Expand Up @@ -88,13 +99,14 @@ class HSLColorPicker @JvmOverloads constructor(context: Context, attrs: Attribut
}

override fun onTouchEvent(event: MotionEvent): Boolean {
var isTouched = true
if (!hueComponent.onTouchEvent(event)) {
if (!saturationComponent.onTouchEvent(event)) {
lightnessComponent.onTouchEvent(event)
isTouched = lightnessComponent.onTouchEvent(event)
}
}
invalidate()
return true
return isTouched
}

fun setColorSelectionListener(listener: OnColorSelectionListener) {
Expand All @@ -117,4 +129,44 @@ class HSLColorPicker @JvmOverloads constructor(context: Context, attrs: Attribut
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, resources.displayMetrics)
}


override fun onSaveInstanceState(): Parcelable {
val bundle = super.onSaveInstanceState()
val savedState = SavedState(bundle)
savedState.color = ColorUtils.HSLToColor(metrics.hsl)
return savedState
}

override fun onRestoreInstanceState(state: Parcelable?) {
if (state is SavedState) {
super.onRestoreInstanceState(state.superState)
setColor(state.color)
} else {
super.onRestoreInstanceState(state)
}
}

internal class SavedState : BaseSavedState {
var color: Int = 0

constructor(bundle: Parcelable) : super(bundle)

private constructor(parcel: Parcel) : super(parcel) {
color = parcel.readInt()
}

companion object {
@JvmField val CREATOR: Parcelable.Creator<SavedState> = object : Parcelable.Creator<SavedState> {
override fun createFromParcel(source: Parcel): SavedState = SavedState(source)
override fun newArray(size: Int): Array<SavedState?> = arrayOfNulls(size)
}
}

override fun describeContents() = 0

override fun writeToParcel(dest: Parcel, flags: Int) {
super.writeToParcel(dest, flags)
dest.writeInt(color)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal abstract class ArcComponent(metrics: Metrics, paints: Paints) : ColorCo
private val matrix = Matrix()
private lateinit var shader: Shader
private var innerCircleArcReference: RectF? = null
private val borderColor = floatArrayOf(0f, 0.8f, 1f)

abstract val hslIndex: Int
abstract val arcLength: Float
Expand All @@ -36,31 +37,65 @@ internal abstract class ArcComponent(metrics: Metrics, paints: Paints) : ColorCo

internal open fun drawArc(canvas: Canvas) {
val shaderPaint = paints.shaderPaint
shaderPaint.shader = getShader()
shaderPaint.style = Paint.Style.STROKE
shaderPaint.strokeWidth = strokeWidth
shaderPaint.strokeCap = Paint.Cap.ROUND

if (innerCircleArcReference == null) {
innerCircleArcReference = RectF(metrics.centerX - radius, metrics.centerY - radius, metrics.centerX + radius, metrics.centerY + radius)
}
if (borderWidth > 0) {
shaderPaint.shader = null
shaderPaint.color = if (strokeColor == 0) Color.WHITE else strokeColor
shaderPaint.strokeWidth = strokeWidth + borderWidth*2
canvas.drawArc(innerCircleArcReference, arcStartAngle, arcLength, false, shaderPaint)
}

shaderPaint.strokeWidth = strokeWidth
shaderPaint.shader = getShader()
canvas.drawArc(innerCircleArcReference, arcStartAngle, arcLength, false, shaderPaint)
}


internal open fun drawIndicator(canvas: Canvas) {
indicatorX = (metrics.centerX + radius * Math.cos(Math.toRadians(angle))).toFloat()
indicatorY = (metrics.centerY + radius * Math.sin(Math.toRadians(angle))).toFloat()

val indicatorPaint = paints.indicatorPaint
indicatorPaint.style = Paint.Style.FILL

indicatorPaint.color = ColorUtils.HSLToColor(metrics.hsl)
val color = ColorUtils.HSLToColor(metrics.hsl)
indicatorPaint.color = color
canvas.drawCircle(indicatorX, indicatorY, indicatorRadius, indicatorPaint)

indicatorPaint.style = Paint.Style.STROKE
indicatorPaint.strokeWidth = indicatorStrokeWidth
indicatorPaint.color = Color.WHITE
canvas.drawCircle(indicatorX, indicatorY, indicatorRadius, indicatorPaint)
if (indicatorStrokeWidth > 0) {
indicatorPaint.color = getBorderColor(color)
indicatorPaint.style = Paint.Style.STROKE
indicatorPaint.strokeWidth = indicatorStrokeWidth
canvas.drawCircle(indicatorX, indicatorY, indicatorRadius, indicatorPaint)
}
}

private fun getBorderColor(color: Int): Int {
if (indicatorStrokeColor != 0) {
return indicatorStrokeColor
}
borderColor[0] = metrics.hsl[0]
val contrastW = ColorUtils.calculateContrast(color, Color.WHITE)
val contrastB = ColorUtils.calculateContrast(color, Color.BLACK)
when {
contrastB - contrastW > 16 -> borderColor[2] = 0f
contrastB - contrastW > 10 -> borderColor[2] = 0.1f
contrastB - contrastW > 6 -> borderColor[2] = 0.2f
contrastB - contrastW > 4 -> borderColor[2] = 0.3f
contrastB - contrastW > 2 -> borderColor[2] = 0.4f
contrastB - contrastW > 0 -> borderColor[2] = 0.5f
contrastB - contrastW > -2 -> borderColor[2] = 0.6f
contrastB - contrastW > -4 -> borderColor[2] = 0.7f
contrastB - contrastW > -8 -> borderColor[2] = 0.8f
contrastB - contrastW > -12 -> borderColor[2] = 0.9f
else -> borderColor[2] = 1f
}
return ColorUtils.HSLToColor(borderColor)
}

override fun getShader(): Shader {
Expand Down Expand Up @@ -154,7 +189,7 @@ internal abstract class ArcComponent(metrics: Metrics, paints: Paints) : ColorCo
}

override fun updateAngle(component: Float) {
val baseAngle = component/range * arcLength
val baseAngle = component / range * arcLength
val relativeAngle = baseAngle + arcStartAngle

angle = relativeAngle.toDouble()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ internal abstract class ColorComponent(val metrics: Metrics, val paints: Paints)

var radius: Float = 0f
var strokeWidth: Float = 0f
var strokeColor: Int = 0
var borderWidth: Float = 0f
var indicatorRadius: Float = 0f
var indicatorStrokeWidth: Float = 0f
var indicatorStrokeColor: Int = 0
var indicatorX: Float = 0f
var indicatorY: Float = 0f

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,4 @@ internal class HueComponent(metrics: Metrics, paints: Paints, override val arcLe
return colors
}

override fun drawIndicator(canvas: Canvas) {
indicatorX = (metrics.centerX + radius * Math.cos(Math.toRadians(angle))).toFloat()
indicatorY = (metrics.centerY + radius * Math.sin(Math.toRadians(angle))).toFloat()

val indicatorPaint = paints.indicatorPaint
indicatorPaint.style = Paint.Style.FILL

indicatorPaint.color = ColorUtils.HSLToColor(floatArrayOf(metrics.hsl[0], 1f, 0.5f))
canvas.drawCircle(indicatorX, indicatorY, indicatorRadius, indicatorPaint)

indicatorPaint.style = Paint.Style.STROKE
indicatorPaint.strokeWidth = indicatorStrokeWidth
indicatorPaint.color = Color.WHITE
canvas.drawCircle(indicatorX, indicatorY, indicatorRadius, indicatorPaint)
}
}
12 changes: 12 additions & 0 deletions pikolo/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
<resources>

<declare-styleable name="HSLColorPicker">
<attr name="hue_arc_width" format="dimension" />
<attr name="saturation_arc_width" format="dimension" />
<attr name="lightness_arc_width" format="dimension" />

<attr name="hue_stroke_width" format="dimension" />
<attr name="saturation_stroke_width" format="dimension" />
<attr name="lightness_stroke_width" format="dimension" />
Expand All @@ -14,6 +18,14 @@
<attr name="saturation_indicator_stroke_width" format="dimension" />
<attr name="lightness_indicator_stroke_width" format="dimension" />

<attr name="hue_indicator_stroke_color" format="color" />
<attr name="saturation_indicator_stroke_color" format="color" />
<attr name="lightness_indicator_stroke_color" format="color" />

<attr name="hue_stroke_color" format="color" />
<attr name="saturation_stroke_color" format="color" />
<attr name="lightness_stroke_color" format="color" />

<attr name="hue_start_angle" format="float" />
<attr name="hue_arc_length" format="float" />

Expand Down
4 changes: 4 additions & 0 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@
pikolo:lightness_start_angle="270"
pikolo:saturation_arc_length="110"
pikolo:saturation_radius_offset="20dp"
app:hue_stroke_width="0dp"
pikolo:saturation_start_angle="150"
app:lightness_indicator_stroke_width="0dp"
app:saturation_indicator_stroke_color="@color/colorPrimary"
app:hue_stroke_color="#2bff00"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
Expand Down

0 comments on commit 19838d5

Please sign in to comment.