SIMPOL Documentation

Finishing the Color Lab Program

Now that we have the basic program running and displaying the form, all that remains is to fill in the functions that are currently empty. One thing that we can do to minimize the amount of coding is to use a common piece of code for some of the functions, and call it from each of them. From looking at the code, it seems that the scrollbar event handling functions will probably be similar, as will the functions that handle the edit control events for the three color values. Everything will eventually call the adjustformcolorvals() function. Since that is the function that everything has in common (it is even being called during initialization), let's build that first.

Example 5.3. The Full Implementation of the adjustformcolorvals() Function
function adjustformcolorvals(wxform f, integer rgbval, \
                             string ignorescrollbar="")
  integer red, green, blue

  blue = rgbval / 0x10000
  green = (rgbval mod 0x10000) / 0x100
  red = ((rgbval mod 0x10000) mod 0x100)
  f!tbHexColorValue.settext(.tostr(rgbval, 16))
  f!tbDecColorValue.settext(.tostr(rgbval, 10))
  f!tbRed.settext(.tostr(red, 10))
  f!tbGreen.settext(.tostr(green, 10))
  f!tbBlue.settext(.tostr(blue, 10))
  f!sbRed.setbackgroundrgb(red)
  f!sbGreen.setbackgroundrgb(green * 0x100)
  f!sbBlue.setbackgroundrgb(blue * 0x10000)
  if ignorescrollbar != "red"
    f!sbRed.setscroll(position=red)
  end if
  if ignorescrollbar != "green"
    f!sbGreen.setscroll(position=green)
  end if
  if ignorescrollbar != "blue"
    f!sbBlue.setscroll(position=blue)
  end if
  f!rBorder.setrgb(rgb=rgbval)
end function
      

What this function does, is to take the final color value and then use it to set the value of all the other controls. While playing around with this, it was noticed that setting the position of a scrollbar that had caused the event resulted in a strange flicker, so an extra parameter was created that is ignored by the other functions, but which is passed by the code that handles the scrollbar events. That let's the function choose not to set the scroll position for the scrollbar that is passed. Other than that, the function is fairly basic. It takes the color value that comes as an RGB value and splits it into the red, green, and blue values. It then assigns the value to each of the edit controls. It also uses the individual color values to set the background color for each of the scrollbars, as well as being used to set the position of the thumb in the scrollbars. Finally, it sets the color of the rectangle to that of the color passed.

The next two functions are quite similar, but different enough to deserve different function implementations. In each case, the functions retrieve the current value of the control, convert it to a value, force the value to be within a valid range, and then they each call the adjustformcolorvals() function.

Example 5.4. The Code for the hexval_olf() and decval_olf() Functions
function hexval_olf(wxformedittext me)
  integer rgbval
  string hexcolor

  hexcolor = me.gettext()
  hexcolor = .if(hexcolor <= "", "0", hexcolor)

  rgbval = .toval(hexcolor, nohexdigits(hexcolor), 16)
  rgbval = .max(.min(0xffffff, rgbval), 0)
  adjustformcolorvals(me.form, rgbval)
end function

function decval_olf(wxformedittext me)
  integer rgbval
  string deccolor

  deccolor = me.gettext()
  deccolor = .if(deccolor <= "", "0", deccolor)

  rgbval = .toval(deccolor, nodigits(deccolor), 10)
  rgbval = .max(.min(0xffffff, rgbval), 0)
  adjustformcolorvals(me.form, rgbval)
end function
      

There are two special function calls in the previous code, nohexdigits() and nodigits(). Each is designed to extract all of the characters of a specific type, either normal digits or the normal plus hexadecimal digits. The result is passed to the .toval() function as the characters to ignore when converting the value.

The next task is to handle the events for the individual color values. As mentioned earlier, these will, in fact, be exactly the same code in each case, since the change to any one color value still requires all the color values to be read. All the event handlers will call the exact same function, which we will call handleonecolorchange().

Example 5.5. Handling the Events for the Color Edit Controls
function handleonecolorchange(wxform f)
  integer red, green, blue, rgbval
  string color

  color = f!tbRed.gettext()
  red = .toval(color, nodigits(color), 10)
  red = .max(0, .min(255, red))
  color = f!tbGreen.gettext()
  green = .toval(color, nodigits(color), 10)
  green = .max(0, .min(255, green))
  color = f!tbBlue.gettext()
  blue = .toval(color, nodigits(color), 10)
  blue = .max(0, .min(255, blue))
  rgbval = calcrgbval(red, green, blue)
  adjustformcolorvals(f, rgbval)
end function

function redval_olf(wxformedittext me)
  handleonecolorchange(me.form)
end function

function greenval_olf(wxformedittext me)
  handleonecolorchange(me.form)
end function

function blueval_olf(wxformedittext me)
  handleonecolorchange(me.form)
end function

function calcrgbval(integer red, integer green, integer blue)
  integer rgbval

  rgbval = blue * 0x10000 + green * 0x100 + red
end function rgbval
      

The final piece of the puzzle is to handle the events for the scrollbars, and this next piece of code does that. Again, all three have much in common, so they all call one common routine called doscrollbars().

Example 5.6. Handling the Scroll Bar Events
function getcurrentcolorvals(wxform f, integer red, \
                             integer green, integer blue)
  red = f!sbRed.position
  green = f!sbGreen.position
  blue = f!sbBlue.position
end function

function doscrollbars(wxform f, string ignorescrollbar)
  integer red, green, blue, rgbval

  red = 0; green = 0; blue = 0
  getcurrentcolorvals(f, red, green, blue)
  rgbval = calcrgbval(red, green, blue)
  adjustformcolorvals(f, rgbval, ignorescrollbar)
end function

function redscroll_os(wxformscrollbar me, string scrolltype)
  doscrollbars(me.form, "red")
end function

function greenscroll_os(wxformscrollbar me, string scrolltype)
  doscrollbars(me.form, "green")
end function

function bluescroll_os(wxformscrollbar me, string scrolltype)
  doscrollbars(me.form, "blue")
end function
      

The first function in the previous chunk of code is called by the doscrollbars() function to retrieve the component color values from the position property of each of the scrollbars. Since we set the range of the scrollbars to 256 and the thumb size to 1, that means that the range of valid positions is from 0 through 255. Once the component values have been retrieved, it calls the calcrgbval() function that is also called by the handleonecolorchange() function.

Finally, here is the code for the two functions mentioned earlier for extracting the valid digits from the string passed.

Example 5.7. Extracting the Digits from String Values
function nodigits(string s)
end function s-"0"-"1"-"2"-"3"-"4"-"5"-"6"-"7"-"8"-"9"

function nohexdigits(string s)
end function s-"0"-"1"-"2"-"3"-"4"-"5"-"6"-\
               "7"-"8"-"9"-"a"-"b"-"c"-"d"-\
               "e"-"f"-"A"-"B"-"C"-"D"-"E"-"F"