home *** CD-ROM | disk | FTP | other *** search
- //
- // colorvw.cpp
- //
- // Copyright (c) 1993, by Liant Software Corp.
- // ALL RIGHTS RESERVED.
- //
- // Revision History:
- // -----------------
- // 06/29/93 mxs Created.
-
- extern "C" {
- #include "stdlib.h"
- }
-
- #include "colorvw.h"
-
- #include "brush.h"
- #include "class.h"
- #include "iterator.h"
- #include "port.h"
- #include "pushbttn.h"
- #include "rect.h"
- #include "scrollbr.h"
- #include "shade.h"
- #include "textbox.h"
- #include "timer.h"
- #include "view.h"
-
-
- defineClass(ColorView, VAppView)
-
-
- //--------------------------------------------------------------------------
- ColorView::ColorView()
- {
- setTitle("Colors");
- setBackground(new VBrush(LightGray));
-
- // Create the background brushes for the RGB scrollbars
- rBrush = new VBrush(RED);
- gBrush = new VBrush(GREEN);
- bBrush = new VBrush(BLUE);
-
- // Create the background brushes for the CMYK scrollbars
- cBrush = new VBrush(WHITE);
- mBrush = new VBrush(WHITE);
- yBrush = new VBrush(WHITE);
- kBrush = new VBrush(WHITE);
-
- // create the RGB and CMYK scrollbars
- rScrollBar = new VScrollBar(VFrame(40, 40, 20, 200), this, StyleVertical);
- rScrollBar->setBackground(new VBrush(RED));
- rScrollBar->setValues(255, 0, 1, 5);
- rScrollBar->tracking(TRUE);
- rScrollBar->uponChange(this, methodOf(ColorView, rgbValueChanged));
- rScrollBar->setPos(255);
-
- gScrollBar = new VScrollBar(VFrame(100, 40, 20, 200), this, StyleVertical);
- gScrollBar->setBackground(new VBrush(GREEN));
- gScrollBar->setValues(255, 0, 1, 5);
- gScrollBar->tracking(TRUE);
- gScrollBar->uponChange(this, methodOf(ColorView, rgbValueChanged));
- gScrollBar->setPos(255);
-
- bScrollBar = new VScrollBar(VFrame(160, 40, 20, 200), this, StyleVertical);
- bScrollBar->setBackground(new VBrush(BLUE));
- bScrollBar->setValues(255, 0, 1, 5);
- bScrollBar->tracking(TRUE);
- bScrollBar->uponChange(this, methodOf(ColorView, rgbValueChanged));
- bScrollBar->setPos(255);
-
- cScrollBar = new VScrollBar(VFrame(400, 40, 20, 200), this, StyleVertical);
- cScrollBar->setBackground(new VBrush(CYAN));
- cScrollBar->setValues(255, 0, 1, 5);
- cScrollBar->tracking(TRUE);
- cScrollBar->uponChange(this, methodOf(ColorView, cValueChanged));
-
- mScrollBar = new VScrollBar(VFrame(460, 40, 20, 200), this, StyleVertical);
- mScrollBar->setBackground(new VBrush(MAGENTA));
- mScrollBar->setValues(255, 0, 1, 5);
- mScrollBar->tracking(TRUE);
- mScrollBar->uponChange(this, methodOf(ColorView, mValueChanged));
-
- yScrollBar = new VScrollBar(VFrame(520, 40, 20, 200), this, StyleVertical);
- yScrollBar->setBackground(new VBrush(YELLOW));
- yScrollBar->setValues(255, 0, 1, 5);
- yScrollBar->tracking(TRUE);
- yScrollBar->uponChange(this, methodOf(ColorView, yValueChanged));
-
- kScrollBar = new VScrollBar(VFrame(580, 40, 20, 200), this, StyleVertical);
- kScrollBar->setBackground(new VBrush(BLACK));
- kScrollBar->setValues(255, 0, 1, 5);
- kScrollBar->tracking(TRUE);
- kScrollBar->uponChange(this, methodOf(ColorView, kValueChanged));
-
- // Create the color boxes for all the scrollbars to indicate the
- // current intensity of that particular color
- rColorBox = new VView(VFrame(40, 280, 20, 20), this, StyleBorder);
- rColorBox->setBackground(rBrush);
- rColorBox->disable();
-
- gColorBox = new VView(VFrame(100, 280, 20, 20), this, StyleBorder);
- gColorBox->setBackground(gBrush);
- gColorBox->disable();
-
- bColorBox = new VView(VFrame(160, 280, 20, 20), this, StyleBorder);
- bColorBox->setBackground(bBrush);
- bColorBox->disable();
-
- cColorBox = new VView(VFrame(400, 280, 20, 20), this, StyleBorder);
- cColorBox->setBackground(cBrush);
- cColorBox->disable();
-
- mColorBox = new VView(VFrame(460, 280, 20, 20), this, StyleBorder);
- mColorBox->setBackground(mBrush);
- mColorBox->disable();
-
- yColorBox = new VView(VFrame(520, 280, 20, 20), this, StyleBorder);
- yColorBox->setBackground(yBrush);
- yColorBox->disable();
-
- kColorBox = new VView(VFrame(580, 280, 20, 20), this, StyleBorder);
- kColorBox->setBackground(kBrush);
- kColorBox->disable();
-
- // Create the textboxes to indicate the current color value in
- // nuumeric form for each of the RGB and CMYK colors
- rEdit = new VTextBox(VFrame(35, 338, 30, 24), this,
- StyleBorder | StyleCenter);
- rEdit->putText("255");
-
- gEdit = new VTextBox(VFrame(95, 338, 30, 24), this,
- StyleBorder | StyleCenter);
- gEdit->putText("255");
-
- bEdit = new VTextBox(VFrame(155, 338, 30, 24), this,
- StyleBorder | StyleCenter);
- bEdit->putText("255");
-
- cEdit = new VTextBox(VFrame(395, 338, 30, 24), this,
- StyleBorder | StyleCenter);
- cEdit->putText("0");
-
- mEdit = new VTextBox(VFrame(455, 338, 30, 24), this,
- StyleBorder | StyleCenter);
- mEdit->putText("0");
-
- yEdit = new VTextBox(VFrame(515, 338, 30, 24), this,
- StyleBorder | StyleCenter);
- yEdit->putText("0");
-
- kEdit = new VTextBox(VFrame(575, 338, 30, 24), this,
- StyleBorder | StyleCenter);
- kEdit->putText("0");
-
- // Create the big color window which represents the current
- // RGB/CMYK combination
- colorBox = new VView(VFrame(220, 40, 140, 200), this, StyleBorder);
- colorBox->disable();
-
- brush = new VBrush(WHITE);
-
- colorBox->setBackground(brush);
-
- // Create the 'Play' pushbutton and attach a callback
- playButton = new
- VPushButton(VFrame(220, 280, 140, 80), this, StyleNoAutoDefault);
- playButton->uponClick(this, methodOf(ColorView, play));
- playButton->putText("Play");
-
- // Create the 'Exit' pushbutton and attach a callback
- exitButton = new
- VPushButton(VFrame(40, 400, 560, 20), this, StyleNoAutoDefault);
- exitButton->uponClick(this, methodOf(ColorView, exit));
- exitButton->putText("Exit");
-
- // Create the timer and attach a callback
- timer = new VTimer;
- timer->uponTimeout(this, methodOf(ColorView, timeout));
- }
-
-
- //---------------------------------------------------------------------------
- ColorView::~ColorView()
- {
- // Free up all the brushes and the timer object.
-
- delete rBrush;
- delete gBrush;
- delete bBrush;
-
- delete cBrush;
- delete mBrush;
- delete yBrush;
- delete kBrush;
-
- delete rScrollBar->getBackground();
- delete gScrollBar->getBackground();
- delete bScrollBar->getBackground();
-
- delete cScrollBar->getBackground();
- delete mScrollBar->getBackground();
- delete yScrollBar->getBackground();
- delete kScrollBar->getBackground();
-
- delete brush;
- delete getBackground();
-
- delete timer;
- }
-
-
- //---------------------------------------------------------------------------
- boolean ColorView::free()
- {
- delete this;
- return(TRUE);
- }
-
-
- //---------------------------------------------------------------------------
- boolean ColorView::rgbValueChanged(int amount)
- {
- // All three rScrollBar, gScrollBar and bScrollBar have the same
- // callback
-
- int r, g, b;
- int c, m, y, k;
- char value[10];
-
- //get the current RGB values
- r = rScrollBar->getPos();
- g = gScrollBar->getPos();
- b = bScrollBar->getPos();
-
- // convert the RGB values to CMYK values
- c = 255 - r;
- m = 255 - g;
- y = 255 - b;
- k = minimum(c, m, y);
- c = c - k;
- m = m - k;
- y = y - k;
-
- // Update the textboxes representing the numeric RGB values
- itoa(r, value, 10);
- rEdit->putText(value);
- itoa(g, value, 10);
- gEdit->putText(value);
- itoa(b, value, 10);
- bEdit->putText(value);
-
- // Update the textboxes representing the numeric CMYK values
- itoa(c, value, 10);
- cEdit->putText(value);
- itoa(m, value, 10);
- mEdit->putText(value);
- itoa(y, value, 10);
- yEdit->putText(value);
- itoa(k, value, 10);
- kEdit->putText(value);
-
- // Update the thumb positions on the CMYK scrollbars
- cScrollBar->setPos(c);
- mScrollBar->setPos(m);
- yScrollBar->setPos(y);
- kScrollBar->setPos(k);
-
- // Update the backgrounds for the RGB color boxes
- rBrush->background(RGBCLR(r, 0, 0));
- gBrush->background(RGBCLR(0, g, 0));
- bBrush->background(RGBCLR(0, 0, b));
-
- // Update the backgrounds for CMYK color boxes
- cBrush->background(RGBCLR(255-c, 255, 255));
- mBrush->background(RGBCLR(255, 255-m, 255));
- yBrush->background(RGBCLR(255, 255, 255-y));
- kBrush->background(RGBCLR(255-k, 255-k, 255-k));
-
- // Update the background for big color window
- brush->background(RGBCLR(r,g,b));
-
- // Force actual refresh of the RGB color boxes
- rColorBox->update();
- gColorBox->update();
- bColorBox->update();
-
- // Force actual refresh of the CMYK color boxes
- cColorBox->update();
- mColorBox->update();
- yColorBox->update();
- kColorBox->update();
-
- // Force actual refresh of the big color window
- colorBox->update();
-
- return TRUE;
- }
-
-
- //---------------------------------------------------------------------------
- boolean ColorView::cValueChanged(int amount)
- {
- // Callback for cScrollBar
-
- int c, m, y, k;
- int r;
-
- // get the current CMY values
- c = cScrollBar->getPos();
- m = mScrollBar->getPos();
- y = yScrollBar->getPos();
-
- // one of c, m, or y must be zero at all times
- if (m > 0 && y > 0) {
- cScrollBar->setPos(0);
- return TRUE;
- }
-
- // get the current k value
- k = kScrollBar->getPos();
-
- // compute the coressponding new r value
- r = 255 - c - k;
-
- // Force the rScrollBar to the new r value, which in turn will call
- // its callback function (rgbValueChanged) which is responsible for
- // updating the display
- rScrollBar->track(r);
-
- return TRUE;
- }
-
-
- //---------------------------------------------------------------------------
- boolean ColorView::mValueChanged(int amount)
- {
- int c, m, y, k;
- int g;
-
- // get the current CMY values
- c = cScrollBar->getPos();
- m = mScrollBar->getPos();
- y = yScrollBar->getPos();
-
- // one of c, m, or y must be zero at all times
- if (c > 0 && y > 0) {
- mScrollBar->setPos(0);
- return TRUE;
- }
-
- // get the current K value
- k = kScrollBar->getPos();
-
- // compute the coressponding new g value
- g = 255 - m - k;
-
- // Force the gScrollBar to the new g value, which in turn will call
- // its callback function (rgbValueChanged) which is responsible for
- // updating the display
- gScrollBar->track(g);
-
- return TRUE;
- }
-
-
- //---------------------------------------------------------------------------
- boolean ColorView::yValueChanged(int amount)
- {
- int c, m, y, k;
- int b;
-
- // get the current CMY values
- c = cScrollBar->getPos();
- m = mScrollBar->getPos();
- y = yScrollBar->getPos();
-
- // one of c, m, or y must be zero at all times
- if (c > 0 && m > 0) {
- yScrollBar->setPos(0);
- return TRUE;
- }
-
- // get the current K value
- k = kScrollBar->getPos();
-
- // compute the coressponding new b value
- b = 255 - y - k;
-
- // Force the bScrollBar to the new b value, which in turn will call
- // its callback function (rgbValueChanged) which is responsible for
- // updating the display
- bScrollBar->track(b);
-
- return TRUE;
- }
-
-
- //---------------------------------------------------------------------------
- boolean ColorView::kValueChanged(int amount)
- {
- int r, g, b, k;
- int maxRGB, trackAmt;
-
- // get the current RGB and K values
- r = rScrollBar->getPos();
- g = gScrollBar->getPos();
- b = bScrollBar->getPos();
- k = kScrollBar->getPos();
-
- maxRGB = maximum(r, g, b);
- trackAmt = 255 - k - maxRGB;
-
- // (k + maxRGB) must always be equal to 255
- // We don't need to do anything if the value of k is not changed
- if (trackAmt == 0) {
- return TRUE;
- }
-
- if (maxRGB == r) {
- rScrollBar->track(r + trackAmt);
- }
-
- if (maxRGB == g) {
- gScrollBar->track(g + trackAmt);
- }
-
- bScrollBar->track(b + trackAmt);
-
- return TRUE;
- }
-
-
- //---------------------------------------------------------------------------
- boolean ColorView::play(VPushButton *btn)
- {
- if (timer->timing()) {
- timer->stop();
- playButton->putText("Play");
- }
- else {
- timer->start(100);
- playButton->putText("Stop!");
- }
-
- return TRUE;
- }
-
-
- //---------------------------------------------------------------------------
- boolean ColorView::exit(VPushButton *btn)
- {
- if (timer->timing()) {
- timer->stop();
- }
- close();
-
- return TRUE;
- }
-
-
- //-------------------------------------------------------------------------
- boolean ColorView::timeout(VTimer *t)
- {
- // Callback from the timer
-
- // create random r,g,b values
- int r = random(256);
- int g = random(256);
- int b = random(256);
-
- rScrollBar->track(r);
- gScrollBar->track(g);
- bScrollBar->track(b);
-
- return TRUE;
- }
-
-
- //---------------------------------------------------------------------------
- boolean ColorView::givenFocus()
- {
- VWindow *currentChild;
- currentChild = getChildFocus();
-
- if (currentChild) {
- currentChild->takeFocus();
- }
- else {
- playButton->takeFocus();
- }
-
- return TRUE;
- }
-
-
- //---------------------------------------------------------------------------
- boolean ColorView::paint()
- {
- int x, y, w, h, insetX, insetY;
-
- VShade shade;
- shade.setBevelThickness(1);
-
- VRectangle rect;
- VPort port(this);
-
- port.open();
-
- // Create the 3D frame around all the objects
- if (children) {
- DO(children, VWindow, child)
- child->getRel(&x, &y, &w, &h);
- if (!child->isA(VTextBoxCls)) {
- x -= 20;
- y -= 20;
- w += 40;
- h += 40;
- insetX = 18;
- insetY = 18;
- }
- else {
- x -= 15;
- y -= 18;
- w += 30;
- h += 36;
- insetX = 13;
- insetY = 16;
- }
- rect.set(CornerDim, x, y, w, h);
- shade.panel(Raised, rect, 0, port);
- rect.inset(insetX, insetY);
- shade.panel(Indented, rect, 0, port);
- END
- }
- port.close();
-
- return TRUE;
- }
-
- //---------------------------------------------------------------------------
- int ColorView::minimum(int x, int y, int z)
- {
- int m;
- m = (x < y) ? x : y;
- m = (m < z) ? m : z;
- return m;
- }
-
- //---------------------------------------------------------------------------
- int ColorView::maximum(int x, int y, int z)
- {
- int m;
- m = (x > y) ? x : y;
- m = (m > z) ? m : z;
- return m;
- }