-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcolorsConverter.js
100 lines (92 loc) · 2.44 KB
/
colorsConverter.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
// https://akelpad.sourceforge.net/forum/viewtopic.php?p=9926#p9926
// https://infocatcher.ucoz.net/js/akelpad_scripts/colorsConverter.js
// https://github.com/Infocatcher/AkelPad_scripts/blob/master/colorsConverter.js
// (c) Infocatcher 2010-2011
// Version: 0.1.2 - 2011-12-20
// Author: Infocatcher
//// Convert color between "#fee" and "rgb(255, 238, 238)" formats
function _localize(s) {
var strings = {
"Color:": {
ru: "Цвет:"
},
"Color from “%S”:": {
ru: "Цвет из «%S»:"
},
"Invalid color format!": {
ru: "Некорректный формат цвета!"
}
};
var lng = "en";
switch(AkelPad.GetLangId(1 /*LANGID_PRIMARY*/)) {
case 0x19: lng = "ru";
}
_localize = function(s) {
return strings[s] && strings[s][lng] || s;
};
return _localize(s);
}
//var AkelPad = new ActiveXObject("AkelPad.document");
var hMainWnd = AkelPad.GetMainWnd();
if(hMainWnd)
convColor(AkelPad.GetSelText());
function convColor(color, forceAsk) {
if(!color || forceAsk)
color = askColor(_localize("Color:"), color);
if(!color)
return;
var newColor;
if(/^\W*#?([0-9a-f]{3}|[0-9a-f]{6})\W*$/i.test(color)) // #aaa or #aaaaaa
newColor = h2d(RegExp.$1);
else if(/^\D*(\d{1,3}\D+\d{1,3}\D+\d{1,3})\D*$/.test(color)) // rgb(170, 170, 170)
newColor = d2h.apply(this, RegExp.$1.split(/\D+/));
if(!newColor) {
AkelPad.MessageBox(hMainWnd, _localize("Invalid color format!"), WScript.ScriptName, 48 /*MB_ICONEXCLAMATION*/);
convColor(color, true);
return;
}
newColor = askColor(_localize("Color from “%S”:").replace("%S", color), newColor);
newColor && convColor(newColor);
}
function askColor(caption, defaultValue) {
return AkelPad.InputBox(
hMainWnd, WScript.ScriptName,
caption,
defaultValue || ""
);
}
function hex(n) {
var h = (typeof n == "number" ? n : parseInt(n, 10)).toString(16);
if(h.length > 2)
return null;
return "00".substr(h.length) + h;
}
function d2h() {
var i, h, r = [], l = arguments.length;
var same = /^([0-9a-f])\1$/i;
var isSame = true;
for(i = 0; i < l; i++) {
h = hex(arguments[i]);
if(!h)
return null;
if(isSame && !same.test(h))
isSame = false;
r.push(h);
}
if(isSame)
for(i = 0; i < l; i++)
r[i] = r[i].charAt(0);
return /* "#" + */ r.join("");
}
function h2d(h) {
var l = h.length;
var s = l == 3 ? 1 : 2;
var i, r = [], h2;
for(var i = 0; i < l; i += s) {
h2 = h.substr(i, s);
if(s == 1)
h2 += h2;
r.push(parseInt(h2, 16));
}
return r.join(", ");
}