Skip to content

Commit

Permalink
button texts are black or white
Browse files Browse the repository at this point in the history
  • Loading branch information
jabbalaci committed Nov 23, 2018
1 parent 7da2d67 commit bc61662
Show file tree
Hide file tree
Showing 3 changed files with 285 additions and 6 deletions.
263 changes: 263 additions & 0 deletions black_or_white.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
{
"comment": "this info is extracted from https://upload.wikimedia.org/wikipedia/commons/1/15/Xterm_256color_chart.svg",
"whites": [
0,
1,
2,
3,
4,
5,
6,
8,
9,
12,
13,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30,
31,
32,
33,
34,
35,
36,
37,
52,
53,
54,
55,
56,
57,
58,
59,
60,
61,
62,
63,
64,
65,
66,
67,
68,
69,
70,
88,
89,
90,
91,
92,
93,
94,
95,
96,
97,
98,
99,
100,
101,
102,
103,
104,
105,
124,
125,
126,
127,
128,
129,
130,
131,
132,
133,
134,
135,
136,
137,
138,
139,
140,
160,
161,
162,
163,
164,
165,
166,
167,
168,
169,
170,
171,
196,
197,
198,
199,
200,
201,
202,
203,
204,
232,
233,
234,
235,
236,
237,
238,
239,
240,
241,
242,
243,
244,
245,
246
],
"blacks": [
7,
10,
11,
14,
15,
38,
39,
40,
41,
42,
43,
44,
45,
46,
47,
48,
49,
50,
51,
71,
72,
73,
74,
75,
76,
77,
78,
79,
80,
81,
82,
83,
84,
85,
86,
87,
106,
107,
108,
109,
110,
111,
112,
113,
114,
115,
116,
117,
118,
119,
120,
121,
122,
123,
141,
142,
143,
144,
145,
146,
147,
148,
149,
150,
151,
152,
153,
154,
155,
156,
157,
158,
159,
172,
173,
174,
175,
176,
177,
178,
179,
180,
181,
182,
183,
184,
185,
186,
187,
188,
189,
190,
191,
192,
193,
194,
195,
205,
206,
207,
208,
209,
210,
211,
212,
213,
214,
215,
216,
217,
218,
219,
220,
221,
222,
223,
224,
225,
226,
227,
228,
229,
230,
231,
247,
248,
249,
250,
251,
252,
253,
254,
255
]
}
12 changes: 11 additions & 1 deletion helper.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import json
from typing import Set, Tuple

from PyQt5.QtGui import QClipboard
from PyQt5.QtWidgets import QApplication

Expand All @@ -12,9 +15,16 @@ def get_text_from_clipboard() -> str:
return str(cb.text())


def hex_to_rgb(hex_str):
def hex_to_rgb(hex_str: str) -> Tuple[int, int, int]:
assert(len(hex_str) == 6)
r = int(hex_str[:2], 16)
g = int(hex_str[2:4], 16)
b = int(hex_str[4:], 16)
return (r, g, b)


def get_white_color_codes() -> Set[int]:
fname = "black_or_white.json"
with open(fname) as f:
d = json.load(f)
return set(d['whites'])
16 changes: 11 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
"""

import sys
from typing import Set

from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QMessageBox
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox, QPushButton

import dist
import helper
import showMainGui
import dist


class Main(QMainWindow, showMainGui.Ui_MainWindow):
Expand All @@ -25,6 +28,7 @@ def __init__(self):
self.hexColor.returnPressed.connect(self.set_color)
self.okButton.clicked.connect(self.set_color)
self.pasteButton.clicked.connect(self.paste_text)
self.whites: Set[int] = helper.get_white_color_codes()

def paste_text(self):
self.hexColor.setText(helper.get_text_from_clipboard())
Expand All @@ -46,13 +50,15 @@ def set_color(self):
self.messageLabel.setText("")
(r, g, b) = helper.hex_to_rgb(hex_value)
closest = dist.find_closest_color((r, g, b))
xterm_number = closest['xterm_number']
# print(closest)
btn_text_color = "white" if int(xterm_number) in self.whites else "black"
result = closest['hex_str'] # includes the leading '#' sign
self.originalColorButton.setStyleSheet(f"background-color: #{hex_value}; border: none;");
self.originalColorButton.setStyleSheet(f"background-color: #{hex_value}; color: {btn_text_color}; border: none;");
self.originalColorButton.setText(f"#{hex_value}")
#
self.colorButton.setStyleSheet(f"background-color: {result}; border: none;");
self.colorButton.setText(closest['xterm_number'])
self.colorButton.setStyleSheet(f"background-color: {result}; color: {btn_text_color}; border: none;");
self.colorButton.setText(xterm_number)
text = ""
for k, v in closest.items():
text += (f"<b>{k}:</b> {v}<br>")
Expand Down

0 comments on commit bc61662

Please sign in to comment.