Skip to content

Instantly share code, notes, and snippets.

@mattdesl
Created March 2, 2015 03:18
Show Gist options
  • Save mattdesl/52e125f406286fb33a19 to your computer and use it in GitHub Desktop.
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/
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