瀏覽代碼

added color options

naelstrof 11 年之前
父節點
當前提交
eef0c29838
共有 5 個檔案被更改,包括 71 行新增10 行删除
  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 查看文件

@@ -17,6 +17,9 @@ int main( int argc, char** argv ) {
17 17
     int padding = options->m_padding;
18 18
     int borderSize = options->m_borderSize;
19 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 24
     // First we set up the x interface and grab the mouse,
22 25
     // if we fail for either we exit immediately.
@@ -72,7 +75,8 @@ int main( int argc, char** argv ) {
72 75
                                                          t.m_y - t.m_border,
73 76
                                                          t.m_width + t.m_border,
74 77
                                                          t.m_height + t.m_border,
75
-                                                         borderSize, padding );
78
+                                                         borderSize, padding,
79
+                                                         r, g, b );
76 80
                     xengine->addRect( windowselection );
77 81
                     window = xengine->m_hoverXWindow;
78 82
                 }
@@ -89,7 +93,7 @@ int main( int argc, char** argv ) {
89 93
             case 1: {
90 94
                 // Simply create a new rectangle at the mouse position and move on
91 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 97
                 xengine->addRect( selection );
94 98
                 state++;
95 99
                 break;

+ 33
- 1
options.cpp 查看文件

@@ -7,6 +7,9 @@ slrn::Options::Options() {
7 7
     m_padding = 0;
8 8
     m_xdisplay = ":0";
9 9
     m_tolerance = 4;
10
+    m_red = 0;
11
+    m_green = 0;
12
+    m_blue = 0;
10 13
 }
11 14
 
12 15
 void slrn::Options::printHelp() {
@@ -19,8 +22,9 @@ void slrn::Options::printHelp() {
19 22
     printf( "    -p=INT, --m_padding=INT        set m_padding size for selection.\n" );
20 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 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 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 30
 int slrn::Options::parseOptions( int argc, char** argv ) {
@@ -41,6 +45,11 @@ int slrn::Options::parseOptions( int argc, char** argv ) {
41 45
             if ( err ) {
42 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 53
         } else if ( matches( arg, "-t=", "--tolerance=" ) ) {
45 54
             int err = parseInt( arg, &m_tolerance );
46 55
             if ( err ) {
@@ -122,3 +131,26 @@ int slrn::Options::parseString( std::string arg, std::string* returnString ) {
122 131
     delete[] y;
123 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 查看文件

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

+ 25
- 4
x.cpp 查看文件

@@ -212,11 +212,12 @@ slrn::Rectangle::~Rectangle() {
212 212
     if ( m_window == None ) {
213 213
         return;
214 214
     }
215
+    //XFreeColors( xengine->m_display, xengine->m_colormap, m_color.pixel, 1,
215 216
     XUnmapWindow( xengine->m_display, m_window );
216 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 221
     m_xoffset = 0;
221 222
     m_yoffset = 0;
222 223
     m_x = x;
@@ -232,11 +233,14 @@ slrn::Rectangle::Rectangle( int x, int y, int width, int height, int border, int
232 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 241
     XSetWindowAttributes attributes;
238 242
     attributes.background_pixmap = None;
239
-    attributes.background_pixel = m_forground.pixel;
243
+    attributes.background_pixel = m_color.pixel;
240 244
     attributes.save_under = True;
241 245
     attributes.override_redirect = True;
242 246
     attributes.colormap = xengine->m_colormap;
@@ -369,3 +373,20 @@ void slrn::Rectangle::constrain( int w, int h ) {
369 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 查看文件

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