PKIEf:&77ColorInfoTip/package.kpf if (typeof(macros) == 'undefined') { var macros = {}; } if (typeof(macros.dafizilla) == 'undefined') { macros.dafizilla = {}; } macros.dafizilla.cssColorInfoTip = {}; (function() { var XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"; var box = document.createElementNS(XUL_NS, "box"); var reCSSColor = /(rgb\(.*?\))|(#[\dA-Fa-f]+)/g; var nsIDOMCSSPrimitiveValue = Components.interfaces.nsIDOMCSSPrimitiveValue; function adjustPositionByTabs(line, pos, tabWidth) { var currPos = 0; var adjustedPos = 0; for (var i = 0; i < line.length && currPos < pos; i++) { if (line.charAt(i) == "\t") { var spaces = (tabWidth - currPos % tabWidth); currPos += spaces; } else { ++currPos; } ++adjustedPos; } return adjustedPos; } function findColor(line, pos) { // reset regexpr reCSSColor.lastIndex = 0; var m = null; while ((m = reCSSColor.exec(line)) != null) { var start = m.index; var end = start + m[0].length; if (start <= pos && pos <= end) { return m[0]; } } return null; } function getRGBFromCSSColor(color) { if (color) { box.setAttribute("style", "background-color : " + color); var style = box.ownerDocument.defaultView.getComputedStyle(box, null); var value = style.getPropertyCSSValue("background-color"); if (value.primitiveType == nsIDOMCSSPrimitiveValue.CSS_RGBCOLOR) { var rgbColor = value.getRGBColorValue(); return { r : rgbColor.red.getFloatValue(nsIDOMCSSPrimitiveValue.CSS_NUMBER), g : rgbColor.green.getFloatValue(nsIDOMCSSPrimitiveValue.CSS_NUMBER), b : rgbColor.blue.getFloatValue(nsIDOMCSSPrimitiveValue.CSS_NUMBER) }; } } return null; } function onMouseMove(event) { var x = event.clientX < 0 ? 0 : event.clientX - this.boxObject.x; var y = event.clientY - this.boxObject.y; var scimoz = ko.views.manager.currentView.scimoz; var position = scimoz.positionFromPoint(x, y); var currLine = scimoz.lineFromPosition(position); var text = new Object(); scimoz.getLine(currLine, text); var line = text.value; var column = adjustPositionByTabs(line, scimoz.getColumn(position), scimoz.tabWidth); var cssColor = findColor(line, column); var rgb = getRGBFromCSSColor(cssColor); if (rgb != null) { // Scintilla uses BBGGRR instead of RRGGBB scimoz.callTipSetBack(rgb.r + rgb.g * 256 + rgb.b * 256 * 256); // remove calltip to ensure color is refreshed scimoz.callTipCancel(); scimoz.callTipShow(position, " \n\n"); } else { scimoz.callTipSetBack(scimoz.styleGetBack(scimoz.STYLE_DEFAULT)); scimoz.callTipCancel(); } } this.init = function() { var docLang = ko.views.manager.currentView.document.language; if (docLang == "HTML" || docLang == "CSS") { ko.views.manager.currentView.addEventListener("mousemove", onMouseMove, true); } } }).apply(macros.dafizilla.cssColorInfoTip); macros.dafizilla.cssColorInfoTip.init(); 1 PKIEf:&77ColorInfoTip/package.kpfPKFm