This repository has been archived by the owner on Jan 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
123 lines (97 loc) · 3.85 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
#include <gtk/gtk.h>
#include <string>
#include <sstream>
#include <iomanip>
#include "modules/CssManager.h"
#include "hex_table.hex"
using namespace std;
const string homeDir = getenv("HOME");
// HEX opacity table
const gchar* alpha_to_hex(gdouble alpha) {
guint alpha_hex = (guint)(alpha * 127 + 0.5);
if (alpha_hex >= 0 && alpha_hex < 128) {
return alpha_hex_table[alpha_hex];
} else {
return NULL;
}
}
// Color channel to HEX
string channel_to_hex(gdouble channel) {
guint channel_hex = (guint)(channel * 255);
std::stringstream stream;
stream << uppercase << setfill('0') << setw(2) << hex << channel_hex;
return stream.str();
}
// RGBA to HEX with opacity
string color_to_hex(const GdkRGBA *color) {
string hex_str;
const gchar* alpha_hex = alpha_to_hex(color->alpha);
if (alpha_hex != NULL) {
// Color to HEX with opacity
hex_str = "0x" + string(alpha_hex) + channel_to_hex(color->red) +
channel_to_hex(color->green) + channel_to_hex(color->blue);
} else {
// If transparency is outside the acceptable range, set it to 100%
alpha_hex = "FF";
hex_str = "0x" + string(alpha_hex) + channel_to_hex(color->red) +
channel_to_hex(color->green) + channel_to_hex(color->blue);
}
return hex_str;
}
void color_dialog(GtkWidget *button, gpointer user_data) {
GtkWidget *dialog;
gint result;
GdkRGBA color;
GtkWidget *color_field = (GtkWidget*)user_data; // User color input
dialog = gtk_color_chooser_dialog_new("Select Color", NULL);
result = gtk_dialog_run(GTK_DIALOG(dialog));
if (result == GTK_RESPONSE_OK) {
gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(dialog), &color);
// Color to HEX with opacity
string hex_color = color_to_hex(&color);
// Update label "color_field"
gtk_label_set_text(GTK_LABEL(color_field), hex_color.c_str());
}
gtk_widget_destroy(dialog);
}
static void activate(GtkApplication* app, gpointer user_data) {
GtkWidget *window;
GtkWidget *box;
GtkWidget *button;
GtkWidget *color_field;
// Vertical box
box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
// Setup window
window = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(window), "Color-Converter");
gtk_window_set_default_size(GTK_WINDOW(window), 400, 215);
gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
// Label with user color in format: "0x+opacity+hex"
color_field = gtk_label_new("Color will be displayed here");
gtk_widget_set_size_request(color_field, 400, 100);
gtk_label_set_selectable(GTK_LABEL(color_field), TRUE);
// Color chooser button
button = gtk_button_new_with_label("Open Color Chooser");
gtk_widget_set_size_request(button, 400, 100);
g_signal_connect(button, "clicked", G_CALLBACK(color_dialog), color_field);
// container setup
gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 0); // Добавляем кнопку в контейнер без расширения и выравнивания
gtk_box_pack_start(GTK_BOX(box), color_field, FALSE, FALSE, 0); // Добавляем метку в контейнер без расширения и выравнивания
// container add to window
gtk_container_add(GTK_CONTAINER(window), box);
//CSS
CssManager css;
string path = string(homeDir) + "/.config/colorconvert/style.css";
css.loadFromFile(path.c_str());
gtk_widget_show_all(window);
}
int main(int argc, char **argv) {
GtkApplication *app;
int status;
app = gtk_application_new("color.converter.app", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return status;
}