Created
August 16, 2025 07:05
-
-
Save quasar098/3dc90e456e485549817c855411ec3282 to your computer and use it in GitHub Desktop.
color-pick-ui.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| from sys import argv | |
| import pyperclip | |
| import gi | |
| gi.require_version('Gtk', '3.0') | |
| from gi.repository import Gtk, Gdk, GdkPixbuf | |
| color = " ".join(argv[1:]) | |
| color = color.lstrip('#') | |
| def hex2rgb(h): | |
| assert len(h) >= 3 | |
| if len(h) < 6: | |
| h = "".join([c*2 for c in h]) | |
| return tuple([int(h[i:i+2], 16) for i in range(0,len(h),2)]) | |
| def rgb2hex(t, hashtag_prefix=False): | |
| assert len(t) in (3, 4), len(v) | |
| total = '' | |
| for i in t: | |
| total += hex(i)[2:].rjust(2,'0') | |
| return "#"*hashtag_prefix + total | |
| color = eval(color) if all(c in '()0123456789,.- ' for c in color) and ',' in color else hex2rgb(color) | |
| # ok the part above was written by me and you can tell | |
| # luckily i dont have to go through gtk docs to figure out how to use gtk | |
| # i think it sped up my development for this since i wrote it in 30 min without knowing how to use gtk stuff | |
| class ButtonApp(Gtk.Window): | |
| def __init__(self): | |
| self.put_hashtag = True | |
| Gtk.Window.__init__(self, title="Stupid AI-Written Color Picker UI") | |
| self.set_default_size(600, 250) | |
| self.set_position(Gtk.WindowPosition.CENTER) | |
| self.set_keep_above(True) | |
| vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10) | |
| vbox.set_margin_top(20) | |
| vbox.set_margin_bottom(20) | |
| vbox.set_margin_start(20) | |
| vbox.set_margin_end(20) | |
| self.add(vbox) | |
| self.color_rect = Gtk.DrawingArea() | |
| self.color_rect.set_size_request(560, 90) | |
| self.color_rect.set_halign(Gtk.Align.START) | |
| self.color_rect.connect("draw", self.draw_rectangle) | |
| vbox.pack_start(self.color_rect, False, False, 0) | |
| self.button_a = Gtk.Button(label=f"Copy Hex ({rgb2hex(color, self.put_hashtag)})") | |
| self.button_a.set_halign(Gtk.Align.START) | |
| self.button_a.set_size_request(560, 60) | |
| self.button_a.connect("clicked", self.on_button_clicked, rgb2hex(color, self.put_hashtag)) | |
| vbox.pack_start(self.button_a, True, True, 0) | |
| self.button_b = Gtk.Button(label=f"Copy RGB {color}") | |
| self.button_b.set_halign(Gtk.Align.START) | |
| self.button_b.set_size_request(560, 60) | |
| self.button_b.connect("clicked", self.on_button_clicked, str(color)) | |
| vbox.pack_start(self.button_b, True, True, 0) | |
| def draw_rectangle(self, widget, cr): | |
| cr.set_source_rgb(color[0]/255.0, color[1]/255.0, color[2]/255.0) | |
| cr.rectangle(0, 0, widget.get_allocated_width(), widget.get_allocated_height()) | |
| cr.fill() | |
| def on_button_clicked(self, widget, copy_me): | |
| pyperclip.copy(copy_me) | |
| Gtk.main_quit() | |
| def main(): | |
| win = ButtonApp() | |
| win.connect("destroy", Gtk.main_quit) | |
| win.show_all() | |
| Gtk.main() | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment