Created
March 2, 2015 03:18
-
-
Save mattdesl/52e125f406286fb33a19 to your computer and use it in GitHub Desktop.
ES6 tagged template strings for GLSL hex-to-vec3 http://tc39wiki.calculist.org/es6/template-strings/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { getRgb } from 'color-string' | |
function glsl(strings, ...variables) { | |
variables = variables.map(v => { | |
var rgb = getRgb(v) | |
.map(x => x/255) | |
.slice(0, 3) | |
.join(', ') | |
return `vec3(${rgb})` | |
}) | |
var str = [] | |
strings.forEach((x, i) => { | |
str.push(x) | |
str.push(variables[i]||'') | |
}) | |
return str.join('') | |
} | |
var background = '#ff0' | |
var snippet = glsl` | |
vec3 color = ${'cyan'}; | |
gl_FragColor.rgb = ${background}; | |
` | |
console.log(snippet) | |
// ... prints ... | |
// vec3 color = vec3(0, 1, 1); | |
// gl_FragColor.rgb = vec3(1, 1, 0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment