summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/xforms/examples/colbrowser.pp
blob: 4646a502ed467c34bacd9911fb43f20e07280923 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
Program colbrowser;

uses xforms,strings;

Const  MAX_RGB = 3000;

var
  cl : PFL_FORM;
  rescol, dbobj, colbr, rs, gs, bs : PFL_OBJECT;
  dbname : string;
  infile : text;

{ the RGB data file does not have a standard location on unix. }
{ You may need to edit this }

const rgbfile = '/usr/lib/X11/rgb.txt';

type TRGBdb = record
       r, g, b : longint;
     end;

var
rgbdb : array [0..MAX_RGB] of TRGBdb;
numcol : longint;

procedure set_entry(i : longint);

var
    db : TRGBdb;

begin
  db := rgbdb[i-1];

    fl_freeze_form(cl);
    fl_mapcolor(FL_FREE_COL4+i, db.r, db.g, db.b);
    fl_mapcolor(FL_FREE_COL4, db.r, db.g, db.b);
    fl_set_slider_value(rs, db.r);
    fl_set_slider_value(gs, db.g);
    fl_set_slider_value(bs, db.b);
    fl_redraw_object(rescol);
    fl_unfreeze_form(cl);
end;

procedure br_cb(ob : PFL_OBJECT; q :longint);cdecl;

var r : longint;

begin
    r := fl_get_browser(ob);
    if (r <= 0) then exit;
    set_entry(r - 1);
end;

{ slow but straightforward }
function stripsp (s : string) : string;

var temp : string;
    i : longint;
begin
  temp:='';
  for i:=1 to length(s) do
    if pos(s[i],'0987654321')<>0 then temp:=temp+s[i];
  stripsp:=temp;
end;


function  read_entry(Var r,g,b : longint;var name : string) : longint;

var
    n : longint;
    buf,temp : string;
    code : word;


begin
    readln (infile,buf);
    if buf[1]='!' then exit(0);
    temp:=stripsp(copy(buf,1,4));delete(buf,1,4);
    val (temp,r,code);
    if code<>0 then exit(0);
    temp:=stripsp(copy(buf,1,4));delete(buf,1,4);
    val (temp,g,code);
    if code<>0 then exit(0);
    temp:=stripsp(copy(buf,1,4));delete(buf,1,4);
    val (temp,b,code);
    if code<>0 then exit(0);
    { strip leading spaces from name }
    while (buf[code+1]=' ') or (buf[code+1]=#9) do inc(code);
    if code<>0 then delete(buf,1,code);
    name:=buf+#0;
    read_entry:=1;
end;


function load_browser(fname : string) : longint;

var buf : string;
    r,g,b : Longint;
    rr,gg,bb : string[3];

begin
   assign (infile,fname);
{$i-}
  reset(infile);
{$i+}
  if ioresult<>0 then
    begin
      fname:=fname+#0;
        fl_show_alert('Load', @fname[1], 'Can''t open', 0);
        exit(0);
    end;

    fl_freeze_form(cl);
    numcol:=-1;
    while not eof(infile) do
      begin
      if read_entry(r, g, b, buf)<>0 then
        begin
        inc(numcol);
        rgbdb[numcol].r := r;
        rgbdb[numcol].g := g;
        rgbdb[numcol].b := b;
        str (r,rr); if length(rr)<3 then rr:=copy('   ',1,3-length(rr))+rr;
        str(g,gg);if length(gg)<3 then gg:=copy('   ',1,3-length(gg))+gg;
        str(b,bb);if length(bb)<3 then bb:=copy('   ',1,3-length(bb))+bb;
        buf:='('+rr+' '+gg+' '+bb+') '+buf;
        fl_addto_browser(colbr, @buf[1]);
        end;
      end;
    close(infile);
    fl_set_browser_topline(colbr, 1);
    fl_select_browser_line(colbr, 1);
    set_entry(0);
    fl_unfreeze_form(cl);
    load_browser:=1;
end;

function search_entry(r,g,b : Longint) : Longint;

var i, j, diffr, diffg, diffb,diff, mindiff : longint;

begin
    mindiff := 1 shl 25;
    J:=0;
    i:=0;
    for i:=0 to numcol do
      begin
       diffr := abs(r - rgbdb[i].r);
       diffg := abs(g - rgbdb[i].g);
       diffb := abs(b - rgbdb[i].b);
       diff := round((3.0 * diffr) +
               (5.9 * diffg) +
               (1.1 * diffb));
       if (mindiff > diff) then
         begin
         mindiff := diff;
         j := i;
         end;
      end;
    search_entry:= j;
end;

procedure search_rgb(ob : PFL_OBJECT; q : longint);cdecl;

var r, g, b, i,top : longint;

begin
    top  := fl_get_browser_topline(colbr);
    r := round(fl_get_slider_value(rs));
    g := round(fl_get_slider_value(gs));
    b := round(fl_get_slider_value(bs));

    fl_freeze_form(cl);
    fl_mapcolor(FL_FREE_COL4, r, g, b);
    fl_redraw_object(rescol);
    i := search_entry(r, g, b);
    { change topline only if necessary }
    if (i < top) or (i > (top+15)) then
       fl_set_browser_topline(colbr, i-8);
    fl_select_browser_line(colbr, i + 1);
    fl_unfreeze_form(cl);
end;

{ change database }
procedure db_cb(ob : PFL_OBJECT; q : longint);cdecl;

var p: pchar;
    buf : string;

begin
    p := fl_show_input('Enter New Database Name', @dbname[1]);
    buf:=strpas(p)+#0;
    if buf=dbname then exit;

    if (load_browser(buf)<>0) then
        dbname:=buf
    else
        fl_set_object_label(ob, @dbname[1]);
end;

procedure done_cb (ob : PFL_OBJECT; q :  longint);cdecl;
begin
    halt(0);
end;

procedure create_form_cl;
var
    obj : PFL_OBJECT;

begin
    if (cl<>nil) then exit;
    cl := fl_bgn_form(FL_NO_BOX, 330, 385);
    obj := fl_add_box(FL_UP_BOX, 0, 0, 330, 385, '');
    fl_set_object_color(obj, FL_INDIANRED, FL_COL1);
    obj := fl_add_box(FL_NO_BOX, 40, 10, 250, 30, 'Color Browser');
    fl_set_object_lcol(obj, FL_RED);
    fl_set_object_lsize(obj, FL_HUGE_SIZE);
    fl_set_object_lstyle(obj, FL_BOLD_STYLE + FL_SHADOW_STYLE);
    obj := fl_add_button(FL_NORMAL_BUTTON, 40, 50, 250, 25, '');
    dbobj := obj ;
    fl_set_object_boxtype(obj, FL_BORDER_BOX);
{    if fl_get_visual_depth()=1 then
      fl_set_object_color(obj, FL_WHITE,FL_INDIANRED)
    else
      fl_set_object_color(obj, FL_INDIANRED, FL_INDIANRED);
}
    fl_set_object_callback(obj, PFL_CALLBACKPTR(@db_cb), 0);

    obj:= fl_add_valslider(FL_VERT_FILL_SLIDER, 225, 130, 30, 200, '');
    rs := obj;
    fl_set_object_color(obj, FL_INDIANRED, FL_RED);
    fl_set_slider_bounds(obj, 0, 255);
    fl_set_slider_precision(obj, 0);
    fl_set_object_callback(obj, PFL_CALLBACKPTR(@search_rgb), 0);
    fl_set_slider_return(obj, 0);
    obj:= fl_add_valslider(FL_VERT_FILL_SLIDER, 255, 130, 30, 200, '');
    gs := obj ;
    fl_set_object_color(obj, FL_INDIANRED, FL_GREEN);
    fl_set_slider_bounds(obj, 0.0, 255.0);
    fl_set_slider_precision(obj, 0);
    fl_set_object_callback(obj, PFL_CALLBACKPTR(@search_rgb), 1);
    fl_set_slider_return(obj, 0);
    obj := fl_add_valslider(FL_VERT_FILL_SLIDER, 285, 130, 30, 200, '');
    bs := obj;
    fl_set_object_color(obj, FL_INDIANRED, FL_BLUE);
    fl_set_slider_bounds(obj, double(0.0), double(255.0));
    fl_set_slider_precision(obj, 0);
    fl_set_object_callback(obj, PFL_CALLBACKPTR(@search_rgb), 2);
    fl_set_slider_return(obj, 0);
    obj := fl_add_browser(FL_HOLD_BROWSER, 10, 90, 205, 240, '');
    colbr := obj ;
    fl_set_browser_fontstyle(obj, FL_FIXED_STYLE);
    fl_set_object_callback(obj, PFL_CALLBACKPTR(@br_cb), 0);

    obj := fl_add_button(FL_NORMAL_BUTTON, 135, 345, 80, 30, 'Done');
    fl_set_object_callback(obj, PFL_CALLBACKPTR(@done_cb), 0);
    obj := fl_add_box(FL_FLAT_BOX, 225, 90, 90, 35, '');
    rescol := obj;
    fl_set_object_color(obj, FL_FREE_COL4, FL_FREE_COL4);
    fl_set_object_boxtype(obj, FL_BORDER_BOX);

    fl_end_form();
    {fl_scale_form (cl, 1.1, 1.0);}
end;

begin
    fl_initialize(@argc, argv, 'FormDemo', nil, 0);
    cl:=nil;
    create_form_cl();
    dbname:= rgbfile+#0;
    if (load_browser(dbname)<>0) then
        fl_set_object_label(dbobj, @dbname[1])
    else
        fl_set_object_label(dbobj, 'None');

    fl_set_form_minsize(cl, cl^.w , cl^.h);
    fl_set_form_maxsize(cl, 2*cl^.w , 2*cl^.h);
    fl_show_form(cl, FL_PLACE_FREE, FL_TRANSIENT, 'RGB Browser');

    while (fl_do_forms()<>nil) do;
end.