Browse Source

added color options

naelstrof 11 years ago
parent
commit
eef0c29838
5 changed files with 71 additions and 10 deletions
  1. 6
    2
      main.cpp
  2. 33
    1
      options.cpp
  3. 4
    0
      options.hpp
  4. 25
    4
      x.cpp
  5. 3
    3
      x.hpp

+ 6
- 2
main.cpp View File

17
     int padding = options->m_padding;
17
     int padding = options->m_padding;
18
     int borderSize = options->m_borderSize;
18
     int borderSize = options->m_borderSize;
19
     int tolerance = options->m_tolerance;
19
     int tolerance = options->m_tolerance;
20
+    float r = options->m_red;
21
+    float g = options->m_green;
22
+    float b = options->m_blue;
20
 
23
 
21
     // First we set up the x interface and grab the mouse,
24
     // First we set up the x interface and grab the mouse,
22
     // if we fail for either we exit immediately.
25
     // if we fail for either we exit immediately.
72
                                                          t.m_y - t.m_border,
75
                                                          t.m_y - t.m_border,
73
                                                          t.m_width + t.m_border,
76
                                                          t.m_width + t.m_border,
74
                                                          t.m_height + t.m_border,
77
                                                          t.m_height + t.m_border,
75
-                                                         borderSize, padding );
78
+                                                         borderSize, padding,
79
+                                                         r, g, b );
76
                     xengine->addRect( windowselection );
80
                     xengine->addRect( windowselection );
77
                     window = xengine->m_hoverXWindow;
81
                     window = xengine->m_hoverXWindow;
78
                 }
82
                 }
89
             case 1: {
93
             case 1: {
90
                 // Simply create a new rectangle at the mouse position and move on
94
                 // Simply create a new rectangle at the mouse position and move on
91
                 // to the next state.
95
                 // to the next state.
92
-                selection = new slrn::Rectangle( xengine->m_mousex, xengine->m_mousey, 0, 0, borderSize, padding );
96
+                selection = new slrn::Rectangle( xengine->m_mousex, xengine->m_mousey, 0, 0, borderSize, padding, r, g, b );
93
                 xengine->addRect( selection );
97
                 xengine->addRect( selection );
94
                 state++;
98
                 state++;
95
                 break;
99
                 break;

+ 33
- 1
options.cpp View File

7
     m_padding = 0;
7
     m_padding = 0;
8
     m_xdisplay = ":0";
8
     m_xdisplay = ":0";
9
     m_tolerance = 4;
9
     m_tolerance = 4;
10
+    m_red = 0;
11
+    m_green = 0;
12
+    m_blue = 0;
10
 }
13
 }
11
 
14
 
12
 void slrn::Options::printHelp() {
15
 void slrn::Options::printHelp() {
19
     printf( "    -p=INT, --m_padding=INT        set m_padding size for selection.\n" );
22
     printf( "    -p=INT, --m_padding=INT        set m_padding size for selection.\n" );
20
     printf( "    -t=INT, --tolerance=INT        if you have a shaky mouse, increasing this value will make slrn detect single clicks better. Rather than interpreting your shaky clicks as region selections.\n" );
23
     printf( "    -t=INT, --tolerance=INT        if you have a shaky mouse, increasing this value will make slrn detect single clicks better. Rather than interpreting your shaky clicks as region selections.\n" );
21
     printf( "    -x=STRING, --xdisplay=STRING   set x display (STRING must be hostname:number.screen_number format)\n" );
24
     printf( "    -x=STRING, --xdisplay=STRING   set x display (STRING must be hostname:number.screen_number format)\n" );
25
+    printf( "    -c=COLOR, --color=COLOR        set selection rectangle color, COLOR is in format FLOAT,FLOAT,FLOAT\n" );
22
     printf( "examples\n" );
26
     printf( "examples\n" );
23
-    printf( "    slrn -b=10 -x=:0 -p=-30 -t=4\n" );
27
+    printf( "    slrn -b=10 -x=:0 -p=-30 -t=4 -c=0.5,0.5,0.5\n" );
24
 }
28
 }
25
 
29
 
26
 int slrn::Options::parseOptions( int argc, char** argv ) {
30
 int slrn::Options::parseOptions( int argc, char** argv ) {
41
             if ( err ) {
45
             if ( err ) {
42
                 return 1;
46
                 return 1;
43
             }
47
             }
48
+        } else if ( matches( arg, "-c=", "--color=" ) ) {
49
+            int err = parseColor( arg, &m_red, &m_green, &m_blue );
50
+            if ( err ) {
51
+                return 1;
52
+            }
44
         } else if ( matches( arg, "-t=", "--tolerance=" ) ) {
53
         } else if ( matches( arg, "-t=", "--tolerance=" ) ) {
45
             int err = parseInt( arg, &m_tolerance );
54
             int err = parseInt( arg, &m_tolerance );
46
             if ( err ) {
55
             if ( err ) {
122
     delete[] y;
131
     delete[] y;
123
     return 0;
132
     return 0;
124
 }
133
 }
134
+
135
+int slrn::Options::parseColor( std::string arg, float* r, float* g, float* b ) {
136
+    std::string copy = arg;
137
+    int find = copy.find( "=" );
138
+    while( find != copy.npos ) {
139
+        copy.at( find ) = ' ';
140
+        find = copy.find( "," );
141
+    }
142
+
143
+    // Just in case we error out, grab the actual argument name into x.
144
+    char* x = new char[ arg.size() ];
145
+    int num = sscanf( copy.c_str(), "%s %f %f %f", x, r, g, b );
146
+    if ( num != 4 ) {
147
+        fprintf( stderr, "Error parsing command arguments near %s\n", arg.c_str() );
148
+        fprintf( stderr, "Usage: %s=COLOR\n", x );
149
+        fprintf( stderr, "Example: %s=0,0,0 or %s=0.7,0.2,1\n", x, x );
150
+        fprintf( stderr, "Try -h or --help for help.\n" );
151
+        delete[] x;
152
+        return 1;
153
+    }
154
+    delete[] x;
155
+    return 0;
156
+}

+ 4
- 0
options.hpp View File

14
     int         m_borderSize;
14
     int         m_borderSize;
15
     int         m_padding;
15
     int         m_padding;
16
     int         m_tolerance;
16
     int         m_tolerance;
17
+    float       m_red;
18
+    float       m_green;
19
+    float       m_blue;
17
     std::string m_xdisplay;
20
     std::string m_xdisplay;
18
 private:
21
 private:
19
     int         parseInt( std::string arg, int* returnInt );
22
     int         parseInt( std::string arg, int* returnInt );
20
     int         parseString( std::string arg, std::string* returnString );
23
     int         parseString( std::string arg, std::string* returnString );
24
+    int         parseColor( std::string arg, float* r, float* g, float* b );
21
     bool        matches( std::string arg, std::string shorthand, std::string longhand );
25
     bool        matches( std::string arg, std::string shorthand, std::string longhand );
22
 };
26
 };
23
 
27
 

+ 25
- 4
x.cpp View File

212
     if ( m_window == None ) {
212
     if ( m_window == None ) {
213
         return;
213
         return;
214
     }
214
     }
215
+    //XFreeColors( xengine->m_display, xengine->m_colormap, m_color.pixel, 1,
215
     XUnmapWindow( xengine->m_display, m_window );
216
     XUnmapWindow( xengine->m_display, m_window );
216
     XDestroyWindow( xengine->m_display, m_window );
217
     XDestroyWindow( xengine->m_display, m_window );
217
 }
218
 }
218
 
219
 
219
-slrn::Rectangle::Rectangle( int x, int y, int width, int height, int border, int padding ) {
220
+slrn::Rectangle::Rectangle( int x, int y, int width, int height, int border, int padding, float r, float g, float b ) {
220
     m_xoffset = 0;
221
     m_xoffset = 0;
221
     m_yoffset = 0;
222
     m_yoffset = 0;
222
     m_x = x;
223
     m_x = x;
232
         return;
233
         return;
233
     }
234
     }
234
 
235
 
235
-    XAllocNamedColor( xengine->m_display, xengine->m_colormap, "black", &m_forground, &m_forgroundExact );
236
-    XAllocNamedColor( xengine->m_display, xengine->m_colormap, "white", &m_background, &m_backgroundExact );
236
+    // This sets up m_color
237
+    int err = convertColor( r, g, b );
238
+    if ( err ) {
239
+        fprintf( stderr, "Couldn't allocate color of value %f,%f,%f!\n", r, g, b );
240
+    }
237
     XSetWindowAttributes attributes;
241
     XSetWindowAttributes attributes;
238
     attributes.background_pixmap = None;
242
     attributes.background_pixmap = None;
239
-    attributes.background_pixel = m_forground.pixel;
243
+    attributes.background_pixel = m_color.pixel;
240
     attributes.save_under = True;
244
     attributes.save_under = True;
241
     attributes.override_redirect = True;
245
     attributes.override_redirect = True;
242
     attributes.colormap = xengine->m_colormap;
246
     attributes.colormap = xengine->m_colormap;
369
         m_height = h + pad*2;
373
         m_height = h + pad*2;
370
     }
374
     }
371
 }
375
 }
376
+
377
+int slrn::Rectangle::convertColor( float r, float g, float b ) {
378
+    // Convert float colors to shorts.
379
+    short red   = short( floor( r * 65535.f ) );
380
+    short green = short( floor( g * 65535.f ) );
381
+    short blue  = short( floor( b * 65535.f ) );
382
+    XColor color;
383
+    color.red = red;
384
+    color.green = green;
385
+    color.blue = blue;
386
+    int err = XAllocColor( xengine->m_display, xengine->m_colormap, &color );
387
+    if ( err == BadColor ) {
388
+        return err;
389
+    }
390
+    m_color = color;
391
+    return 0;
392
+}

+ 3
- 3
x.hpp View File

37
 
37
 
38
 class Rectangle {
38
 class Rectangle {
39
 public:
39
 public:
40
-            Rectangle( int x, int y, int width, int height, int border, int padding );
40
+            Rectangle( int x, int y, int width, int height, int border, int padding, float r, float g, float b );
41
             ~Rectangle();
41
             ~Rectangle();
42
     void    setPos( int x, int y );
42
     void    setPos( int x, int y );
43
     void    setDim( int w, int h );
43
     void    setDim( int w, int h );
50
     int     m_height;
50
     int     m_height;
51
     int     m_border;
51
     int     m_border;
52
     int     m_padding;
52
     int     m_padding;
53
-    XColor  m_forground, m_forgroundExact;
54
-    XColor  m_background, m_backgroundExact;
53
+    XColor  m_color;
55
     bool    m_flippedx;
54
     bool    m_flippedx;
56
     bool    m_flippedy;
55
     bool    m_flippedy;
57
 private:
56
 private:
57
+    int     convertColor( float r, float g, float b );
58
     void    constrain( int w, int h );
58
     void    constrain( int w, int h );
59
 };
59
 };
60
 
60