You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/** * Converts the selected HEX or HSL value to RGB format. * Accepts hex codes with `#`, `\#`, or no prefix */constselectedText=this.app.workspace.activeLeaf.view.editor.getSelection();functionhexToRgb(hex){hex=hex.replace(/(\\#|#)/g,'');if(hex.length===3){hex=hex.split('').map(char=>char+char).join('');}constr=parseInt(hex.substring(0,2),16);constg=parseInt(hex.substring(2,4),16);constb=parseInt(hex.substring(4,6),16);return`rgb(${r}, ${g}, ${b})`;}functionhslToRgb(h,s,l){s=s/100;l=l/100;letc=(1-Math.abs(2*l-1))*s;letx=c*(1-Math.abs(((h/60)%2)-1));letm=l-c/2;letr=0,g=0,b=0;if(0<=h&&h<60){r=c;g=x;b=0;}elseif(60<=h&&h<120){r=x;g=c;b=0;}elseif(120<=h&&h<180){r=0;g=c;b=x;}elseif(180<=h&&h<240){r=0;g=x;b=c;}elseif(240<=h&&h<300){r=x;g=0;b=c;}elseif(300<=h&&h<360){r=c;g=0;b=x;}r=Math.round((r+m)*255);g=Math.round((g+m)*255);b=Math.round((b+m)*255);return`rgb(${r}, ${g}, ${b})`;}letoutput;if(/^(\\#|#)?[0-9A-F]{6}$/i.test(selectedText)){output=hexToRgb(selectedText);}elseif(/^hsl\((\d+),\s*([\d.]+)%,\s*([\d.]+)%\)$/i.test(selectedText)){const[h,s,l]=selectedText.match(/\d+/g);output=hslToRgb(h,s,l);}else{output="Invalid color format.";}this.app.workspace.activeLeaf.view.editor.replaceSelection(output);
Convert color code to HEX format
/** * Converts the selected RGB or HSL value to HEX format */constselectedText=this.app.workspace.activeLeaf.view.editor.getSelection();functionhslToRgb(h,s,l){s=s/100;l=l/100;letc=(1-Math.abs(2*l-1))*s;letx=c*(1-Math.abs(((h/60)%2)-1));letm=l-c/2;letr=0,g=0,b=0;if(0<=h&&h<60){r=c;g=x;b=0;}elseif(60<=h&&h<120){r=x;g=c;b=0;}elseif(120<=h&&h<180){r=0;g=c;b=x;}elseif(180<=h&&h<240){r=0;g=x;b=c;}elseif(240<=h&&h<300){r=x;g=0;b=c;}elseif(300<=h&&h<360){r=c;g=0;b=x;}r=Math.round((r+m)*255);g=Math.round((g+m)*255);b=Math.round((b+m)*255);return`rgb(${r}, ${g}, ${b})`;}functionrgbToHex(r,g,b){r=parseInt(r);g=parseInt(g);b=parseInt(b);return"\\#"+((1<<24)+(r<<16)+(g<<8)+b).toString(16).slice(1).toUpperCase();}letoutput;if(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/i.test(selectedText)){const[r,g,b]=selectedText.match(/\d+/g);output=rgbToHex(r,g,b);}elseif(/^hsl\((\d+),\s*([\d.]+)%,\s*([\d.]+)%\)$/i.test(selectedText)){const[h,s,l]=selectedText.match(/\d+/g);constrgb=hslToRgb(h,s,l);output=rgbToHex(rgb[0],rgb[1],rgb[2]);}elseif(/^(\\#|#)?[0-9A-F]{6}$/i.test(selectedText)){consthex=selectedText.replace(/(\\#|#)/g,"");output="\\#"+hex.toUpperCase();}else{output="Invalid color format.";}this.app.workspace.activeLeaf.view.editor.replaceSelection(output);
Convert color code to HSL format
/** * Converts selected RGB or HEX value to HSL format * Accepts hex codes with `#`, `\#`, or no prefix */constselectedText=this.app.workspace.activeLeaf.view.editor.getSelection();functionhexToRgb(hex){hex=hex.replace(/#|\\#/g,"");// Remove # or \# if presentletr=parseInt(hex.substring(0,2),16);letg=parseInt(hex.substring(2,4),16);letb=parseInt(hex.substring(4,6),16);return[r,g,b];}functionrgbToHsl(r,g,b){r/=255,g/=255,b/=255;letmax=Math.max(r,g,b),min=Math.min(r,g,b);leth,s,l=(max+min)/2;if(max===min){h=s=0;}else{letd=max-min;s=l>0.5 ? d/(2-max-min) : d/(max+min);switch(max){caser: h=(g-b)/d+(g<b ? 6 : 0);break;caseg: h=(b-r)/d+2;break;caseb: h=(r-g)/d+4;break;}h/=6;h*=360;}s*=100;l*=100;return`hsl(${Math.round(h)}, ${Math.round(s)}%, ${Math.round(l)}%)`;}letoutput;if(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/i.test(selectedText)){const[r,g,b]=selectedText.match(/\d+/g);output=rgbToHsl(r,g,b);}elseif(/^(\\#|#)?[0-9A-F]{6}$/i.test(selectedText)){constrgb=hexToRgb(selectedText);output=rgbToHsl(rgb[0],rgb[1],rgb[2]);}else{output="Invalid color format.";}this.app.workspace.activeLeaf.view.editor.replaceSelection(output);