Skip to content

Commit 225f8e5

Browse files
authored
Merge pull request #506 from Kitware/add-helpers
Add helpers
2 parents 2d6066c + ab70db4 commit 225f8e5

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

Sources/Common/Core/Math/index.js

+34
Original file line numberDiff line numberDiff line change
@@ -1557,6 +1557,39 @@ function solveLeastSquares(
15571557
return successFlag;
15581558
}
15591559

1560+
function hex2float(hexStr, outFloatArray = [0, 0.5, 1]) {
1561+
switch (hexStr.length) {
1562+
case 3: // abc => #aabbcc
1563+
outFloatArray[0] = parseInt(hexStr[0], 16) * 17 / 255;
1564+
outFloatArray[1] = parseInt(hexStr[1], 16) * 17 / 255;
1565+
outFloatArray[2] = parseInt(hexStr[2], 16) * 17 / 255;
1566+
return outFloatArray;
1567+
case 4: // #abc => #aabbcc
1568+
outFloatArray[0] = parseInt(hexStr[1], 16) * 17 / 255;
1569+
outFloatArray[1] = parseInt(hexStr[2], 16) * 17 / 255;
1570+
outFloatArray[2] = parseInt(hexStr[3], 16) * 17 / 255;
1571+
return outFloatArray;
1572+
case 6: // ab01df => #ab01df
1573+
outFloatArray[0] = parseInt(hexStr.substr(0, 2), 16) / 255;
1574+
outFloatArray[1] = parseInt(hexStr.substr(2, 2), 16) / 255;
1575+
outFloatArray[2] = parseInt(hexStr.substr(4, 2), 16) / 255;
1576+
return outFloatArray;
1577+
case 7: // #ab01df
1578+
outFloatArray[0] = parseInt(hexStr.substr(1, 2), 16) / 255;
1579+
outFloatArray[1] = parseInt(hexStr.substr(3, 2), 16) / 255;
1580+
outFloatArray[2] = parseInt(hexStr.substr(5, 2), 16) / 255;
1581+
return outFloatArray;
1582+
case 9: // #ab01df00
1583+
outFloatArray[0] = parseInt(hexStr.substr(1, 2), 16) / 255;
1584+
outFloatArray[1] = parseInt(hexStr.substr(3, 2), 16) / 255;
1585+
outFloatArray[2] = parseInt(hexStr.substr(5, 2), 16) / 255;
1586+
outFloatArray[3] = parseInt(hexStr.substr(7, 2), 16) / 255;
1587+
return outFloatArray;
1588+
default:
1589+
return outFloatArray;
1590+
}
1591+
}
1592+
15601593
function rgb2hsv(rgb, hsv) {
15611594
let h;
15621595
let s;
@@ -2017,6 +2050,7 @@ export default {
20172050
jacobiN,
20182051
solveHomogeneousLeastSquares,
20192052
solveLeastSquares,
2053+
hex2float,
20202054
rgb2hsv,
20212055
hsv2rgb,
20222056
lab2xyz,

Sources/Interaction/UI/CornerAnnotation/index.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,24 @@ const KEY_MAPPING = {
1414
se: 'southEastContainer',
1515
};
1616

17+
// ----------------------------------------------------------------------------
18+
// Static helpers
19+
// ----------------------------------------------------------------------------
20+
21+
function get(path, obj, fb = `$\{${path}}`) {
22+
return path
23+
.split('.')
24+
.reduce((res, key) => (res[key] !== undefined ? res[key] : fb), obj);
25+
}
26+
27+
/* from https://gist.github.com/smeijer/6580740a0ff468960a5257108af1384e */
28+
function applyTemplate(template, map, fallback) {
29+
return template.replace(/\${([^{]+)}/g, (match) => {
30+
const path = match.substr(2, match.length - 3).trim();
31+
return get(path, map, fallback);
32+
});
33+
}
34+
1735
// ----------------------------------------------------------------------------
1836
// vtkCornerAnnotation methods
1937
// ----------------------------------------------------------------------------
@@ -143,4 +161,4 @@ export const newInstance = macro.newInstance(extend, 'vtkCornerAnnotation');
143161

144162
// ----------------------------------------------------------------------------
145163

146-
export default { newInstance, extend };
164+
export default { newInstance, extend, applyTemplate };

0 commit comments

Comments
 (0)