UNPKG

2.41 kBJavaScriptView Raw
1!function(r,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):r.pointsToVertices=t()}(this,function(){"use strict";var r=function(r){return r/255},t=function(r,t,o){return o<r?r:o>t?t:o},o=function(o){var n=!1;o.startsWith("rgba(")?n=!0:o.startsWith("rgb(")&&(n=!1);var i=n?"rgba(":"rgb(",e=o.split(i)[1].split(")")[0].split(",");if(e.length<3||e.length>4)throw new Error(o+" is not a valid rgb(a) color");e=e.map(function(r){return parseFloat(r)}).map(function(r){return t(0,255,r)});var a=e,d=a[0],c=a[1],u=a[2],v=a[3],f=void 0===v?1:v,s=t(0,1,f),l=[d,c,u].map(r);return[].concat(l,[s])},n=function(r){var t=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(r);t=t.slice(1,4),t=t.map(function(r){return parseInt(r,16)});var o=t,n=o[0],i=o[1],e=o[2],a=1;return{r:n,g:i,b:e,a:a}},i=function(t){var o=/^#[0-9A-F]{6}$/i.test(t);if(!o)throw new Error(t+" is not a valid hex color");var i=n(t),e=i.r,a=i.g,d=i.b,c=[e,a,d];return[].concat(c.map(function(t){return r(t)}),[1])},e=function(r){if("string"==typeof r){if(r.startsWith("rgba"))return o(r);if(r.startsWith("rgb"))return o(r);if(r.startsWith("#"))return i(r);throw new Error(r+" is not a valid color, must be rgba, rgb, hex or vec4")}if(Array.isArray(r)){if(4!==r.length)throw new Error("["+r+"] is not a valid vec4");return r}throw new Error(r+" is not a valid color, must be rgba, rgb, hex or vec4")},a=function(r,t,o){return o<r?r:o>t?t:o},d=function(r){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=t.color,n=void 0===o||o,i=t.dx,d=void 0===i?0:i,c=t.dy,u=void 0===c?0:c,v=t.dz,f=void 0===v?0:v,s=t.dcolor,l=void 0===s?[0,0,0,1]:s,g=r.x,h=void 0===g?d:g,p=r.y,b=void 0===p?u:p,m=r.z,x=void 0===m?f:m;h=a(-1,1,h),b=a(-1,1,b),x=a(-1,1,x);var y=r.color,w=void 0===y?l:y;if(w&&n){var E=e(w);return[h,b,x].concat(E)}return[h,b,x]},c=function(r){return[].concat.apply([],r)},u=function(r){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=t.color,n=void 0===o||o,i=t.dx,e=void 0===i?0:i,a=t.dy,u=void 0===a?0:a,v=t.dz,f=void 0===v?0:v,s=t.dcolor,l=void 0===s?[0,0,0,1]:s,g=t.meta,h=void 0!==g&&g,p={color:n,dx:e,dy:u,dz:f,dcolor:l,meta:h},b=c(r.map(function(r){return d(r,p)}));if(h){var m={};return m.vertexLength=n?7:3,m.amount=b.length/m.vertexLength,m.position={start:0,length:3},n&&(m.color={start:3,length:4}),{data:b,meta:m}}return b};return u});