Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include "SmartArray.h"
- #ifndef NULL
- #define NULL 0
- #endif
- #include <iostream>
- #include <gtkmm.h>
- #define cairo_color(a) ((a)/(static_cast<double>(0xFF)))
- using std::cout;
- using std::endl;
- typedef unsigned char byte;
- class Color:public SmartArray<Color>::Reference::SmartObject
- {
- private:
- struct
- {
- unsigned int dummy: 1;
- unsigned int red: 5;
- unsigned int green: 5;
- unsigned int blue: 5;
- } _RGB;
- byte round(byte b)
- {
- double res = b / 8.0;
- return static_cast<byte>((fmod(b,1.0)>0.5)? ((b>=31)?floor(res):floor(res)+1):floor(res));
- }
- protected:
- byte GetRed() const{ return _RGB.red * 8; }
- byte GetGreen() const{ return _RGB.green * 8; }
- byte GetBlue() const{ return _RGB.blue * 8; }
- byte SetRed(byte r){
- return _RGB.red = round(r);
- }
- byte SetGreen(byte g){
- return _RGB.green = round(g);
- }
- byte SetBlue(byte b){
- return _RGB.blue = round(b);
- }
- public:
- Color()
- {
- SetRed(0);
- SetGreen(0);
- SetBlue(0);
- }
- SmartObject_Type_Information("Palette")
- byte Red(){ return this->GetRed(); }
- byte Green(){ return this->GetGreen(); }
- byte Blue(){ return this->GetBlue(); }
- byte Red(byte r){ return this->SetRed(r); }
- byte Green(byte g){ return this->SetGreen(g); }
- byte Blue(byte b){ return this->SetBlue(b); }
- Color& operator = ( unsigned int RGB_ )
- {
- Red( (RGB_>>16)&0xFF );
- Green( (RGB_>>8)&0xFF );
- Blue( RGB_&0xFF );
- return *this;
- }
- Color& operator = (unsigned short v)
- {
- SetRed((v&0x3C00)>>10);
- SetGreen((v&0x03E0)>>5);
- SetBlue(v&0x001F);
- return *this;
- }
- Color& operator = ( Color& c)
- {
- Red(c.Red());
- Green(c.Green());
- Blue(c.Blue());
- return *this;
- }
- operator int()
- {
- return ((Red()<<16)|Green()<<8|Blue());
- }
- };
- class Palette:public SmartArray<Palette>::Reference::SmartObject, public Gtk::DrawingArea,protected SmartArray<Color>
- {
- private:
- SmartString name;
- public:
- SmartObject_Type_Information("Palette")
- Color& operator [] ( int i)
- {
- return SmartArray<Color>::operator[](i);
- }
- Palette& operator = ( Palette& pal )
- {
- for( unsigned int i=0; i<16; i++)
- {
- operator[](i) = pal[i];
- }
- return *this;
- }
- virtual void ExportToFile(std::ostream& o ) {}
- virtual void LoadFromFile(std::istream& i ) {}
- Palette(const char* _name):SmartArray<Color>(16),name(_name)
- {
- for(unsigned int i=0; i<16; i++)
- Append(new Color);
- }
- const SmartString& Name() const
- {
- return name;
- }
- };
- class Palette_Renderer:public Gtk::DrawingArea, public SmartArray<Palette>::Reference
- {
- protected:
- Gtk::ColorSelectionDialog mCSD;
- Color& prepare(int id, Cairo::RefPtr<Cairo::Context>& context )
- {
- Color& col = (*this)()[id];
- context->set_source_rgb(cairo_color(col.Red()),cairo_color(col.Green()),cairo_color(col.Blue()));
- return col;
- }
- virtual bool on_draw(GdkEventExpose *event)
- {
- Cairo::RefPtr<Cairo::Context> context = ( get_window()->create_cairo_context() );
- Gtk::Allocation allocation = get_allocation();
- const int width = allocation.get_width();
- const int height = allocation.get_height();
- const int paddingW=2,paddingH=2;
- const int colW = (width) / 4;//4 = padding.
- const int colH = (height) / 4;
- try
- {
- Palette& l_pal = (*this)();
- for(unsigned int y=0,color=0;y<4;y++)
- for( unsigned int x=0;x<4;x++,color++)
- {
- prepare((y*4)+x,context);
- context->rectangle(x*colW+paddingW,y*colH+paddingH,colW-2*paddingW,colH-2*paddingH);
- context->fill();
- }
- }
- catch( SmartArray<Palette>::Reference::NullReference )
- {
- context->set_source_rgba(0,0,0,.6);
- context->rectangle(0,0,width,height);
- context->fill();
- }
- return true;
- }
- virtual bool on_click(GdkEventButton *event)
- {
- try
- {
- Palette& l_pal = (*this)();
- Gtk::ColorSelection &CS = *mCSD.get_color_selection();
- Gtk::Allocation allocation = get_allocation();
- const int width = allocation.get_width();
- const int height = allocation.get_height();
- const int colW = (width) / 4;//4 = padding.
- const int colH = (height) / 4;
- Color& mc = l_pal[static_cast<unsigned int>(floor(event->y/colH)) * 4 + static_cast<unsigned int>(floor(event->x/colW))];
- Gdk::Color l_gdkcolor;
- l_gdkcolor.set_red(mc.Red()<<8);
- l_gdkcolor.set_green(mc.Green()<<8);
- l_gdkcolor.set_blue(mc.Blue()<<8);
- CS.set_current_color(l_gdkcolor);
- int response = mCSD.run();
- if( response == Gtk::RESPONSE_OK || response == Gtk::RESPONSE_ACCEPT ) {
- l_gdkcolor = CS.get_current_color();
- mc.Red( l_gdkcolor.get_red() >> 8 );
- mc.Green( l_gdkcolor.get_green() >> 8 );
- mc.Blue( l_gdkcolor.get_blue() >> 8 );
- mCSD.hide();
- on_draw(NULL);
- } else mCSD.hide();
- }
- catch( SmartArray<Palette>::Reference::NullReference ) {}
- return true;
- }
- public:
- Palette_Renderer():mCSD("Choose a new color.")
- {
- add_events( Gdk::BUTTON_PRESS_MASK );
- set_size_request((16*20)+(2*2),(16*20)+(2*2));
- signal_expose_event().connect( sigc::mem_fun(*this, &Palette_Renderer::on_draw));
- signal_button_press_event().connect( sigc::mem_fun(*this,&Palette_Renderer::on_click));
- }
- Palette_Renderer& operator = ( Palette* nPal )
- {
- SmartArray<Palette>::Reference::operator=(nPal);
- on_draw(NULL);
- return *this;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement