00001 #include "pgcolorselector.h"
00002
00003 PG_ColorSelector::PG_ColorBox::PG_ColorBox(PG_ColorSelector* parent, const PG_Rect& r) : PG_ThemeWidget(parent, r) {
00004
00005 SetBackground((SDL_Surface*)NULL);
00006 SetSimpleBackground(false);
00007
00008 my_btndown = false;
00009
00010 p.x = r.w/2;
00011 p.y = r.h/2;
00012 }
00013
00014 bool PG_ColorSelector::PG_ColorBox::eventMouseMotion(const SDL_MouseMotionEvent* motion) {
00015 if(!my_btndown) {
00016 return false;
00017 }
00018
00019 p = ScreenToClient(motion->x, motion->y);
00020
00021 if(p.x < 0) {
00022 p.x = 0;
00023 }
00024
00025 if(p.x >= my_width-1) {
00026 p.x = my_width-1;
00027 }
00028
00029 if(p.y < 0) {
00030 p.y = 0;
00031 }
00032
00033 if(p.y >= my_height-1) {
00034 p.y = my_height-1;
00035 }
00036
00037 Update();
00038 GetParent()->SetBaseColor(GetBaseColor());
00039 return true;
00040 }
00041
00042 bool PG_ColorSelector::PG_ColorBox::eventMouseButtonDown(const SDL_MouseButtonEvent* button) {
00043 if(my_btndown) {
00044 return false;
00045 }
00046
00047 SetCapture();
00048 my_btndown = true;
00049 p = ScreenToClient(button->x, button->y);
00050 Update();
00051 GetParent()->SetBaseColor(GetBaseColor());
00052 return true;
00053 }
00054
00055 bool PG_ColorSelector::PG_ColorBox::eventMouseButtonUp(const SDL_MouseButtonEvent* button) {
00056 if(!my_btndown) {
00057 return false;
00058 }
00059
00060 ReleaseCapture();
00061 my_btndown = false;
00062 return true;
00063 }
00064
00065 void PG_ColorSelector::PG_ColorBox::eventBlit(SDL_Surface* srf, const PG_Rect& src, const PG_Rect& dst) {
00066 PG_ThemeWidget::eventBlit(srf, src, dst);
00067
00068
00069 DrawHLine(0, p.y, w, PG_Color(255,255,255));
00070 DrawVLine(p.x, 0, h, PG_Color(255,255,255));
00071 }
00072
00073 PG_Color PG_ColorSelector::PG_ColorBox::GetBaseColor() {
00074 PG_Color result;
00075
00076 PG_Color cy1, cy2;
00077 PG_Color r,g,b,w;
00078 PG_Gradient gr = GetGradient();
00079
00080 r = gr.colors[0];
00081 g = gr.colors[1];
00082 b = gr.colors[2];
00083 w = gr.colors[3];
00084
00085
00086
00087 cy1.r = (Uint8)((float)r.r + (((float)b.r - (float)r.r) / (float)my_height) * (float)p.y);
00088 cy1.g = (Uint8)((float)r.g + (((float)b.g - (float)r.g) / (float)my_height) * (float)p.y);
00089 cy1.b = (Uint8)((float)r.b + (((float)b.b - (float)r.b) / (float)my_height) * (float)p.y);
00090
00091
00092
00093 cy2.r = (Uint8)((float)g.r + (((float)w.r - (float)g.r) / (float)my_height) * (float)p.y);
00094 cy2.g = (Uint8)((float)g.g + (((float)w.g - (float)g.g) / (float)my_height) * (float)p.y);
00095 cy2.b = (Uint8)((float)g.b + (((float)w.b - (float)g.b) / (float)my_height) * (float)p.y);
00096
00097
00098
00099 result.r = (Uint8)((float)cy1.r + (((float)cy2.r - (float)cy1.r) / (float)my_width) * (float)p.x);
00100 result.g = (Uint8)((float)cy1.g + (((float)cy2.g - (float)cy1.g) / (float)my_width) * (float)p.x);
00101 result.b = (Uint8)((float)cy1.b + (((float)cy2.b - (float)cy1.b) / (float)my_width) * (float)p.x);
00102
00103 return result;
00104 }
00105
00106 PG_ColorSelector::PG_ColorSelector(PG_Widget* parent, const PG_Rect&r, const std::string& style) : PG_ThemeWidget(parent, r, style) {
00107 PG_Gradient g;
00108
00109 my_color.r = 255;
00110 my_color.g = 255;
00111 my_color.b = 255;
00112
00113 my_colorbox = new PG_ColorBox(this, PG_Rect(5,5,r.h-10,r.h-10));
00114
00115 g.colors[0].r = 255;
00116 g.colors[0].g = 255;
00117 g.colors[0].b = 0;
00118
00119 g.colors[1].r = 0;
00120 g.colors[1].g = 255;
00121 g.colors[1].b = 255;
00122
00123 g.colors[2].r = 255;
00124 g.colors[2].g = 0;
00125 g.colors[2].b = 255;
00126
00127 g.colors[3].r = 255;
00128 g.colors[3].g = 255;
00129 g.colors[3].b = 255;
00130
00131 my_colorbox->SetGradient(g);
00132
00133 my_colorslider = new PG_Slider(this, PG_Rect(r.h, 5, 20, r.h-10), PG_ScrollBar::VERTICAL);
00134 my_colorslider->SetRange(0, 255);
00135
00136 my_colorslider->SetBackground((SDL_Surface*)NULL);
00137 my_colorslider->SetSimpleBackground(false);
00138 my_colorslider->sigSlide.connect(slot(*this, &PG_ColorSelector::handle_colorslide));
00139
00140 my_colorresult = new PG_ThemeWidget(this, PG_Rect(r.h+20,r.h/2,r.w - ((r.h+20)+5), r.h - (5+r.h/2)));
00141 my_colorresult->SetSimpleBackground(true);
00142
00143 SetBaseColor(my_colorbox->GetBaseColor());
00144 }
00145
00146 PG_ColorSelector::~PG_ColorSelector() {
00147 }
00148
00149 void PG_ColorSelector::SetColor(const PG_Color& c) {
00150 my_color = c;
00151 }
00152
00153 void PG_ColorSelector::SetBaseColor(const PG_Color& c) {
00154 PG_Gradient g;
00155 my_basecolor = c;
00156
00157 g.colors[0].r = my_basecolor.r;
00158 g.colors[0].g = my_basecolor.g;
00159 g.colors[0].b = my_basecolor.b;
00160
00161 g.colors[1].r = my_basecolor.r;
00162 g.colors[1].g = my_basecolor.g;
00163 g.colors[1].b = my_basecolor.b;
00164
00165 g.colors[2].r = 0;
00166 g.colors[2].g = 0;
00167 g.colors[2].b = 0;
00168
00169 g.colors[3].r = 0;
00170 g.colors[3].g = 0;
00171 g.colors[3].b = 0;
00172
00173 my_colorslider->SetGradient(g);
00174
00175 float v = my_colorslider->GetPosition();
00176
00177 PG_Color r;
00178 r.r = (Uint8)(((float)c.r / 255.0) * (255.0 - v));
00179 r.g = (Uint8)(((float)c.g / 255.0) * (255.0 - v));
00180 r.b = (Uint8)(((float)c.b / 255.0) * (255.0 - v));
00181
00182 my_colorresult->SetBackgroundColor(r);
00183 my_colorresult->Update();
00184 }
00185
00186 bool PG_ColorSelector::handle_colorslide(long data) {
00187 SetBaseColor(my_colorbox->GetBaseColor());
00188 return true;
00189 }