Skip to content

Commit 4555b12

Browse files
committed
Label-color-picker: Update color settings
1 parent 41e7caf commit 4555b12

File tree

1 file changed

+42
-55
lines changed

1 file changed

+42
-55
lines changed

github-label-color-picker.user.js

+42-55
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ==UserScript==
22
// @name GitHub Label Color Picker
3-
// @version 1.0.7
3+
// @version 1.0.8
44
// @description A userscript that adds a color picker to the label color input
55
// @license MIT
66
// @author Rob Garrison
@@ -13,20 +13,20 @@
1313
// @grant GM_setValue
1414
// @grant GM_registerMenuCommand
1515
// @require https://greasyfork.org/scripts/23181-colorpicker/code/colorPicker.js?version=147862
16+
// @require https://greasyfork.org/scripts/398877-utils-js/code/utilsjs.js?version=952600
1617
// @icon https://github.githubassets.com/pinned-octocat.svg
1718
// @updateURL https://raw.githubusercontent.com/Mottie/GitHub-userscripts/master/github-label-color-picker.user.js
1819
// @downloadURL https://raw.githubusercontent.com/Mottie/GitHub-userscripts/master/github-label-color-picker.user.js
1920
// @supportURL https://github.com/Mottie/GitHub-userscripts/issues
2021
// ==/UserScript==
21-
/* global jsColorPicker */
22+
/* global jsColorPicker $ on */
2223
(() => {
2324
"use strict";
2425

25-
// GitHub-Dark changes "text-black" to #c0c0c0
2626
GM_addStyle(`
27-
div.cp-app { margin:0; z-index:10; }
27+
div.cp-app { margin:100px 0 0 -7px; z-index:10; }
2828
.js-new-label-color-icon { pointer-events:none; }
29-
.js-new-label-color-icon.text-black { color:#000 !important; }
29+
.js-new-label-color-icon.color-scale-black { color:#000 !important; }
3030
`);
3131

3232
function addPicker() {
@@ -35,7 +35,7 @@
3535
customBG: "#222",
3636
noAlpha: true,
3737
renderCallback: function(colors) {
38-
let input = this && this.input;
38+
const input = this && this.input;
3939
if (input) {
4040
updateSwatch(input, colors);
4141
}
@@ -45,31 +45,34 @@
4545
}
4646

4747
function updateSwatch(input, colors) {
48-
let background = "#" + colors.HEX;
49-
input.value = background;
50-
let textColor = calcContrast(colors.HEX);
48+
input.value = colors.HEX;
49+
const colorStyle = calcStyle(colors.rgb, colors.hsl);
50+
5151
// Update color swatch next to input
52-
let swatch = $(".js-new-label-color", input.closest("dd"));
53-
updateIcon(swatch, textColor);
54-
updateColors(swatch, background, textColor);
52+
const inputSwatch = $(".js-new-label-color", input.closest("dd"));
53+
inputSwatch.style = colorStyle;
54+
5555
// Update label preview
56-
swatch = $(
56+
const labelSwatch = $(
5757
".js-label-preview .IssueLabel--big",
58-
input.closest(".table-list-item")
58+
input.closest(".Box-row")
5959
);
60-
updateColors(swatch, background, textColor);
61-
}
62-
63-
function updateIcon(swatch, textColor) {
64-
let icon = $(".octicon", swatch);
65-
// !important set on these GitHub primer color definitions
66-
icon.classList.remove("text-white", "text-black");
67-
icon.classList.add("text-" + textColor);
60+
labelSwatch.style = colorStyle;
6861
}
6962

70-
function updateColors(el, background, color) {
71-
el.style.backgroundColor = background;
72-
el.style.color = color;
63+
function calcStyle(rgb, hsl) {
64+
// GitHub adds CSS variables to the wrapper
65+
// rgb is used as the foreground (text) color
66+
// hsl is used to calculate a color variant for the background
67+
const multiplier = { h: 360, s: 100, l: 100 };
68+
const fg = Object.entries(rgb).map(
69+
([c, v]) => `--label-${c}:${(v * 255).toFixed(0)}`
70+
);
71+
const bg = Object.entries(hsl).map(
72+
([c, v]) => `--label-${c}:${(v * multiplier[c]).toFixed(0)}`
73+
);
74+
// --label-r:255; --label-g:255; --label-b:255; --label-h:15; --label-s:0; --label-l:100;
75+
return `${fg.join("; ")}; ${bg.join("; ")}`;
7376
}
7477

7578
/* replace colorPicker storage */
@@ -84,53 +87,38 @@
8487
"'rgba(83,25,231,1)','rgba(86,66,66,1)','rgba(22,20,223,1)'"
8588
*/
8689
function convertColorsToRgba(values) {
87-
let result = [];
8890
// see http://stackoverflow.com/a/26196012/145346
89-
values
91+
return values
9092
.replace(/['"]/g, "")
9193
.split(/\s*,(?![^()]*(?:\([^()]*\))?\))\s*/g)
92-
.forEach(val => {
93-
let rgb = hexToRgb(val);
94+
.map(val => {
95+
const rgb = hexToRgb(val);
9496
if (rgb) {
95-
result.push(`'rgba(${rgb.r},${rgb.g},${rgb.b},1)'`);
97+
return `'rgba(${rgb.r},${rgb.g},${rgb.b},1)'`;
9698
} else if (rgb === null && val.indexOf("rgba(") > -1) {
9799
// allow adding rgba() definitions
98-
result.push(`'${val}'`);
100+
return`'${val}'`;
99101
}
100-
});
101-
return result.join(",");
102+
})
103+
.filter(Boolean)
104+
.join(",");
102105
}
103106

104107
// Modified code from http://stackoverflow.com/a/5624139/145346
108+
// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
109+
const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
105110
function hexToRgb(hex) {
106-
let result,
107-
// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
108-
shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
109-
hex = hex.replace(shorthandRegex, (m, r, g, b) => {
111+
const modHex = hex.replace(shorthandRegex, (_, r, g, b) => {
110112
return r + r + g + g + b + b;
111113
});
112-
result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
114+
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(modHex);
113115
return result ? {
114116
r: parseInt(result[1], 16),
115117
g: parseInt(result[2], 16),
116118
b: parseInt(result[3], 16)
117119
} : null;
118120
}
119121

120-
// Calculate contrasting text color for the given background color
121-
// https://24ways.org/2010/calculating-color-contrast/
122-
function calcContrast(hex) {
123-
const r = parseInt(hex.substr(0, 2), 16),
124-
g = parseInt(hex.substr(2, 2), 16),
125-
b = parseInt(hex.substr(4, 2), 16),
126-
yiq = ((r * 299) + (g * 587) + (b * 114)) / 1000;
127-
return yiq >= 128 ? "black" : "white";
128-
}
129-
130-
function $(selector, el) {
131-
return (el || document).querySelector(selector);
132-
}
133-
134122
// Add GM options
135123
GM_registerMenuCommand(
136124
"Set label ColorPicker swatches (8 HEX or RGBA Max)",
@@ -143,15 +131,14 @@
143131
}
144132
);
145133

146-
document.body.addEventListener("click", event => {
134+
on(document.body, "click", event => {
147135
// initialize if "Edit" or "New label" button clicked
148136
// because "Save changes" updates the entire item
149137
if (
150-
event.target && event.target.matches(".js-edit-label, .js-details-target")
138+
event.target?.matches(".js-edit-label, .js-details-target")
151139
) {
152140
addPicker();
153141
}
154142
});
155143
addPicker();
156-
157144
})();

0 commit comments

Comments
 (0)