forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoolbox.ts
More file actions
193 lines (178 loc) · 6.25 KB
/
toolbox.ts
File metadata and controls
193 lines (178 loc) · 6.25 KB
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
namespace pxt.toolbox {
export const blockColors: Map<number | string> = {
loops: '#107c10',
logic: '#006970',
math: '#712672',
variables: '#A80000',
functions: '#005a9e',
text: '#996600',
arrays: '#A94400',
advanced: '#3c3c3c',
addpackage: '#717171',
search: '#000',
debug: '#e03030',
default: '#dddddd',
topblocks: '#aa8f00',
recipes: '#717171'
}
export const blockIcons: Map<number | string> = {
loops: '\uf01e',
logic: '\uf074',
math: '\uf1ec',
variables: '\uf039',
functions: '\uf109',
text: '\uf035',
arrays: '\uf0cb',
advancedcollapsed: '\uf078',
advancedexpanded: '\uf077',
more: '\uf141',
addpackage: '\uf055',
search: '\uf002',
debug: '\uf111',
default: '\uf12e',
topblocks: '\uf005',
recipes: '\uf0eb'
}
let toolboxStyleBuffer: string = '';
export function appendToolboxIconCss(className: string, i: string): void {
if (toolboxStyleBuffer.indexOf(className) > -1) return;
if (i.length === 1) {
const icon = Util.unicodeToChar(i);
toolboxStyleBuffer += `
.blocklyTreeIcon.${className}::before {
content: "${icon}";
}
`;
}
else {
toolboxStyleBuffer += `
.blocklyTreeIcon.${className} {
background-image: url("${Util.pathJoin(pxt.webConfig.commitCdnUrl, encodeURI(i))}")!important;
width: 30px;
height: 100%;
background-size: 20px !important;
background-repeat: no-repeat !important;
background-position: 50% 50% !important;
}
`;
}
}
export function getNamespaceColor(ns: string): string {
ns = ns.toLowerCase();
if (pxt.appTarget.appTheme.blockColors && pxt.appTarget.appTheme.blockColors[ns])
return pxt.appTarget.appTheme.blockColors[ns] as string;
if (pxt.toolbox.blockColors[ns])
return pxt.toolbox.blockColors[ns] as string;
return "";
}
export function getNamespaceIcon(ns: string): string {
ns = ns.toLowerCase();
if (pxt.appTarget.appTheme.blockIcons && pxt.appTarget.appTheme.blockIcons[ns]) {
return pxt.appTarget.appTheme.blockIcons[ns] as string;
}
if (pxt.toolbox.blockIcons[ns]) {
return pxt.toolbox.blockIcons[ns] as string;
}
return "";
}
export function advancedTitle() { return Util.lf("{id:category}Advanced"); }
export function addPackageTitle() { return Util.lf("{id:category}Extensions"); }
export function recipesTitle() { return Util.lf("{id:category}Tutorials"); }
/**
* Convert blockly hue to rgb
*/
export function convertColor(colour: string): string {
colour = parseHex(colour); // convert from 0x hex encoding if necessary
const hue = parseInt(colour);
if (!isNaN(hue)) {
return hueToRgb(hue);
}
return colour;
}
export function hueToRgb(hue: number) {
const HSV_SATURATION = 0.45;
const HSV_VALUE = 0.65 * 255;
const rgbArray = hsvToRgb(hue, HSV_SATURATION, HSV_VALUE);
return `#${componentToHex(rgbArray[0])}${componentToHex(rgbArray[1])}${componentToHex(rgbArray[2])}`;
}
/**
* Converts an HSV triplet to an RGB array. V is brightness because b is
* reserved for blue in RGB.
* Closure's HSV to RGB function: https://github.com/google/closure-library/blob/master/closure/goog/color/color.js#L613
*/
function hsvToRgb(h: number, s: number, brightness: number) {
let red = 0;
let green = 0;
let blue = 0;
if (s == 0) {
red = brightness;
green = brightness;
blue = brightness;
} else {
let sextant = Math.floor(h / 60);
let remainder = (h / 60) - sextant;
let val1 = brightness * (1 - s);
let val2 = brightness * (1 - (s * remainder));
let val3 = brightness * (1 - (s * (1 - remainder)));
switch (sextant) {
case 1:
red = val2;
green = brightness;
blue = val1;
break;
case 2:
red = val1;
green = brightness;
blue = val3;
break;
case 3:
red = val1;
green = val2;
blue = brightness;
break;
case 4:
red = val3;
green = val1;
blue = brightness;
break;
case 5:
red = brightness;
green = val1;
blue = val2;
break;
case 6:
case 0:
red = brightness;
green = val3;
blue = val1;
break;
}
}
return [Math.floor(red), Math.floor(green), Math.floor(blue)];
}
function componentToHex(c: number) {
const hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
function parseHex(s: string) {
if (!s) return "#000000";
if (s.substring(0, 2) == "0x") return "#" + s.substring(2);
return s;
}
export function fadeColor(hex: string, luminosity: number, lighten: boolean): string {
// #ABC => ABC
hex = hex.replace(/[^0-9a-f]/gi, '');
// ABC => AABBCC
if (hex.length < 6)
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
// tweak
let rgb = "#";
for (let i = 0; i < 3; i++) {
let c = parseInt(hex.substr(i * 2, 2), 16);
c = Math.round(Math.min(Math.max(0, lighten ? c + (c * luminosity) : c - (c * luminosity)), 255));
let cStr = c.toString(16);
rgb += ("00" + cStr).substr(cStr.length);
}
return rgb;
}
}