File indexing completed on 2024-12-29 05:00:16

0001 /*
0002  *  SPDX-FileCopyrightText: 2015 Kai Uwe Broulik <kde@privat.broulik.de>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 .pragma library
0008 
0009 var copyToClipboardText = "Copy to Clipboard";
0010 
0011 var formats = [
0012     "RRR,GGG,BBB",
0013     "#rrggbb",
0014     "rrggbb",
0015     "#RRGGBB",
0016     "RRGGBB",
0017     "rgb",
0018     "rgba",
0019     "Qt.rgba",
0020     "LaTeX"
0021 ]
0022 
0023 function formatColor(color, format) {
0024     var hexR = padHex((Math.round(color.r * 255)).toString(16))
0025     var hexG = padHex((Math.round(color.g * 255)).toString(16))
0026     var hexB = padHex((Math.round(color.b * 255)).toString(16))
0027 
0028     switch (format) {
0029     case "RRR,GGG,BBB":
0030         return [Math.round(color.r * 255), Math.round(color.g * 255), Math.round(color.b * 255)].join(", ")
0031     case "#rrggbb":
0032         return "#" + formatColor(color, "rrggbb")
0033     case "rrggbb":
0034         return formatColor(color, "RRGGBB").toLowerCase()
0035     case "#RRGGBB":
0036         return "#" + formatColor(color, "RRGGBB")
0037     case "RRGGBB":
0038         return (hexR + hexG + hexB).toUpperCase()
0039     case "rgb":
0040         return "rgb(" + formatColor(color, "RRR,GGG,BBB") + ")"
0041     case "rgba":
0042         return "rgba(" + formatColor(color, "RRR,GGG,BBB") + ", 1)"
0043     case "Qt.rgba":
0044         return "Qt.rgba(" + [roundComponent(color.r), roundComponent(color.g), roundComponent(color.b)].join(", ") + ", 1)"
0045     case "LaTeX":
0046         return "\\definecolor{ColorName}{rgb}{" + [roundComponent(color.r), roundComponent(color.g), roundComponent(color.b)].join(",") + "}"
0047     }
0048 }
0049 
0050 function padHex(n) {
0051     return ("0" + n).substr(-2, 2) // fancy
0052 }
0053 
0054 function roundComponent(n) {
0055     return Math.round(n * 100) / 100
0056 }
0057 
0058 function menuForColor(color) {
0059     return [
0060         {text: copyToClipboardText, section: true},
0061         {text: formatColor(color, "RRR,GGG,BBB")},
0062         {text: formatColor(color, "rgb")},
0063         {text: formatColor(color, "rgba")},
0064         {separator: true},
0065         {text: formatColor(color, "#rrggbb")},
0066         {text: formatColor(color, "rrggbb")},
0067         {text: formatColor(color, "#RRGGBB")},
0068         {text: formatColor(color, "RRGGBB")},
0069         {separator: true},
0070         {text: formatColor(color, "Qt.rgba")},
0071         {text: formatColor(color, "LaTeX")}
0072     ]
0073 }
0074 
0075 function createContextMenu(visualParent, currentColor, picker, colorLabel, copyIndicatorLabel, colorLabelRestoreTimer) {
0076     const initialArgs = {
0077         model: menuForColor(currentColor),
0078         visualParent,
0079         picker,
0080         colorLabel,
0081         copyIndicatorLabel,
0082         colorLabelRestoreTimer,
0083     };
0084     const component = Qt.createComponent("ColorContextMenu.qml");
0085     const menu = component.createObject(visualParent, initialArgs);
0086     component.destroy();
0087     return menu;
0088 }
0089 
0090 function showLoadingIndicator(parent, urls) {
0091     if (parent.loadingIndicator === null) {
0092         const component = Qt.createComponent(Qt.resolvedUrl("LoadingIndicator.qml"));
0093         parent.loadingIndicator = component.createObject(parent, {
0094             "jobRemaining": urls.length,
0095         });
0096         component.destroy();
0097     } else {
0098         parent.loadingIndicator.jobRemaining += urls.length;
0099     }
0100 }