1 #include "colorpicker.qh"
5 entity makeXonoticColorpicker(entity theTextbox)
8 me = NEW(XonoticColorpicker);
9 me.configureXonoticColorpicker(me, theTextbox);
13 void XonoticColorpicker_configureXonoticColorpicker(entity me, entity theTextbox)
15 me.controlledTextbox = theTextbox;
16 me.configureImage(me, me.image);
19 METHOD(XonoticColorpicker, mousePress, bool(XonoticColorpicker this, vector pos))
21 this.mouseDrag(this, pos);
25 // must match hslimage.c
26 vector hslimage_color(vector v, vector margin)
28 v_x = (v.x - margin.x) / (1 - 2 * margin.x);
29 v_y = (v.y - margin.y) / (1 - 2 * margin.y);
34 if(v.y > 0.875) // grey bar
35 return hsl_to_rgb(eZ * v.x);
37 return hsl_to_rgb(v.x * 6 * eX + eY + v.y / 0.875 * eZ);
40 vector color_hslimage(vector v, vector margin)
54 pos_x = margin.x + pos.x * (1 - 2 * margin.x);
55 pos_y = margin.y + pos.y * (1 - 2 * margin.y);
59 float XonoticColorpicker_mouseDrag(entity me, vector coords)
64 i = me.controlledTextbox.cursorPos;
66 int res = checkColorCode(me.controlledTextbox.text, i);
69 int tag_length = floor(res / 10);
71 for (int j = tag_length - ofs; j > 0; j--)
72 me.controlledTextbox.keyDown(me.controlledTextbox, K_RIGHTARROW, 8, 0);
73 for (int j = tag_length; j > 0; j--)
74 me.controlledTextbox.keyDown(me.controlledTextbox, K_BACKSPACE, 8, 0);
81 if(substring(me.controlledTextbox.text, i-1, 1) == "^")
83 if(!isCaretEscaped(me.controlledTextbox.text, i-1))
84 me.controlledTextbox.enterText(me.controlledTextbox, "^"); // escape previous caret
88 margin = me.imagemargin;
89 if(coords.x >= margin.x)
90 if(coords.y >= margin.y)
91 if(coords.x <= 1 - margin.x)
92 if(coords.y <= 1 - margin.y)
93 me.controlledTextbox.enterText(me.controlledTextbox, rgb_to_hexcolor(hslimage_color(coords, margin)));
98 float XonoticColorpicker_mouseRelease(entity me, vector coords)
100 m_play_click_sound(MENU_SOUND_SLIDE);
101 me.mouseDrag(me, coords);
105 void XonoticColorpicker_focusLeave(entity me)
107 me.controlledTextbox.saveCvars(me.controlledTextbox);
109 float XonoticColorpicker_keyDown(entity me, float key, float ascii, float shift)
111 return me.controlledTextbox.keyDown(me.controlledTextbox, key, ascii, shift);
113 void XonoticColorpicker_draw(entity me)
115 SUPER(XonoticColorpicker).draw(me);
118 C = cvar("r_textcontrast");
119 B = cvar("r_textbrightness");
121 // for this to work, C/(1-B) must be in 0..1
126 C = bound(0, C, 1-B);
128 aC = 1 - C / (1 - B);
130 draw_Picture(me.imgOrigin, strcat(me.src, "_m"), me.imgSize, '0 0 0', aC);
131 draw_Picture(me.imgOrigin, strcat(me.src, "_m"), me.imgSize, me.color, B);