2 CLASS(XonoticColorpicker) EXTENDS(Image)
3 METHOD(XonoticColorpicker, configureXonoticColorpicker, void(entity, entity))
4 METHOD(XonoticColorpicker, mousePress, float(entity, vector))
5 METHOD(XonoticColorpicker, mouseRelease, float(entity, vector))
6 METHOD(XonoticColorpicker, mouseDrag, float(entity, vector))
7 ATTRIB(XonoticColorpicker, controlledTextbox, entity, NULL)
8 ATTRIB(XonoticColorpicker, image, string, SKINGFX_COLORPICKER)
9 ATTRIB(XonoticColorpicker, imagemargin, vector, SKINMARGIN_COLORPICKER)
10 ATTRIB(XonoticColorpicker, focusable, float, 1)
11 METHOD(XonoticColorpicker, focusLeave, void(entity))
12 METHOD(XonoticColorpicker, keyDown, float(entity, float, float, float))
13 METHOD(XonoticColorpicker, draw, void(entity))
14 ENDCLASS(XonoticColorpicker)
15 entity makeXonoticColorpicker(entity theTextbox);
19 entity makeXonoticColorpicker(entity theTextbox)
22 me = spawnXonoticColorpicker();
23 me.configureXonoticColorpicker(me, theTextbox);
27 void XonoticColorpicker_configureXonoticColorpicker(entity me, entity theTextbox)
29 me.controlledTextbox = theTextbox;
30 me.configureImage(me, me.image);
33 float XonoticColorpicker_mousePress(entity me, vector coords)
35 me.mouseDrag(me, coords);
39 // must match hslimage.c
40 vector hslimage_color(vector v, vector margin)
42 v_x = (v_x - margin_x) / (1 - 2 * margin_x);
43 v_y = (v_y - margin_y) / (1 - 2 * margin_y);
48 if(v_y > 0.875) // grey bar
49 return hsl_to_rgb(eZ * v_x);
51 return hsl_to_rgb(v_x * 6 * eX + eY + v_y / 0.875 * eZ);
54 vector color_hslimage(vector v, vector margin)
68 pos_x = margin_x + pos_x * (1 - 2 * margin_x);
69 pos_y = margin_y + pos_y * (1 - 2 * margin_y);
73 float XonoticColorpicker_mouseDrag(entity me, vector coords)
78 i = me.controlledTextbox.cursorPos;
81 if(substring(me.controlledTextbox.text, i-2, 1) == "^")
82 if(strstrofs("0123456789", substring(me.controlledTextbox.text, i-1, 1), 0) >= 0)
84 me.controlledTextbox.keyDown(me.controlledTextbox, K_BACKSPACE, 8, 0);
85 me.controlledTextbox.keyDown(me.controlledTextbox, K_BACKSPACE, 8, 0);
92 if(substring(me.controlledTextbox.text, i-5, 2) == "^x")
93 if(strstrofs("0123456789abcdefABCDEF", substring(me.controlledTextbox.text, i-3, 1), 0) >= 0)
94 if(strstrofs("0123456789abcdefABCDEF", substring(me.controlledTextbox.text, i-2, 1), 0) >= 0)
95 if(strstrofs("0123456789abcdefABCDEF", substring(me.controlledTextbox.text, i-1, 1), 0) >= 0)
97 me.controlledTextbox.keyDown(me.controlledTextbox, K_BACKSPACE, 8, 0);
98 me.controlledTextbox.keyDown(me.controlledTextbox, K_BACKSPACE, 8, 0);
99 me.controlledTextbox.keyDown(me.controlledTextbox, K_BACKSPACE, 8, 0);
100 me.controlledTextbox.keyDown(me.controlledTextbox, K_BACKSPACE, 8, 0);
101 me.controlledTextbox.keyDown(me.controlledTextbox, K_BACKSPACE, 8, 0);
109 margin = me.imagemargin;
110 if(coords_x >= margin_x)
111 if(coords_y >= margin_y)
112 if(coords_x <= 1 - margin_x)
113 if(coords_y <= 1 - margin_y)
114 me.controlledTextbox.enterText(me.controlledTextbox, rgb_to_hexcolor(hslimage_color(coords, margin)));
119 float XonoticColorpicker_mouseRelease(entity me, vector coords)
121 me.mouseDrag(me, coords);
125 void XonoticColorpicker_focusLeave(entity me)
127 me.controlledTextbox.saveCvars(me.controlledTextbox);
129 float XonoticColorpicker_keyDown(entity me, float key, float ascii, float shift)
131 return me.controlledTextbox.keyDown(me.controlledTextbox, key, ascii, shift);
133 void XonoticColorpicker_draw(entity me)
135 SUPER(XonoticColorpicker).draw(me);
138 C = cvar("r_textcontrast");
139 B = cvar("r_textbrightness");
141 // for this to work, C/(1-B) must be in 0..1
146 C = bound(0, C, 1-B);
148 aC = 1 - C / (1 - B);
150 draw_Picture(me.imgOrigin, strcat(me.src, "_m"), me.imgSize, '0 0 0', aC);
151 draw_Picture(me.imgOrigin, strcat(me.src, "_m"), me.imgSize, me.color, B);