Skip to content

Commit

Permalink
Merge pull request #506 from Kitware/add-helpers
Browse files Browse the repository at this point in the history
Add helpers
  • Loading branch information
jourdain authored Jan 17, 2018
2 parents 2d6066c + ab70db4 commit 225f8e5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
34 changes: 34 additions & 0 deletions Sources/Common/Core/Math/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1557,6 +1557,39 @@ function solveLeastSquares(
return successFlag;
}

function hex2float(hexStr, outFloatArray = [0, 0.5, 1]) {
switch (hexStr.length) {
case 3: // abc => #aabbcc
outFloatArray[0] = parseInt(hexStr[0], 16) * 17 / 255;
outFloatArray[1] = parseInt(hexStr[1], 16) * 17 / 255;
outFloatArray[2] = parseInt(hexStr[2], 16) * 17 / 255;
return outFloatArray;
case 4: // #abc => #aabbcc
outFloatArray[0] = parseInt(hexStr[1], 16) * 17 / 255;
outFloatArray[1] = parseInt(hexStr[2], 16) * 17 / 255;
outFloatArray[2] = parseInt(hexStr[3], 16) * 17 / 255;
return outFloatArray;
case 6: // ab01df => #ab01df
outFloatArray[0] = parseInt(hexStr.substr(0, 2), 16) / 255;
outFloatArray[1] = parseInt(hexStr.substr(2, 2), 16) / 255;
outFloatArray[2] = parseInt(hexStr.substr(4, 2), 16) / 255;
return outFloatArray;
case 7: // #ab01df
outFloatArray[0] = parseInt(hexStr.substr(1, 2), 16) / 255;
outFloatArray[1] = parseInt(hexStr.substr(3, 2), 16) / 255;
outFloatArray[2] = parseInt(hexStr.substr(5, 2), 16) / 255;
return outFloatArray;
case 9: // #ab01df00
outFloatArray[0] = parseInt(hexStr.substr(1, 2), 16) / 255;
outFloatArray[1] = parseInt(hexStr.substr(3, 2), 16) / 255;
outFloatArray[2] = parseInt(hexStr.substr(5, 2), 16) / 255;
outFloatArray[3] = parseInt(hexStr.substr(7, 2), 16) / 255;
return outFloatArray;
default:
return outFloatArray;
}
}

function rgb2hsv(rgb, hsv) {
let h;
let s;
Expand Down Expand Up @@ -2017,6 +2050,7 @@ export default {
jacobiN,
solveHomogeneousLeastSquares,
solveLeastSquares,
hex2float,
rgb2hsv,
hsv2rgb,
lab2xyz,
Expand Down
20 changes: 19 additions & 1 deletion Sources/Interaction/UI/CornerAnnotation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ const KEY_MAPPING = {
se: 'southEastContainer',
};

// ----------------------------------------------------------------------------
// Static helpers
// ----------------------------------------------------------------------------

function get(path, obj, fb = `$\{${path}}`) {
return path
.split('.')
.reduce((res, key) => (res[key] !== undefined ? res[key] : fb), obj);
}

/* from https://gist.github.com/smeijer/6580740a0ff468960a5257108af1384e */
function applyTemplate(template, map, fallback) {
return template.replace(/\${([^{]+)}/g, (match) => {
const path = match.substr(2, match.length - 3).trim();
return get(path, map, fallback);
});
}

// ----------------------------------------------------------------------------
// vtkCornerAnnotation methods
// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -143,4 +161,4 @@ export const newInstance = macro.newInstance(extend, 'vtkCornerAnnotation');

// ----------------------------------------------------------------------------

export default { newInstance, extend };
export default { newInstance, extend, applyTemplate };

0 comments on commit 225f8e5

Please sign in to comment.