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;
65 string theText = me.controlledTextbox.text;
66 vector res = checkColorCode(theText, strlen(theText), i, true);
71 int new_pos = i - res.y;
72 theText = strcat(substring(theText, 0, new_pos), substring(theText, new_pos + cc_len, -1));
73 me.controlledTextbox.setText(me.controlledTextbox, theText);
74 me.controlledTextbox.cursorPos = new_pos;
77 if(substring(me.controlledTextbox.text, i-1, 1) == "^")
79 if(!isCaretEscaped(me.controlledTextbox.text, i-1))
80 me.controlledTextbox.enterText(me.controlledTextbox, "^"); // escape previous caret
84 margin = me.imagemargin;
85 if(coords.x >= margin.x)
86 if(coords.y >= margin.y)
87 if(coords.x <= 1 - margin.x)
88 if(coords.y <= 1 - margin.y)
89 me.controlledTextbox.enterText(me.controlledTextbox, rgb_to_hexcolor(hslimage_color(coords, margin)));
94 float XonoticColorpicker_mouseRelease(entity me, vector coords)
96 m_play_click_sound(MENU_SOUND_SLIDE);
97 me.mouseDrag(me, coords);
101 void XonoticColorpicker_focusLeave(entity me)
103 me.controlledTextbox.saveCvars(me.controlledTextbox);
105 float XonoticColorpicker_keyDown(entity me, float key, float ascii, float shift)
107 return me.controlledTextbox.keyDown(me.controlledTextbox, key, ascii, shift);
109 void XonoticColorpicker_draw(entity me)
111 SUPER(XonoticColorpicker).draw(me);
114 C = cvar("r_textcontrast");
115 B = cvar("r_textbrightness");
117 // for this to work, C/(1-B) must be in 0..1
122 C = bound(0, C, 1-B);
124 aC = 1 - C / (1 - B);
126 draw_Picture(me.imgOrigin, strcat(me.src, "_m"), me.imgSize, '0 0 0', aC);
127 draw_Picture(me.imgOrigin, strcat(me.src, "_m"), me.imgSize, me.color, B);