This repository has been archived by the owner on Dec 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathColorPicker.js
180 lines (169 loc) · 7.72 KB
/
ColorPicker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* @file ModPE widget library for providing color picker API.
* @author Astro <[email protected]>
* @version 1.0
* @license Apache-2.0
*/
/** @namespace global */
($ => {
"use strict";
const Bitmap_ = android.graphics.Bitmap,
Canvas_ = android.graphics.Canvas,
Color_ = android.graphics.Color,
LinearGradient_ = android.graphics.LinearGradient,
Paint_ = android.graphics.Paint,
Shader_ = android.graphics.Shader,
BitmapDrawable_ = android.graphics.drawable.BitmapDrawable,
ColorDrawable_ = android.graphics.drawable.ColorDrawable,
Gravity_ = android.view.Gravity,
MotionEvent_ = android.view.MotionEvent,
View_ = android.view.View,
LinearLayout_ = android.widget.LinearLayout,
PopupWindow_ = android.widget.PopupWindow,
TextView_ = android.widget.TextView,
CONTEXT = com.mojang.minecraftpe.MainActivity.currentMainActivity.get(),
DP = CONTEXT.getResources().getDisplayMetrics().density;
/**
* Class representing a color picker.
* @since 2016-05-04
* @class
* @memberOf global
* @param {Number} [r=255] Red
* @param {Number} [g=0] Blue
* @param {Number} [b=0] Green
* @param {Function} [func=function(){}] Callback to be invoked when color was changed
*/
function ColorPicker(r, g, b, func) {
r = r || 255;
g = g || 0;
b = b || 0;
func = func || (() => {});
let controller = this._controller = new TextView_(CONTEXT),
picker = this._picker = new TextView_(CONTEXT),
paint = new Paint_(),
bitmapC = Bitmap_.createBitmap(40, 240, Bitmap_.Config.ARGB_8888),
bitmapP = Bitmap_.createBitmap(240, 240, Bitmap_.Config.ARGB_8888),
canvasC = new Canvas_(bitmapC),
canvasP = new Canvas_(bitmapP),
pickerX = 120,
pickerY = 1;
paint.setShader(new LinearGradient_(0, 0, 0, 40, Color_.rgb(255, 0, 0), Color_.rgb(255, 255, 0), Shader_.TileMode.CLAMP));
canvasC.drawRect(0, 0, 40, 40, paint);
paint.setShader(new LinearGradient_(0, 40, 0, 80, Color_.rgb(255, 255, 0), Color_.rgb(0, 255, 0), Shader_.TileMode.CLAMP));
canvasC.drawRect(0, 40, 40, 80, paint);
paint.setShader(new LinearGradient_(0, 80, 0, 120, Color_.rgb(0, 255, 0), Color_.rgb(0, 255, 255), Shader_.TileMode.CLAMP));
canvasC.drawRect(0, 80, 40, 120, paint);
paint.setShader(new LinearGradient_(0, 120, 0, 160, Color_.rgb(0, 255, 255), Color_.rgb(0, 0, 255), Shader_.TileMode.CLAMP));
canvasC.drawRect(0, 120, 40, 160, paint);
paint.setShader(new LinearGradient_(0, 160, 0, 200, Color_.rgb(0, 0, 255), Color_.rgb(255, 0, 255), Shader_.TileMode.CLAMP));
canvasC.drawRect(0, 160, 40, 200, paint);
paint.setShader(new LinearGradient_(0, 200, 0, 240, Color_.rgb(255, 0, 255), Color_.rgb(255, 0, 0), Shader_.TileMode.CLAMP));
canvasC.drawRect(0, 200, 40, 240, paint);
paint.setShader(new LinearGradient_(240, 0, 0, 0, Color_.rgb(r, g, b), Color_.WHITE, Shader_.TileMode.CLAMP));
canvasP.drawRect(0, 0, 240, 240, paint);
paint.setShader(new LinearGradient_(0, 0, 0, 240, Color_.TRANSPARENT, Color_.BLACK, Shader_.TileMode.CLAMP));
canvasP.drawRect(0, 0, 240, 240, paint);
controller.setBackgroundDrawable(new BitmapDrawable_(bitmapC));
controller.setOnTouchListener(new View_.OnTouchListener({
onTouch(view, event) {
let action = event.getAction();
if (action === MotionEvent_.ACTION_DOWN || action === MotionEvent_.ACTION_MOVE || action === MotionEvent_.ACTION_UP) {
let x = Math.floor(event.getX() / DP),
y = Math.floor(event.getY() / DP);
if (x > 0 && x < 40 && y > 0 && y < 240) {
paint.setShader(new LinearGradient_(240, 0, 0, 0, bitmapC.getPixel(0, y), Color_.WHITE, Shader_.TileMode.CLAMP));
canvasP.drawRect(0, 0, 240, 240, paint);
paint.setShader(new LinearGradient_(0, 0, 0, 240, Color_.TRANSPARENT, Color_.BLACK, Shader_.TileMode.CLAMP));
canvasP.drawRect(0, 0, 240, 240, paint);
picker.setBackgroundDrawable(new BitmapDrawable_(bitmapP));
func(bitmapP.getPixel(pickerX, pickerY));
}
}
return true;
}
}));
picker.setBackgroundDrawable(new BitmapDrawable_(bitmapP));
picker.setOnTouchListener(new View_.OnTouchListener({
onTouch(view, event) {
let action = event.getAction();
if (action === MotionEvent_.ACTION_DOWN || action === MotionEvent_.ACTION_MOVE || action === MotionEvent_.ACTION_UP) {
let x = Math.floor(event.getX() / DP),
y = Math.floor(event.getY() / DP);
if (x > 0 && x < 240 && y > 0 && y < 240) {
pickerX = x;
pickerY = y;
func(bitmapP.getPixel(x, y));
}
}
return true;
}
}));
}
/**
* Returns the widget of color picker.
* @since 2016-05-04
* @returns {android.widget.LinearLayout} the widget of color picker
*/
ColorPicker.prototype.show = function () {
let layout = new LinearLayout_(CONTEXT);
layout.addView(this._picker, DP * 240, DP * 240);
layout.addView(this._controller, DP * 40, DP * 240);
return layout;
};
/**
* Class representing a window that shows a color picker.
* @since 2016-05-04
* @class
* @memberOf global
* @param {Number} [r=255] Red
* @param {Number} [g=0] Green
* @param {Number} [b=0] Blue
* @param {Function} [func=function(){}] Callback to be invoked when color was changed
*/
function ColorPickerWindow(r, g, b, func) {
r = r || 255;
g = g || 0;
b = b || 0;
func = func || (() => {});
let viewer = this._viewer = new TextView_(CONTEXT);
this._picker = new ColorPicker(r, g, b, color => {
viewer.setBackgroundDrawable(new ColorDrawable_(color));
func(color);
});
viewer.setBackgroundDrawable(new ColorDrawable_(Color_.rgb(r, g, b)));
viewer.setGravity(Gravity_.CENTER);
viewer.setText("Click to close");
viewer.setTextColor(Color_.rgb(r, g, b));
viewer.setTextSize(1, 18);
}
/**
* Display the window of color picker.
* @since 2016-05-04
*/
ColorPickerWindow.prototype.show = function () {
let thiz = this;
CONTEXT.runOnUiThread({
run() {
let layout = new LinearLayout_(CONTEXT),
window = new PopupWindow_(layout, -2, -2);
thiz._viewer.setOnClickListener(new View_.OnClickListener({
onClick(view) {
CONTEXT.runOnUiThread({
run() {
window.dismiss();
window = null;
}
});
}
}));
layout.addView(thiz._picker.show(), DP * 280, DP * 240);
layout.addView(thiz._viewer, DP * 280, DP * 100);
layout.setOrientation(1);
window.setBackgroundDrawable(new ColorDrawable_(Color_.rgb(97, 97, 97)));
window.showAtLocation(CONTEXT.getWindow().getDecorView(), Gravity_.CENTER, 0, 0);
}
});
};
$.ColorPicker = ColorPicker;
$.ColorPickerWindow = ColorPickerWindow;
})(this);