-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingleColorPicker.controller.js
204 lines (177 loc) · 8.6 KB
/
singleColorPicker.controller.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
angular.module("umbraco")
.controller("PRmedia.SingleColorPicker", function ($scope, $element, colorLibraryResource) {
$scope.listedColors = [];
var showButton = $scope.model.config.showAddToLibraryButton;
if (showButton != 1)
{
$($element).find(".colorList").hide();
$($element).find(".addToLibrary").hide();
$($element).find(".colorName").hide();
}
else
{
$($element).find(".colorList").show();
$($element).find(".addToLibrary").show();
$($element).find(".colorName").show();
$scope.listedColors = ["#00FF00"];
updateColorList();
}
$($element).find("div[class*='Slider']").slider({ orientation: "horizontal", range: "min", max: 255 });
$($element).find(".alphaSlider").slider({ orientation: "horizontal", range: "min", max: 1, min: 0, step: 0.01 });
var sliderRed = $($element).find(".redSlider"),
valueRed = $($element).find(".redValue"),
sliderGreen = $($element).find(".greenSlider"),
valueGreen = $($element).find(".greenValue"),
sliderBlue = $($element).find(".blueSlider"),
valueBlue = $($element).find(".blueValue"),
sliderAlpha = $($element).find(".alphaSlider"),
valueAlpha = $($element).find(".alphaValue"),
valueHex = $($element).find(".hexValue"),
colorBox = $($element).find(".colorBox"),
textPreview = $($element).find(".colorTextPreview");
// Set color values from database or default
if (angular.isArray($scope.model.value))
{
var scheme = $scope.model.value[0];
$(sliderRed).slider({ value: scheme.red, slide: refreshSwatch, change: refreshSwatch });
$(sliderGreen).slider({ value: scheme.green, slide: refreshSwatch, change: refreshSwatch });
$(sliderBlue).slider({ value: scheme.blue, slide: refreshSwatch, change: refreshSwatch });
$(sliderAlpha).slider({ value: scheme.alpha, slide: refreshSwatch, change: refreshSwatch });
$(colorBox).css("background", "rgba(" + scheme.red + ", " + scheme.green + "," + scheme.blue + "," + scheme.alpha + ")");
$(valueRed).val(scheme.red);
$(valueGreen).val(scheme.green);
$(valueBlue).val(scheme.blue);
$(valueAlpha).val(scheme.alpha);
var firstHexColor = getHexCode(scheme.red, scheme.green, scheme.blue);
$(valueHex).val(firstHexColor);
$(textPreview).css("color", firstHexColor);
}
else
{
$(sliderRed).slider({ value: 255, slide: refreshSwatch, change: refreshSwatch });
$(sliderGreen).slider({ value: 255, slide: refreshSwatch, change: refreshSwatch });
$(sliderBlue).slider({ value: 255, slide: refreshSwatch, change: refreshSwatch });
$(sliderAlpha).slider({ value: 1, slide: refreshSwatch, change: refreshSwatch });
$(colorBox).css("background", "rgba(255,255,255,1)");
$(textPreview).css("color", "#000000");
$(valueRed).val(255);
$(valueGreen).val(255);
$(valueBlue).val(255);
$(valueAlpha).val(1);
}
// Slider event handler => change values
$($element).find(".colorBar input").on("change paste", function (e)
{
var value = this.value;
var percentValue = (this.value / this.max * 100);
var parentBox = e.currentTarget.parentElement;
$(parentBox).find("div[class*='Slider']").slider({ value: this.value });
});
// HEX color input event handler => change values
$($element).find(".hexColorBar input").on("change paste", function (e)
{
//console.log(e);
var value = this.value;
var isOk = /(^#[0-9A-F]{6}$)/i.test(value);
if (isOk) {
var newValue = hexToRgb(value);
//console.log(newValue);
var parentBox = e.currentTarget.parentElement.parentElement;
//console.log(parentBox);
$(sliderRed).slider({ value: newValue.r });
$(sliderGreen).slider({ value: newValue.g });
$(sliderBlue).slider({ value: newValue.b });
$(this).css("border-color", "#bbbabf");
}
else if (!$(this).val() ) {
$(this).css("border-color", "#bbbabf");
}
else {
$(this).css("border-color", "red");
}
});
$scope.addColorToLibrary = function ()
{
var newColor = $(valueHex).val();
var nameBox = $($element).find(".colorName");
if (!$(nameBox).val()) {
$(nameBox).css("border-color", "red");
}
else {
$(nameBox).css("border-color", "#bbbabf");
var name = $(nameBox).val();
colorLibraryResource.saveNewColor(newColor, name).then(function (response) {
updateColorList();
});
$(nameBox).val("");
}
}
// Library click events => update value
$scope.setColorValue = function (color)
{
var hexCode = color;
var rgbCode = hexToRgb(hexCode);
//$(valueHex).val(hexCode);
$(sliderRed).slider({ value: rgbCode.r });
$(sliderGreen).slider({ value: rgbCode.g });
$(sliderBlue).slider({ value: rgbCode.b });
}
function updateColorList()
{
colorLibraryResource.getColorList().then(function (response) {
var existingColorsView = $($element).find(".existingColors");
//$(existingColorsView).empty();
$scope.listedColors = [];
$.each(response, function (key, value) {
var color = {
HexCode: value.HexCode,
Name: value.Name
}
$scope.listedColors.push(color);
//$(existingColorsView).append("<div class='color' style='background: " + value.HexCode + ";'></div>");
});
});
}
function hexToRgb(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
function rgbToHex(rgb) {
var hex = Number(rgb).toString(16);
if (hex.length < 2) {
hex = "0" + hex;
}
return hex;
}
function getHexCode(red, green, blue) {
var hexRed = rgbToHex(red),
hexGreen = rgbToHex(green),
hexBlue = rgbToHex(blue);
return "#" + hexRed + hexGreen + hexBlue;
}
function refreshSwatch() {
var redOne = $(sliderRed).slider("value"),
greenOne = $(sliderGreen).slider("value"),
blueOne = $(sliderBlue).slider("value"),
alphaOne = $(sliderAlpha).slider("value");
var firstHexCode = "#" + rgbToHex(redOne) + rgbToHex(greenOne) + rgbToHex(blueOne);
$(valueHex).val(firstHexCode);
$(valueRed).val(redOne);
$(valueGreen).val(greenOne);
$(valueBlue).val(blueOne);
$(valueAlpha).val(alphaOne);
$(colorBox).css("background", "rgba(" + redOne + ", " + greenOne + "," + blueOne + "," + alphaOne + ")");
//$scope.model.value = "linear-gradient(rgba(" + redOne + ", " + greenOne + "," + blueOne + ",1), rgba(" + redTwo + ", " + greenTwo + ", " + blueTwo + ", 1))";
//$scope.model.value = [redOne,greenOne,blueOne,alphaOne,redTwo,greenTwo,blueTwo,alphaTwo,degrees];
$scope.model.value = '[{ "red": ' + redOne + ', ' +
'"green": ' + greenOne + ',' +
'"blue": ' + blueOne + ',' +
'"alpha": ' + alphaOne + '}]';
//$($element).find(".colorCode").val("rgba(" + redOne + ", " + greenOne + "," + blueOne + "," + alphaOne + ")");
$(textPreview).css("color", "rgba(" + redOne + ", " + greenOne + "," + blueOne + "," + alphaOne + ")");
}
});