home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 8 / CDASC08.ISO / VRAC / VCOLORS.ZIP / COLORVW.CPP < prev    next >
Encoding:
Text File  |  1993-06-30  |  13.6 KB  |  558 lines

  1. //
  2. //    colorvw.cpp
  3. //
  4. //    Copyright (c) 1993, by Liant Software Corp.
  5. //    ALL RIGHTS RESERVED.
  6. //
  7. //    Revision History:
  8. //    -----------------
  9. //    06/29/93    mxs    Created.
  10.  
  11. extern "C" {
  12.     #include "stdlib.h"
  13. }
  14.  
  15. #include "colorvw.h"
  16.  
  17. #include "brush.h"
  18. #include "class.h"
  19. #include "iterator.h"
  20. #include "port.h"
  21. #include "pushbttn.h"
  22. #include "rect.h"
  23. #include "scrollbr.h"
  24. #include "shade.h"
  25. #include "textbox.h"
  26. #include "timer.h"
  27. #include "view.h"
  28.  
  29.  
  30. defineClass(ColorView, VAppView)
  31.  
  32.  
  33. //--------------------------------------------------------------------------
  34. ColorView::ColorView()
  35. {
  36.     setTitle("Colors");
  37.     setBackground(new VBrush(LightGray));
  38.  
  39.     // Create the background brushes for the RGB scrollbars
  40.     rBrush = new VBrush(RED);
  41.     gBrush = new VBrush(GREEN);
  42.     bBrush = new VBrush(BLUE);
  43.  
  44.     // Create the background brushes for the CMYK scrollbars
  45.     cBrush = new VBrush(WHITE);
  46.     mBrush = new VBrush(WHITE);
  47.     yBrush = new VBrush(WHITE);
  48.     kBrush = new VBrush(WHITE);
  49.  
  50.     // create the RGB and CMYK scrollbars
  51.     rScrollBar = new VScrollBar(VFrame(40, 40, 20, 200), this, StyleVertical);
  52.     rScrollBar->setBackground(new VBrush(RED));
  53.     rScrollBar->setValues(255, 0, 1, 5);
  54.     rScrollBar->tracking(TRUE);
  55.     rScrollBar->uponChange(this, methodOf(ColorView, rgbValueChanged));
  56.     rScrollBar->setPos(255);
  57.  
  58.     gScrollBar = new VScrollBar(VFrame(100, 40, 20, 200), this, StyleVertical);
  59.     gScrollBar->setBackground(new VBrush(GREEN));
  60.     gScrollBar->setValues(255, 0, 1, 5);
  61.     gScrollBar->tracking(TRUE);
  62.     gScrollBar->uponChange(this, methodOf(ColorView, rgbValueChanged));
  63.     gScrollBar->setPos(255);
  64.  
  65.     bScrollBar = new VScrollBar(VFrame(160, 40, 20, 200), this, StyleVertical);
  66.     bScrollBar->setBackground(new VBrush(BLUE));
  67.     bScrollBar->setValues(255, 0, 1, 5);
  68.     bScrollBar->tracking(TRUE);
  69.     bScrollBar->uponChange(this, methodOf(ColorView, rgbValueChanged));
  70.     bScrollBar->setPos(255);
  71.  
  72.     cScrollBar = new VScrollBar(VFrame(400, 40, 20, 200), this, StyleVertical);
  73.     cScrollBar->setBackground(new VBrush(CYAN));
  74.     cScrollBar->setValues(255, 0, 1, 5);
  75.     cScrollBar->tracking(TRUE);
  76.     cScrollBar->uponChange(this, methodOf(ColorView, cValueChanged));
  77.  
  78.     mScrollBar = new VScrollBar(VFrame(460, 40, 20, 200), this, StyleVertical);
  79.     mScrollBar->setBackground(new VBrush(MAGENTA));
  80.     mScrollBar->setValues(255, 0, 1, 5);
  81.     mScrollBar->tracking(TRUE);
  82.     mScrollBar->uponChange(this, methodOf(ColorView, mValueChanged));
  83.  
  84.     yScrollBar = new VScrollBar(VFrame(520, 40, 20, 200), this, StyleVertical);
  85.     yScrollBar->setBackground(new VBrush(YELLOW));
  86.     yScrollBar->setValues(255, 0, 1, 5);
  87.     yScrollBar->tracking(TRUE);
  88.     yScrollBar->uponChange(this, methodOf(ColorView, yValueChanged));
  89.  
  90.     kScrollBar = new VScrollBar(VFrame(580, 40, 20, 200), this, StyleVertical);
  91.     kScrollBar->setBackground(new VBrush(BLACK));
  92.     kScrollBar->setValues(255, 0, 1, 5);
  93.     kScrollBar->tracking(TRUE);
  94.     kScrollBar->uponChange(this, methodOf(ColorView, kValueChanged));
  95.  
  96.     // Create the color boxes for all the scrollbars to indicate the
  97.     // current intensity of that particular color
  98.     rColorBox = new VView(VFrame(40, 280, 20, 20), this, StyleBorder);
  99.     rColorBox->setBackground(rBrush);
  100.     rColorBox->disable();
  101.  
  102.     gColorBox = new VView(VFrame(100, 280, 20, 20), this, StyleBorder);
  103.     gColorBox->setBackground(gBrush);
  104.     gColorBox->disable();
  105.  
  106.     bColorBox = new VView(VFrame(160, 280, 20, 20), this, StyleBorder);
  107.     bColorBox->setBackground(bBrush);
  108.     bColorBox->disable();
  109.  
  110.     cColorBox = new VView(VFrame(400, 280, 20, 20), this, StyleBorder);
  111.     cColorBox->setBackground(cBrush);
  112.     cColorBox->disable();
  113.  
  114.     mColorBox = new VView(VFrame(460, 280, 20, 20), this, StyleBorder);
  115.     mColorBox->setBackground(mBrush);
  116.     mColorBox->disable();
  117.  
  118.     yColorBox = new VView(VFrame(520, 280, 20, 20), this, StyleBorder);
  119.     yColorBox->setBackground(yBrush);
  120.     yColorBox->disable();
  121.  
  122.     kColorBox = new VView(VFrame(580, 280, 20, 20), this, StyleBorder);
  123.     kColorBox->setBackground(kBrush);
  124.     kColorBox->disable();
  125.  
  126.     // Create the textboxes to indicate the current color value in 
  127.     // nuumeric form for each of the RGB and CMYK colors
  128.     rEdit = new VTextBox(VFrame(35, 338, 30, 24), this, 
  129.                 StyleBorder | StyleCenter);
  130.     rEdit->putText("255");
  131.  
  132.     gEdit = new VTextBox(VFrame(95, 338, 30, 24), this,
  133.                 StyleBorder | StyleCenter);
  134.     gEdit->putText("255");
  135.  
  136.     bEdit = new VTextBox(VFrame(155, 338, 30, 24), this,
  137.                 StyleBorder | StyleCenter);
  138.     bEdit->putText("255");
  139.  
  140.     cEdit = new VTextBox(VFrame(395, 338, 30, 24), this,
  141.                 StyleBorder | StyleCenter);
  142.     cEdit->putText("0");
  143.  
  144.     mEdit = new VTextBox(VFrame(455, 338, 30, 24), this,
  145.                 StyleBorder | StyleCenter);
  146.     mEdit->putText("0");
  147.  
  148.     yEdit = new VTextBox(VFrame(515, 338, 30, 24), this,
  149.                 StyleBorder | StyleCenter);
  150.     yEdit->putText("0");
  151.  
  152.     kEdit = new VTextBox(VFrame(575, 338, 30, 24), this,
  153.                 StyleBorder | StyleCenter);
  154.     kEdit->putText("0");
  155.     
  156.     // Create the big color window which represents the current
  157.     // RGB/CMYK combination
  158.     colorBox = new VView(VFrame(220, 40, 140, 200), this, StyleBorder);
  159.     colorBox->disable();
  160.  
  161.     brush = new VBrush(WHITE);
  162.     
  163.     colorBox->setBackground(brush);
  164.  
  165.     // Create the  'Play' pushbutton and attach a callback
  166.     playButton = new 
  167.         VPushButton(VFrame(220, 280, 140, 80), this, StyleNoAutoDefault);
  168.     playButton->uponClick(this, methodOf(ColorView, play));
  169.     playButton->putText("Play");
  170.  
  171.     // Create the 'Exit' pushbutton and attach a callback
  172.      exitButton = new 
  173.         VPushButton(VFrame(40, 400, 560, 20), this, StyleNoAutoDefault);
  174.     exitButton->uponClick(this, methodOf(ColorView, exit));
  175.     exitButton->putText("Exit");
  176.  
  177.     // Create the timer and attach a callback
  178.     timer = new VTimer;
  179.     timer->uponTimeout(this, methodOf(ColorView, timeout));
  180. }
  181.  
  182.  
  183. //---------------------------------------------------------------------------
  184. ColorView::~ColorView()
  185. {
  186.     // Free up all the brushes and the timer object.
  187.  
  188.     delete rBrush;
  189.     delete gBrush;
  190.     delete bBrush;
  191.  
  192.     delete cBrush;
  193.     delete mBrush;
  194.     delete yBrush;
  195.     delete kBrush;
  196.  
  197.     delete rScrollBar->getBackground();
  198.     delete gScrollBar->getBackground();
  199.     delete bScrollBar->getBackground();
  200.  
  201.     delete cScrollBar->getBackground();
  202.     delete mScrollBar->getBackground();
  203.     delete yScrollBar->getBackground();
  204.     delete kScrollBar->getBackground();
  205.  
  206.     delete brush;
  207.     delete getBackground();
  208.  
  209.     delete timer;
  210. }
  211.  
  212.  
  213. //---------------------------------------------------------------------------
  214. boolean ColorView::free()
  215. {
  216.     delete this;
  217.     return(TRUE);
  218. }
  219.  
  220.  
  221. //---------------------------------------------------------------------------
  222. boolean ColorView::rgbValueChanged(int amount)
  223. {
  224.     // All three rScrollBar, gScrollBar and bScrollBar have the same 
  225.     // callback
  226.  
  227.     int r, g, b;
  228.     int c, m, y, k;
  229.     char value[10];
  230.  
  231.     //get the current RGB values
  232.     r = rScrollBar->getPos();
  233.     g = gScrollBar->getPos();
  234.     b = bScrollBar->getPos();
  235.  
  236.     // convert the RGB values to CMYK values
  237.     c = 255 - r;
  238.     m = 255 - g;
  239.     y = 255 - b;
  240.     k = minimum(c, m, y);
  241.     c = c - k;
  242.     m = m - k;
  243.     y = y - k;
  244.  
  245.     // Update the textboxes representing the numeric RGB values
  246.     itoa(r, value, 10);
  247.     rEdit->putText(value);
  248.     itoa(g, value, 10);
  249.     gEdit->putText(value);
  250.     itoa(b, value, 10);
  251.     bEdit->putText(value);
  252.  
  253.     // Update the textboxes representing the numeric CMYK values
  254.     itoa(c, value, 10);
  255.     cEdit->putText(value);
  256.     itoa(m, value, 10);
  257.     mEdit->putText(value);
  258.     itoa(y, value, 10);
  259.     yEdit->putText(value);
  260.     itoa(k, value, 10);
  261.     kEdit->putText(value);
  262.  
  263.     // Update the thumb positions on the CMYK scrollbars
  264.     cScrollBar->setPos(c);
  265.     mScrollBar->setPos(m);
  266.     yScrollBar->setPos(y);
  267.     kScrollBar->setPos(k);
  268.  
  269.     // Update the backgrounds for the RGB color boxes
  270.     rBrush->background(RGBCLR(r, 0, 0));
  271.     gBrush->background(RGBCLR(0, g, 0));
  272.     bBrush->background(RGBCLR(0, 0, b));
  273.  
  274.     // Update the backgrounds for CMYK color boxes
  275.     cBrush->background(RGBCLR(255-c, 255, 255));
  276.     mBrush->background(RGBCLR(255, 255-m, 255));
  277.     yBrush->background(RGBCLR(255, 255, 255-y));
  278.     kBrush->background(RGBCLR(255-k, 255-k, 255-k));
  279.  
  280.     // Update the background for big color window
  281.     brush->background(RGBCLR(r,g,b));
  282.  
  283.     // Force actual refresh of the RGB color boxes
  284.     rColorBox->update();
  285.     gColorBox->update();
  286.     bColorBox->update();
  287.  
  288.     // Force actual refresh of the CMYK color boxes
  289.     cColorBox->update();
  290.     mColorBox->update();
  291.     yColorBox->update();
  292.     kColorBox->update();
  293.  
  294.     // Force actual refresh of the big color window
  295.     colorBox->update();
  296.  
  297.     return TRUE;
  298. }
  299.  
  300.  
  301. //---------------------------------------------------------------------------
  302. boolean ColorView::cValueChanged(int amount)
  303. {
  304.     // Callback for cScrollBar
  305.  
  306.     int c, m, y, k;
  307.     int r;
  308.  
  309.     // get the current CMY values
  310.     c = cScrollBar->getPos();
  311.     m = mScrollBar->getPos();
  312.     y = yScrollBar->getPos();
  313.  
  314.     // one of c, m, or y must be zero at all times
  315.     if (m > 0 && y > 0) {
  316.         cScrollBar->setPos(0);
  317.         return TRUE;
  318.     }
  319.  
  320.     // get the current k value
  321.     k = kScrollBar->getPos();
  322.  
  323.     // compute the coressponding new r value
  324.     r = 255 - c - k;
  325.  
  326.     // Force the rScrollBar to the new r value, which in turn will call
  327.     // its callback function (rgbValueChanged) which is responsible for
  328.     // updating the display
  329.     rScrollBar->track(r);
  330.     
  331.     return TRUE;
  332. }
  333.  
  334.  
  335. //---------------------------------------------------------------------------
  336. boolean ColorView::mValueChanged(int amount)
  337. {
  338.     int c, m, y, k;
  339.     int g;
  340.  
  341.     // get the current CMY values
  342.     c = cScrollBar->getPos();
  343.     m = mScrollBar->getPos();
  344.     y = yScrollBar->getPos();
  345.  
  346.     // one of c, m, or y must be zero at all times
  347.     if (c > 0 && y > 0) {
  348.         mScrollBar->setPos(0);
  349.         return TRUE;
  350.     }
  351.  
  352.     // get the current K value
  353.     k = kScrollBar->getPos();
  354.  
  355.     // compute the coressponding new g value
  356.     g = 255 - m - k;
  357.  
  358.     // Force the gScrollBar to the new g value, which in turn will call
  359.     // its callback function (rgbValueChanged) which is responsible for
  360.     // updating the display
  361.     gScrollBar->track(g);
  362.     
  363.     return TRUE;
  364. }
  365.  
  366.  
  367. //---------------------------------------------------------------------------
  368. boolean ColorView::yValueChanged(int amount)
  369. {
  370.     int c, m, y, k;
  371.     int b;
  372.  
  373.     // get the current CMY values
  374.     c = cScrollBar->getPos();
  375.     m = mScrollBar->getPos();
  376.     y = yScrollBar->getPos();
  377.  
  378.     // one of c, m, or y must be zero at all times
  379.     if (c > 0 && m > 0) {
  380.         yScrollBar->setPos(0);
  381.         return TRUE;
  382.     }
  383.  
  384.     // get the current K value
  385.     k = kScrollBar->getPos();
  386.  
  387.     // compute the coressponding new b value
  388.     b = 255 - y - k;
  389.  
  390.     // Force the bScrollBar to the new b value, which in turn will call
  391.     // its callback function (rgbValueChanged) which is responsible for
  392.     // updating the display
  393.     bScrollBar->track(b);
  394.     
  395.     return TRUE;
  396. }
  397.  
  398.  
  399. //---------------------------------------------------------------------------
  400. boolean ColorView::kValueChanged(int amount)
  401. {
  402.     int r, g, b, k;
  403.     int maxRGB, trackAmt;
  404.  
  405.     // get the current RGB and K values
  406.     r = rScrollBar->getPos();
  407.     g = gScrollBar->getPos();
  408.     b = bScrollBar->getPos();
  409.     k = kScrollBar->getPos();
  410.  
  411.     maxRGB = maximum(r, g, b);
  412.     trackAmt = 255 - k - maxRGB;
  413.     
  414.     // (k + maxRGB) must always be equal to 255
  415.     // We don't need to do anything if the value of k is not changed
  416.     if (trackAmt == 0) {
  417.         return TRUE;
  418.     }
  419.  
  420.     if (maxRGB == r) {
  421.         rScrollBar->track(r + trackAmt);
  422.     }
  423.  
  424.     if (maxRGB == g) {
  425.         gScrollBar->track(g + trackAmt);
  426.     }
  427.  
  428.     bScrollBar->track(b + trackAmt);
  429.  
  430.     return TRUE;
  431. }
  432.  
  433.  
  434. //---------------------------------------------------------------------------
  435. boolean ColorView::play(VPushButton *btn)
  436. {
  437.     if (timer->timing()) {
  438.         timer->stop();
  439.         playButton->putText("Play");
  440.     }
  441.     else {
  442.         timer->start(100);
  443.         playButton->putText("Stop!");
  444.     }
  445.  
  446.     return TRUE;
  447. }
  448.  
  449.  
  450. //---------------------------------------------------------------------------
  451. boolean ColorView::exit(VPushButton *btn)
  452. {
  453.     if (timer->timing()) {
  454.         timer->stop();
  455.     }
  456.     close();
  457.  
  458.     return TRUE;
  459. }
  460.  
  461.  
  462. //-------------------------------------------------------------------------
  463. boolean ColorView::timeout(VTimer *t)
  464. {
  465.     // Callback from the timer
  466.  
  467.     // create random r,g,b values
  468.     int r = random(256);
  469.     int g = random(256);
  470.     int b = random(256);
  471.  
  472.     rScrollBar->track(r);
  473.     gScrollBar->track(g);
  474.     bScrollBar->track(b);
  475.  
  476.     return TRUE;
  477. }
  478.  
  479.  
  480. //---------------------------------------------------------------------------
  481. boolean ColorView::givenFocus() 
  482. {
  483.     VWindow *currentChild;
  484.     currentChild = getChildFocus(); 
  485.  
  486.     if (currentChild) {
  487.         currentChild->takeFocus();
  488.     }
  489.     else {
  490.         playButton->takeFocus();
  491.     }
  492.  
  493.     return TRUE;
  494. }
  495.  
  496.  
  497. //---------------------------------------------------------------------------
  498. boolean ColorView::paint() 
  499.     int x, y, w, h, insetX, insetY;
  500.  
  501.     VShade shade;
  502.     shade.setBevelThickness(1);
  503.  
  504.     VRectangle rect;
  505.     VPort port(this);
  506.  
  507.     port.open();
  508.  
  509.     // Create the 3D frame around all the objects
  510.     if (children) {
  511.         DO(children, VWindow, child)
  512.             child->getRel(&x, &y, &w, &h);
  513.             if (!child->isA(VTextBoxCls)) {
  514.                 x -= 20; 
  515.                 y -= 20;
  516.                 w += 40;
  517.                 h += 40;
  518.                 insetX = 18;
  519.                 insetY = 18;
  520.             }
  521.             else {
  522.                 x -= 15; 
  523.                 y -= 18;
  524.                 w += 30;
  525.                 h += 36;
  526.                 insetX = 13;
  527.                 insetY = 16;
  528.             }            
  529.             rect.set(CornerDim, x, y, w, h);
  530.             shade.panel(Raised, rect, 0, port);
  531.             rect.inset(insetX, insetY);
  532.             shade.panel(Indented, rect, 0, port);
  533.         END 
  534.     }
  535.     port.close();
  536.  
  537.     return TRUE;
  538. }
  539.  
  540. //---------------------------------------------------------------------------
  541. int ColorView::minimum(int x, int y, int z)
  542. {
  543.     int m;
  544.     m = (x < y) ? x : y;
  545.     m = (m < z) ? m : z;
  546.     return m;
  547. }
  548.  
  549. //---------------------------------------------------------------------------
  550. int ColorView::maximum(int x, int y, int z)
  551. {
  552.     int m;
  553.     m = (x > y) ? x : y;
  554.     m = (m > z) ? m : z;
  555.     return m;
  556. }
  557.