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
/** * Formats the selected color code by setting it as the background color * and choosing a contrasting color (black or white) as the foreground. * Accepts RGB values formatted `rgb(r, g, b)` or HEX codes prepended either by `\#`, by `#`, or by nothing at all. */functiongetContrastYIQ(colorInput){letr=0,g=0,b=0;constoriginalInput=colorInput;colorInput=colorInput.replace(/#|\\#/g,"");// Normalize color code by removing # or \#if(/^([0-9A-F]{3}){1,2}$/i.test(colorInput)){// HEX formatif(colorInput.length==3){r=parseInt(colorInput[0]+colorInput[0],16);g=parseInt(colorInput[1]+colorInput[1],16);b=parseInt(colorInput[2]+colorInput[2],16);}else{r=parseInt(colorInput.substr(0,2),16);g=parseInt(colorInput.substr(2,2),16);b=parseInt(colorInput.substr(4,2),16);}}elseif(/^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/i.test(colorInput)){// RGB format[r,g,b]=colorInput.match(/\d+/g).map(Number);}constyiq=((r*299)+(g*587)+(b*114))/1000;return(yiq>=128) ? 'black' : 'white';}constselectedText=this.app.workspace.activeLeaf.view.editor.getSelection();letformattedText="";letvalidColor=/^(#|\\#)?[0-9A-F]{3}([0-9A-F]{3})?$/i.test(selectedText)||/^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/i.test(selectedText);if(validColor){constcontrastColor=getContrastYIQ(selectedText);if(selectedText.startsWith('rgb')){formattedText=`<span style="background-color:${selectedText}; color:${contrastColor}; padding: 2px;">${selectedText}</span>`;}else{consthexColor=selectedText.replace(/#|\\#/g,"").toUpperCase();formattedText=`<span style="background-color:#${hexColor}; color:${contrastColor}; padding: 2px;">\\#${hexColor}</span>`;}this.app.workspace.activeLeaf.view.editor.replaceSelection(formattedText);}else{this.app.workspace.activeLeaf.view.editor.replaceSelection("Invalid color format.");}