UNPKG

dat.gui

Version:

A lightweight graphical user interface for changing variables in JavaScript.

53 lines (48 loc) 1.82 kB
/** * dat-gui JavaScript Controller Library * https://github.com/dataarts/dat.gui * * Copyright 2011 Data Arts Team, Google Creative Lab * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 */ export default function(color, forceCSSHex) { const colorFormat = color.__state.conversionName.toString(); const r = Math.round(color.r); const g = Math.round(color.g); const b = Math.round(color.b); const a = color.a; const h = Math.round(color.h); const s = color.s.toFixed(1); const v = color.v.toFixed(1); if (forceCSSHex || (colorFormat === 'THREE_CHAR_HEX') || (colorFormat === 'SIX_CHAR_HEX')) { let str = color.hex.toString(16); while (str.length < 6) { str = '0' + str; } return '#' + str; } else if (colorFormat === 'CSS_RGB') { return 'rgb(' + r + ',' + g + ',' + b + ')'; } else if (colorFormat === 'CSS_RGBA') { return 'rgba(' + r + ',' + g + ',' + b + ',' + a + ')'; } else if (colorFormat === 'HEX') { return '0x' + color.hex.toString(16); } else if (colorFormat === 'RGB_ARRAY') { return '[' + r + ',' + g + ',' + b + ']'; } else if (colorFormat === 'RGBA_ARRAY') { return '[' + r + ',' + g + ',' + b + ',' + a + ']'; } else if (colorFormat === 'RGB_OBJ') { return '{r:' + r + ',g:' + g + ',b:' + b + '}'; } else if (colorFormat === 'RGBA_OBJ') { return '{r:' + r + ',g:' + g + ',b:' + b + ',a:' + a + '}'; } else if (colorFormat === 'HSV_OBJ') { return '{h:' + h + ',s:' + s + ',v:' + v + '}'; } else if (colorFormat === 'HSVA_OBJ') { return '{h:' + h + ',s:' + s + ',v:' + v + ',a:' + a + '}'; } return 'unknown format'; }