Skip to content

Commit 355b4e0

Browse files
authored
Merge pull request #1 from nitesh-dev/master
Readme updated
2 parents f47e4c4 + 7ffece9 commit 355b4e0

14 files changed

+339
-239
lines changed

.idea/gradle.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ColorPicker/src/main/java/com/abhishek/colorpicker/AlphaSlider.kt

+58-44
Original file line numberDiff line numberDiff line change
@@ -5,76 +5,90 @@ import android.graphics.*
55
import android.util.AttributeSet
66
import android.util.Size
77

8-
class AlphaSlider(context: Context, attrs: AttributeSet) :SliderView(context,attrs) {
9-
private var currentColor= Color.BLACK
10-
private val gridPaint= Paint().apply {
11-
style=Paint.Style.FILL
8+
class AlphaSlider(context: Context, attrs: AttributeSet) : SliderView(context, attrs) {
9+
private var currentColor = Color.RED
10+
private val gridPaint = Paint().apply {
11+
style = Paint.Style.FILL
1212
}
13-
private val boxSize= Size(10,10)
14-
private val gradientPaint=Paint().apply {
15-
style=Paint.Style.FILL
13+
private val boxSize = Size(10, 10)
14+
private val gradientPaint = Paint().apply {
15+
style = Paint.Style.FILL
1616
}
17+
18+
19+
private val rect = RectF()
20+
private val roundSize = resources.getDimensionPixelSize(R.dimen.color_picker_radi).toFloat()
21+
private val clipPath = Path()
1722
override fun onDraw(canvas: Canvas) {
23+
1824
//drawGrid
19-
val spaceH=getRadius().toFloat()
20-
val spaceV=2.toPx.toFloat()
21-
val w=maxTrackSize()
22-
val h=measuredHeight-spaceV*2f
23-
val rect=RectF().fromXYWH(
24-
spaceH,spaceV,w,h
25-
)
25+
val x = getRadius().toFloat() + paddingLeft
26+
val y = paddingTop.toFloat()
27+
val w = maxTrackSize()
28+
val h = (measuredHeight - paddingTop - paddingBottom).toFloat()
29+
rect.fromXYWH(x, y, w, h)
2630
canvas.save()
27-
canvas.clipRect(rect)
28-
canvas.translate(spaceH,spaceV)
29-
drawGrid(canvas,w.toInt())
31+
clipPath.reset()
32+
clipPath.addRoundRect(rect, roundSize, roundSize, Path.Direction.CCW)
33+
canvas.clipPath(clipPath)
34+
35+
canvas.translate(x, y)
36+
drawGrid(canvas, w.toInt())
3037
canvas.restore()
31-
canvas.drawRect(rect,gradientPaint)
38+
canvas.drawRoundRect(rect, roundSize, roundSize, gradientPaint)
3239
super.onDraw(canvas)
3340
}
41+
3442
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
3543
super.onSizeChanged(w, h, oldw, oldh)
3644
onSize()
3745
}
38-
private fun onSize(){
46+
47+
private fun onSize() {
3948
updateShader()
4049
}
41-
private fun updateShader(){
42-
gradientPaint.shader= LinearGradient(
43-
0f,0f,
44-
measuredWidth.toFloat(),0f,0,Utils.toRGB(currentColor),Shader.TileMode.CLAMP
50+
51+
private fun updateShader() {
52+
gradientPaint.shader = LinearGradient(
53+
0f, 0f,
54+
measuredWidth.toFloat(), 0f, 0, Utils.toRGB(currentColor), Shader.TileMode.CLAMP
4555
)
4656
}
47-
private fun drawGrid(canvas: Canvas,w:Int) {
48-
val colorA=Color.WHITE
49-
val colorB=Color.GRAY
50-
val countX=w/ boxSize.width
51-
val countY=measuredHeight/ boxSize.height
52-
for (y in 0..countY){
53-
for (x in 0..countX){
54-
val rect= RectF().apply {
55-
left=x*boxSize.width.toFloat()
56-
top=y*boxSize.height.toFloat()
57-
right+=left+boxSize.width
58-
bottom+=top+boxSize.height
57+
58+
private fun drawGrid(canvas: Canvas, w: Int) {
59+
val colorA = Color.WHITE
60+
val colorB = Color.GRAY
61+
val countX = w / boxSize.width
62+
val countY = measuredHeight / boxSize.height
63+
for (y in 0..countY) {
64+
for (x in 0..countX) {
65+
val rect = RectF().apply {
66+
left = x * boxSize.width.toFloat()
67+
top = y * boxSize.height.toFloat()
68+
right += left + boxSize.width
69+
bottom += top + boxSize.height
5970
}
60-
gridPaint.color=if((x+y)%2==0)colorA else colorB
61-
canvas.drawRect(rect,gridPaint)
71+
gridPaint.color = if ((x + y) % 2 == 0) colorA else colorB
72+
canvas.drawRect(rect, gridPaint)
6273
}
6374
}
6475
}
76+
6577
//public
66-
fun setColor(color:Int){
67-
if (currentColor!=color){
68-
currentColor=color
78+
fun setColor(color: Int) {
79+
if (currentColor != color) {
80+
currentColor = color
6981
updateShader()
7082
invalidate()
7183
}
7284
}
73-
fun setAlpha(alpha:Int){
74-
//just set ratio
75-
setRatio(alpha/255f)
85+
86+
fun setAlpha(alpha: Int) {
87+
//just set ratio
88+
setRatio(alpha / 255f)
7689
}
90+
7791
fun getAlphaValue(): Int {
78-
return (currentRatio*255).toInt()
92+
return (currentRatio * 255).toInt()
7993
}
8094
}

ColorPicker/src/main/java/com/abhishek/colorpicker/ColorBox.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ class ColorBox(context: Context, attrs: AttributeSet) : View(context, attrs) {
1616
color = Color.BLACK
1717
}
1818
private val rect = RectF()
19+
private val roundSize = resources.getDimensionPixelSize(R.dimen.color_picker_radi).toFloat()
1920
override fun onDraw(canvas: Canvas) {
2021
makeRects { rect, i, j ->
2122
paint.color = colorHolder.colors[i + j * 5]
2223
if (paint.color == 0) paint.color = Color.BLACK
23-
canvas.drawRect(rect, paint)
24+
canvas.drawRoundRect(rect, roundSize, roundSize, paint)
2425
return@makeRects false
2526
}
2627
}
@@ -48,7 +49,7 @@ class ColorBox(context: Context, attrs: AttributeSet) : View(context, attrs) {
4849

4950
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
5051
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
51-
val size = Utils.setWrapSize(200.toPx, 80.toPx, widthMeasureSpec, heightMeasureSpec)
52+
val size = Utils.setWrapSize(200.toPx + paddingLeft + paddingRight, 80.toPx + paddingTop + paddingBottom, widthMeasureSpec, heightMeasureSpec)
5253
setMeasuredDimension(size.width, size.height)
5354
}
5455

ColorPicker/src/main/java/com/abhishek/colorpicker/ColorComposer.kt

+58-39
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,59 @@ import android.view.MotionEvent
77
import android.view.View
88

99
open class ColorComposer(context: Context, attrs: AttributeSet) : View(context, attrs) {
10-
private var composeColor=Color.RED
10+
private var composeColor = Color.RED
1111
private lateinit var vShader: LinearGradient
1212
private lateinit var hShader: LinearGradient
13-
private val paint= Paint()
14-
private var bitmap: Bitmap?=null
13+
private val paint = Paint()
14+
private var bitmap: Bitmap? = null
15+
1516
//RATIO
16-
private var ratioPoint=PointF(0.5f,0.5f)
17-
private val cursorPaint=Paint().apply {
18-
style=Paint.Style.STROKE
19-
color=Color.GRAY
17+
private var ratioPoint = PointF(0.5f, 0.5f)
18+
private val cursorPaint = Paint().apply {
19+
style = Paint.Style.STROKE
20+
color = Color.GRAY
21+
strokeWidth = 3f
22+
isAntiAlias = true
2023
}
21-
private var oldColor=Color.BLACK
22-
private var colorChangeListener:(()->Unit)?=null
23-
fun setColorChangeListener(f:()->Unit){
24-
colorChangeListener=f
24+
private var oldColor = Color.BLACK
25+
private var colorChangeListener: (() -> Unit)? = null
26+
fun setColorChangeListener(f: () -> Unit) {
27+
colorChangeListener = f
2528
}
2629

2730
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
2831
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
29-
val size=Utils.setWrapSize(100.toPx,100.toPx,widthMeasureSpec,heightMeasureSpec)
30-
setMeasuredDimension(size.width,size.height)
32+
val size = Utils.setWrapSize(100.toPx, 100.toPx, widthMeasureSpec, heightMeasureSpec)
33+
setMeasuredDimension(size.width, size.height)
3134
}
3235

3336
override fun onDraw(canvas: Canvas) {
34-
if (bitmap!=null){
35-
canvas.drawBitmap(bitmap!!,null,getRectF(),null)
37+
if (bitmap != null) {
38+
canvas.drawBitmap(bitmap!!, null, getRectF(), null)
3639
}
3740
//draw cursor
41+
cursorPaint.color = Color.BLACK
42+
3843
canvas.drawCircle(
39-
ratioPoint.x*measuredWidth,
40-
ratioPoint.y*measuredHeight,
44+
ratioPoint.x * measuredWidth,
45+
ratioPoint.y * measuredHeight,
4146
5.toPx.toFloat(),
4247
cursorPaint
4348
)
49+
cursorPaint.color = Color.WHITE
50+
canvas.drawCircle(
51+
ratioPoint.x * measuredWidth,
52+
ratioPoint.y * measuredHeight,
53+
7.toPx.toFloat(),
54+
cursorPaint
55+
)
4456
}
4557

4658
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
4759
super.onSizeChanged(w, h, oldw, oldh)
4860
onSize()
4961
}
62+
5063
private fun onSize() {
5164
bitmap = Bitmap.createBitmap(measuredWidth, measuredHeight, Bitmap.Config.ARGB_8888)
5265
updateShaderAndBitmap()
@@ -63,43 +76,45 @@ open class ColorComposer(context: Context, attrs: AttributeSet) : View(context,
6376
)
6477
paint.shader = ComposeShader(vShader, hShader, PorterDuff.Mode.MULTIPLY)
6578
if (bitmap != null) {
66-
val canvas=Canvas(bitmap!!)
79+
val canvas = Canvas(bitmap!!)
6780
canvas.drawPaint(paint)
6881
}
6982
}
83+
7084
private fun updateRatioPoint(e: MotionEvent) {
71-
val p=PointF(
72-
e.x/measuredWidth.toFloat(),
73-
e.y/measuredHeight.toFloat(),
85+
val p = PointF(
86+
e.x / measuredWidth.toFloat(),
87+
e.y / measuredHeight.toFloat(),
7488
)
75-
if(p.x<0)p.x=0f
76-
if(p.x>1)p.x=1f
89+
if (p.x < 0) p.x = 0f
90+
if (p.x > 1) p.x = 1f
7791

78-
if(p.y<0)p.y=0f
79-
if(p.y>1)p.y=1f
92+
if (p.y < 0) p.y = 0f
93+
if (p.y > 1) p.y = 1f
8094

8195
setRatioPoint(p)
8296
}
8397

8498

8599
override fun onTouchEvent(e: MotionEvent): Boolean {
86-
when(e.action){
87-
MotionEvent.ACTION_DOWN->{
100+
when (e.action) {
101+
MotionEvent.ACTION_DOWN -> {
88102
updateRatioPoint(e)
89103
return true
90104
}
91-
MotionEvent.ACTION_MOVE->{
105+
MotionEvent.ACTION_MOVE -> {
92106
updateRatioPoint(e)
93107
return true
94108
}
95-
MotionEvent.ACTION_UP->{
109+
MotionEvent.ACTION_UP -> {
96110
return true
97111
}
98112

99113
}
100114

101115
return super.onTouchEvent(e)
102116
}
117+
103118
private fun updateCurrentColor() {
104119
val current = getColor()
105120

@@ -110,24 +125,27 @@ open class ColorComposer(context: Context, attrs: AttributeSet) : View(context,
110125
}
111126

112127
}
113-
private fun setRatioPoint(p:PointF){
114-
if(p.x !=ratioPoint.x || p.y!=ratioPoint.y){
115-
ratioPoint=p
128+
129+
private fun setRatioPoint(p: PointF) {
130+
if (p.x != ratioPoint.x || p.y != ratioPoint.y) {
131+
ratioPoint = p
116132
updateCurrentColor()
117133
invalidate()
118134
}
119135
}
120-
fun setComposeColor(color:Int){
121-
if(color!=composeColor){
122-
composeColor=color
136+
137+
fun setComposeColor(color: Int) {
138+
if (color != composeColor) {
139+
composeColor = color
123140
updateShaderAndBitmap()
124141
updateCurrentColor()
125142
}
126143
}
127-
fun setColor(color: Int){
128-
if(getColor()!=color){
144+
145+
fun setColor(color: Int) {
146+
if (getColor() != color) {
129147
//update
130-
val hsv=color.toHSV()
148+
val hsv = color.toHSV()
131149
setComposeColor(Color.HSVToColor(floatArrayOf(hsv[0], 1f, 1f)))
132150
setRatioPoint(
133151
PointF(
@@ -137,10 +155,11 @@ open class ColorComposer(context: Context, attrs: AttributeSet) : View(context,
137155
)
138156
}
139157
}
158+
140159
fun getColor(): Int {
141160
val hsv = composeColor.toHSV()
142161
hsv[1] = ratioPoint.x
143-
hsv[2] = 1f-ratioPoint.y
162+
hsv[2] = 1f - ratioPoint.y
144163
return Color.HSVToColor(hsv)
145164
}
146165

ColorPicker/src/main/java/com/abhishek/colorpicker/ColorPickerDialog.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.abhishek.colorpicker
22

3+
import android.app.ActionBar.LayoutParams
34
import android.graphics.Color
45
import android.os.Bundle
56
import android.view.LayoutInflater
@@ -16,7 +17,7 @@ import java.io.*
1617

1718
class ColorPickerDialog : DialogFragment() {
1819
private lateinit var binding: FragmentColorPickerDialogBinding
19-
private var currentColor = MutableLiveData(Color.argb(100, 255, 0, 0))
20+
private var currentColor = MutableLiveData(Color.argb(255, 255, 0, 0))
2021
private var okCancelListener: OkCancelFun? = null
2122
private var colorHolder = MutableLiveData(ColorHolder())
2223
private lateinit var colorHolderFile: File
@@ -58,7 +59,7 @@ class ColorPickerDialog : DialogFragment() {
5859
val metrics = resources.displayMetrics
5960
val width = metrics.widthPixels
6061
val height = metrics.heightPixels
61-
requireDialog().window?.setLayout((6 * width) / 7, height * 80 / 100)
62+
requireDialog().window?.setLayout((6 * width) / 7, LayoutParams.WRAP_CONTENT)
6263
}
6364
private fun initialize(){
6465
binding.apply {

0 commit comments

Comments
 (0)