Advertisement
Tyler_Elric

palette.h

Jan 24th, 2012
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.31 KB | None | 0 0
  1. #pragma once
  2. #include "SmartArray.h"
  3.  
  4. #ifndef NULL
  5. #define NULL 0
  6. #endif
  7. #include <iostream>
  8. #include <gtkmm.h>
  9. #define cairo_color(a) ((a)/(static_cast<double>(0xFF)))
  10. using std::cout;
  11. using std::endl;
  12. typedef unsigned char byte;
  13.  
  14.  
  15. class Color:public SmartArray<Color>::Reference::SmartObject
  16. {
  17.     private:
  18.         struct
  19.         {
  20.             unsigned int dummy: 1;
  21.             unsigned int red: 5;
  22.             unsigned int green: 5;
  23.             unsigned int blue: 5;
  24.         } _RGB;
  25.         byte round(byte b)
  26.         {
  27.             double res = b / 8.0;
  28.             return static_cast<byte>((fmod(b,1.0)>0.5)? ((b>=31)?floor(res):floor(res)+1):floor(res));
  29.         }
  30.     protected:
  31.         byte GetRed() const{ return _RGB.red * 8; }
  32.         byte GetGreen() const{ return _RGB.green * 8; }
  33.         byte GetBlue() const{ return _RGB.blue * 8; }
  34.         byte SetRed(byte r){
  35.             return _RGB.red = round(r);
  36.         }
  37.         byte SetGreen(byte g){
  38.             return _RGB.green = round(g);
  39.         }
  40.         byte SetBlue(byte b){
  41.             return _RGB.blue = round(b);
  42.         }
  43.     public:
  44.         Color()
  45.         {
  46.             SetRed(0);
  47.             SetGreen(0);
  48.             SetBlue(0);
  49.         }
  50.         SmartObject_Type_Information("Palette")
  51.         byte Red(){ return this->GetRed(); }
  52.         byte Green(){ return this->GetGreen(); }
  53.         byte Blue(){ return this->GetBlue(); }
  54.         byte Red(byte r){ return this->SetRed(r); }
  55.         byte Green(byte g){ return this->SetGreen(g); }
  56.         byte Blue(byte b){ return this->SetBlue(b); }
  57.         Color& operator = ( unsigned int RGB_ )
  58.         {
  59.             Red( (RGB_>>16)&0xFF );
  60.             Green( (RGB_>>8)&0xFF );
  61.             Blue( RGB_&0xFF );
  62.             return *this;
  63.         }
  64.         Color& operator = (unsigned short v)
  65.         {
  66.             SetRed((v&0x3C00)>>10);
  67.             SetGreen((v&0x03E0)>>5);
  68.             SetBlue(v&0x001F);
  69.             return *this;
  70.         }
  71.         Color& operator = ( Color& c)
  72.         {
  73.             Red(c.Red());
  74.             Green(c.Green());
  75.             Blue(c.Blue());
  76.             return *this;
  77.         }
  78.         operator int()
  79.         {
  80.             return ((Red()<<16)|Green()<<8|Blue());
  81.         }
  82. };
  83.  
  84. class Palette:public SmartArray<Palette>::Reference::SmartObject, public Gtk::DrawingArea,protected SmartArray<Color>
  85. {
  86.     private:
  87.         SmartString name;
  88.     public:
  89.         SmartObject_Type_Information("Palette")
  90.         Color& operator [] ( int i)
  91.         {
  92.             return SmartArray<Color>::operator[](i);
  93.         }
  94.         Palette& operator = ( Palette& pal )
  95.         {
  96.             for( unsigned int i=0; i<16; i++)
  97.             {
  98.                 operator[](i) = pal[i];
  99.             }
  100.             return *this;
  101.         }
  102.         virtual void ExportToFile(std::ostream& o ) {}
  103.         virtual void LoadFromFile(std::istream& i ) {}
  104.         Palette(const char* _name):SmartArray<Color>(16),name(_name)
  105.         {
  106.             for(unsigned int i=0; i<16; i++)
  107.                 Append(new Color);
  108.         }
  109.         const SmartString& Name() const
  110.         {
  111.             return name;
  112.         }
  113. };
  114.  
  115. class Palette_Renderer:public Gtk::DrawingArea, public SmartArray<Palette>::Reference
  116. {
  117. protected:
  118.     Gtk::ColorSelectionDialog mCSD;
  119.     Color& prepare(int id, Cairo::RefPtr<Cairo::Context>& context )
  120.     {
  121.         Color& col = (*this)()[id];
  122.         context->set_source_rgb(cairo_color(col.Red()),cairo_color(col.Green()),cairo_color(col.Blue()));
  123.         return col;
  124.     }
  125.     virtual bool on_draw(GdkEventExpose *event)
  126.     {
  127.         Cairo::RefPtr<Cairo::Context> context = ( get_window()->create_cairo_context() );
  128.         Gtk::Allocation allocation = get_allocation();
  129.         const int width = allocation.get_width();
  130.         const int height = allocation.get_height();
  131.         const int paddingW=2,paddingH=2;
  132.         const int colW = (width) / 4;//4 = padding.
  133.         const int colH = (height) / 4;
  134.         try
  135.         {
  136.             Palette& l_pal = (*this)();
  137.             for(unsigned int y=0,color=0;y<4;y++)
  138.                 for( unsigned int x=0;x<4;x++,color++)
  139.                 {
  140.                     prepare((y*4)+x,context);
  141.                     context->rectangle(x*colW+paddingW,y*colH+paddingH,colW-2*paddingW,colH-2*paddingH);
  142.                     context->fill();
  143.                 }
  144.            
  145.         }
  146.         catch( SmartArray<Palette>::Reference::NullReference )
  147.         {
  148.             context->set_source_rgba(0,0,0,.6);
  149.             context->rectangle(0,0,width,height);
  150.             context->fill();
  151.         }
  152.         return true;
  153.     }
  154.     virtual bool on_click(GdkEventButton *event)
  155.     {
  156.         try
  157.         {
  158.             Palette& l_pal = (*this)();
  159.             Gtk::ColorSelection &CS = *mCSD.get_color_selection();
  160.             Gtk::Allocation allocation = get_allocation();
  161.             const int width = allocation.get_width();
  162.             const int height = allocation.get_height();
  163.             const int colW = (width) / 4;//4 = padding.
  164.             const int colH = (height) / 4;
  165.             Color& mc = l_pal[static_cast<unsigned int>(floor(event->y/colH)) * 4 + static_cast<unsigned int>(floor(event->x/colW))];
  166.             Gdk::Color l_gdkcolor;
  167.             l_gdkcolor.set_red(mc.Red()<<8);
  168.             l_gdkcolor.set_green(mc.Green()<<8);
  169.             l_gdkcolor.set_blue(mc.Blue()<<8);
  170.             CS.set_current_color(l_gdkcolor);
  171.             int response = mCSD.run();
  172.             if( response == Gtk::RESPONSE_OK || response == Gtk::RESPONSE_ACCEPT ) {
  173.                 l_gdkcolor = CS.get_current_color();
  174.                 mc.Red( l_gdkcolor.get_red() >> 8 );
  175.                 mc.Green( l_gdkcolor.get_green() >> 8 );
  176.                 mc.Blue( l_gdkcolor.get_blue() >> 8 );
  177.                 mCSD.hide();
  178.                 on_draw(NULL);
  179.             } else  mCSD.hide();
  180.        
  181.         }
  182.         catch( SmartArray<Palette>::Reference::NullReference ) {}
  183.         return true;
  184.     }
  185. public:
  186.     Palette_Renderer():mCSD("Choose a new color.")
  187.     {
  188.         add_events( Gdk::BUTTON_PRESS_MASK );
  189.         set_size_request((16*20)+(2*2),(16*20)+(2*2));
  190.         signal_expose_event().connect( sigc::mem_fun(*this, &Palette_Renderer::on_draw));
  191.         signal_button_press_event().connect( sigc::mem_fun(*this,&Palette_Renderer::on_click));
  192.     }
  193.     Palette_Renderer& operator = ( Palette* nPal )
  194.     {
  195.         SmartArray<Palette>::Reference::operator=(nPal);
  196.         on_draw(NULL);
  197.         return *this;
  198.     }
  199. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement