annotate.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. //main()
  2. function hex(x) {
  3. var hexDigits = new Array("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");
  4. return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
  5. }
  6. function rgb2hex(rgb) {
  7. console.log(rgb)
  8. rgb = rgb.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)/);
  9. return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
  10. }
  11. function getHex(style, prop) {
  12. const rgb = style.getPropertyValue(prop)
  13. return rgb2hex(rgb);
  14. }
  15. function main() {
  16. for (const el of document.getElementsByClassName("show")) {
  17. const lbl = document.createElement('strong')
  18. const p = document.createElement('p')
  19. p.classList.add('annotation')
  20. lbl.innerText = el.tagName + " "
  21. p.append(lbl)
  22. const style = window.getComputedStyle(el, null)
  23. const bgColor = document.createElement('span')
  24. bgColor.innerText = 'bg: ' + getHex(style, 'background-color') + " "
  25. p.appendChild(bgColor)
  26. const fgColor = document.createElement('span')
  27. fgColor.innerText = 'fg: ' + getHex(style, 'color')
  28. p.appendChild(fgColor)
  29. el.appendChild(p)
  30. }
  31. }