Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

XCmyk - CMYK to RGB Calculator with source code

, 5 Jul 2003 CPOL
XCmyk implements an RGB to CMYK calculator
// XCmykTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "XCmykTest.h"
#include "XCmykTestDlg.h"
#include "about.h"
#include "XCmyk.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


///////////////////////////////////////////////////////////////////////////////
// CXCmykTestDlg dialog

BEGIN_MESSAGE_MAP(CXCmykTestDlg, BASE_CLASS)
	//{{AFX_MSG_MAP(CXCmykTestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_USER + 1, OnChangeRgbValues)
	ON_MESSAGE(WM_USER + 2, OnChangeCmykValues)
END_MESSAGE_MAP()

///////////////////////////////////////////////////////////////////////////////
// ctor
CXCmykTestDlg::CXCmykTestDlg(CWnd* pParent /*=NULL*/)
	: BASE_CLASS(CXCmykTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CXCmykTestDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

///////////////////////////////////////////////////////////////////////////////
// DoDataExchange
void CXCmykTestDlg::DoDataExchange(CDataExchange* pDX)
{
	BASE_CLASS::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CXCmykTestDlg)
	DDX_Control(pDX, IDC_STCCOLOR,      m_stcColor);

	DDX_Control(pDX, IDC_KNOBRED,       m_knbRed);
	DDX_Control(pDX, IDC_KNOBGREEN,     m_knbGreen);
	DDX_Control(pDX, IDC_KNOBBLUE,      m_knbBlue);

	DDX_Control(pDX, IDC_KNOBCYAN,      m_knbCyan);
	DDX_Control(pDX, IDC_KNOBMAGENTA,   m_knbMagenta);
	DDX_Control(pDX, IDC_KNOBYELLOW,    m_knbYellow);
	DDX_Control(pDX, IDC_KNOBBLACK,     m_knbBlack);

	DDX_Control(pDX, IDC_RED_VALUE,     m_RedValue);
	DDX_Control(pDX, IDC_GREEN_VALUE,   m_GreenValue);
	DDX_Control(pDX, IDC_BLUE_VALUE,    m_BlueValue);

	DDX_Control(pDX, IDC_CYAN_VALUE,    m_CyanValue);
	DDX_Control(pDX, IDC_MAGENTA_VALUE, m_MagentaValue);
	DDX_Control(pDX, IDC_YELLOW_VALUE,  m_YellowValue);
	DDX_Control(pDX, IDC_BLACK_VALUE,   m_BlackValue);
	//}}AFX_DATA_MAP
}

///////////////////////////////////////////////////////////////////////////////
// OnInitDialog
BOOL CXCmykTestDlg::OnInitDialog()
{
	BASE_CLASS::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	//ActivateEasyMoveMode(TRUE);

	// =====  RGB knobs  =====
	m_knbRed.SetRange(0, 255);
	m_knbRed.ShowSmallScale(FALSE, FALSE);
	m_knbRed.SetLargeChangeValue(10, FALSE);
	m_knbRed.SetSmallChangeValue(1, FALSE);
	m_knbRed.SetScaleColor((ARGB)Color.Red);
	m_knbRed.SetChangeCallback(m_hWnd, WM_USER + 1);
	m_knbRed.DrawTransparent();
	m_knbRed.SetPos(255);
	m_RedValue.SetBkColor(RGB(255,255,255));
	m_RedValue.SetWindowText(_T("0"));

	m_knbGreen.SetRange(0, 255);
	m_knbGreen.ShowSmallScale(FALSE, FALSE);
	m_knbGreen.SetLargeChangeValue(10, FALSE);
	m_knbGreen.SetSmallChangeValue(1, FALSE);
	m_knbGreen.SetScaleColor((ARGB)Color.Green);
	m_knbGreen.SetChangeCallback(m_hWnd, WM_USER + 1);
	m_knbGreen.DrawTransparent();
	m_knbGreen.SetPos(255);
	m_GreenValue.SetBkColor(RGB(255,255,255));
	m_GreenValue.SetWindowText(_T("0"));

	m_knbBlue.SetRange(0, 255);
	m_knbBlue.ShowSmallScale(FALSE, FALSE);
	m_knbBlue.SetLargeChangeValue(10, FALSE);
	m_knbBlue.SetSmallChangeValue(1, FALSE);
	m_knbBlue.SetScaleColor((ARGB)Color.Blue);
	m_knbBlue.SetChangeCallback(m_hWnd, WM_USER + 1);
	m_knbBlue.DrawTransparent();
	m_knbBlue.SetPos(255);
	m_BlueValue.SetBkColor(RGB(255,255,255));
	m_BlueValue.SetWindowText(_T("0"));

	// =====  CMYK knobs  =====
	m_knbCyan.SetRange(0, 100);
	m_knbCyan.ShowSmallScale(FALSE, FALSE);
	m_knbCyan.SetLargeChangeValue(10, FALSE);
	m_knbCyan.SetSmallChangeValue(1, FALSE);
	m_knbCyan.SetScaleColor((ARGB)Color.Cyan);
	m_knbCyan.SetChangeCallback(m_hWnd, WM_USER + 2);
	m_knbCyan.DrawTransparent();
	m_knbCyan.SetPos(0);
	m_CyanValue.SetBkColor(RGB(255,255,255));
	m_CyanValue.SetWindowText(_T("0"));

	m_knbMagenta.SetRange(0, 100);
	m_knbMagenta.ShowSmallScale(FALSE, FALSE);
	m_knbMagenta.SetLargeChangeValue(10, FALSE);
	m_knbMagenta.SetSmallChangeValue(1, FALSE);
	m_knbMagenta.SetScaleColor((ARGB)Color.Magenta);
	m_knbMagenta.SetChangeCallback(m_hWnd, WM_USER + 2);
	m_knbMagenta.DrawTransparent();
	m_knbMagenta.SetPos(0);
	m_MagentaValue.SetBkColor(RGB(255,255,255));
	m_MagentaValue.SetWindowText(_T("0"));

	m_knbYellow.SetRange(0, 100);
	m_knbYellow.ShowSmallScale(FALSE, FALSE);
	m_knbYellow.SetLargeChangeValue(10, FALSE);
	m_knbYellow.SetSmallChangeValue(1, FALSE);
	m_knbYellow.SetScaleColor((ARGB)Color.Gold);
	m_knbYellow.SetChangeCallback(m_hWnd, WM_USER + 2);
	m_knbYellow.DrawTransparent();
	m_knbYellow.SetPos(0);
	m_YellowValue.SetBkColor(RGB(255,255,255));
	m_YellowValue.SetWindowText(_T("0"));

	m_knbBlack.SetRange(0, 100);
	m_knbBlack.ShowSmallScale(FALSE, FALSE);
	m_knbBlack.SetLargeChangeValue(5, FALSE);
	m_knbBlack.SetSmallChangeValue(1, FALSE);
	m_knbBlack.SetScaleColor((ARGB)Color.Black);
	m_knbBlack.SetChangeCallback(m_hWnd, WM_USER + 2);
	m_knbBlack.DrawTransparent();
	m_knbBlack.SetPos(0);
	m_BlackValue.SetBkColor(RGB(255,255,255));
	m_BlackValue.SetWindowText(_T("0"));

	// Update color label
	OnChangeRgbValues(0, 0);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

///////////////////////////////////////////////////////////////////////////////
// OnSysCommand
void CXCmykTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		BASE_CLASS::OnSysCommand(nID, lParam);
	}
}

///////////////////////////////////////////////////////////////////////////////
// OnPaint
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon.  For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CXCmykTestDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		BASE_CLASS::OnPaint();
	}
}

///////////////////////////////////////////////////////////////////////////////
// OnQueryDragIcon
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CXCmykTestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

///////////////////////////////////////////////////////////////////////////////
// OnChangeRgbValues
LRESULT CXCmykTestDlg::OnChangeRgbValues(WPARAM /*wParam*/, LPARAM /*lParam*/)
{
	TRACE(_T("in CXCmykTestDlg::OnChangeRgbValues\n"));

	BYTE	byRed = 0;
	BYTE	byGreen = 0;
	BYTE	byBlue = 0;
	BYTE	byCyan = 0;
	BYTE	byMagenta = 0;
	BYTE	byYellow = 0;
	BYTE	byBlack = 0;

	TCHAR szBuf[100];

	byRed = (BYTE)m_knbRed.GetPos();
	m_RedValue.SetWindowText(_T(""));
	m_RedValue.RedrawWindow();
	m_RedValue.SetWindowText(_itot(byRed, szBuf, 10));

	byGreen = (BYTE)m_knbGreen.GetPos();
	m_GreenValue.SetWindowText(_T(""));
	m_GreenValue.RedrawWindow();
	m_GreenValue.SetWindowText(_itot(byGreen, szBuf, 10));

	byBlue = (BYTE)m_knbBlue.GetPos();
	m_BlueValue.SetWindowText(_T(""));
	m_BlueValue.RedrawWindow();
	m_BlueValue.SetWindowText(_itot(byBlue, szBuf, 10));

	// Update color label
	COLORREF rgb1 = RGB(byRed, byGreen, byBlue);
	m_stcColor.SetBkColor(rgb1);

	RGB2CMYK(byRed, byGreen, byBlue, byCyan, byMagenta, byYellow, byBlack);

	m_CyanValue.SetWindowText(_T(""));
	m_CyanValue.RedrawWindow();
	m_CyanValue.SetWindowText(_itot(byCyan, szBuf, 10));
	m_knbCyan.SetPos(byCyan);
	m_knbCyan.RedrawWindow();

	m_MagentaValue.SetWindowText(_T(""));
	m_MagentaValue.RedrawWindow();
	m_MagentaValue.SetWindowText(_itot(byMagenta, szBuf, 10));
	m_knbMagenta.SetPos(byMagenta);
	m_knbMagenta.RedrawWindow();

	m_YellowValue.SetWindowText(_T(""));
	m_YellowValue.RedrawWindow();
	m_YellowValue.SetWindowText(_itot(byYellow, szBuf, 10));
	m_knbYellow.SetPos(byYellow);
	m_knbYellow.RedrawWindow();

	m_BlackValue.SetWindowText(_T(""));
	m_BlackValue.RedrawWindow();
	m_BlackValue.SetWindowText(_itot(byBlack, szBuf, 10));
	m_knbBlack.SetPos(byBlack);
	m_knbBlack.RedrawWindow();

	TRACE(_T("RGB(%d,%d,%d) == > c=%d m=%d y=%d k=%d\n"), 
		byRed, byGreen, byBlue,
		byCyan, byMagenta, byYellow, byBlack);

	return 0;
}

///////////////////////////////////////////////////////////////////////////////
// OnChangeCmykValues
LRESULT CXCmykTestDlg::OnChangeCmykValues(WPARAM /*wParam*/, LPARAM /*lParam*/)
{
	TRACE(_T("in CXCmykTestDlg::OnChangeCmykValues\n"));

	BYTE	byRed = 0;
	BYTE	byGreen = 0;
	BYTE	byBlue = 0;
	BYTE	byCyan = 0;
	BYTE	byMagenta = 0;
	BYTE	byYellow = 0;
	BYTE	byBlack = 0;

	TCHAR szBuf[100];

	byCyan = (BYTE)m_knbCyan.GetPos();
	m_CyanValue.SetWindowText(_T(""));
	m_CyanValue.RedrawWindow();
	m_CyanValue.SetWindowText(_itot(byCyan, szBuf, 10));

	byMagenta = (BYTE)m_knbMagenta.GetPos();
	m_MagentaValue.SetWindowText(_T(""));
	m_MagentaValue.RedrawWindow();
	m_MagentaValue.SetWindowText(_itot(byMagenta, szBuf, 10));

	byYellow = (BYTE)m_knbYellow.GetPos();
	m_YellowValue.SetWindowText(_T(""));
	m_YellowValue.RedrawWindow();
	m_YellowValue.SetWindowText(_itot(byYellow, szBuf, 10));

	byBlack = (BYTE)m_knbBlack.GetPos();
	m_BlackValue.SetWindowText(_T(""));
	m_BlackValue.RedrawWindow();
	m_BlackValue.SetWindowText(_itot(byBlack, szBuf, 10));

	COLORREF rgb2 = CMYK2RGB(byCyan, byMagenta, byYellow, byBlack);

	byRed = GetRValue(rgb2);
	byGreen = GetGValue(rgb2);
	byBlue = GetBValue(rgb2);

	TRACE(_T("c=%d m=%d y=%d k=%d ==> RGB(%d,%d,%d)\n"), 
		byCyan, byMagenta, byYellow, byBlack,
		byRed, byGreen, byBlue);

	// Update color label
	m_stcColor.SetBkColor(rgb2);

	m_RedValue.SetWindowText(_T(""));
	m_RedValue.RedrawWindow();
	m_RedValue.SetWindowText(_itot(byRed, szBuf, 10));
	m_knbRed.SetPos(byRed);
	m_knbRed.RedrawWindow();

	m_GreenValue.SetWindowText(_T(""));
	m_GreenValue.RedrawWindow();
	m_GreenValue.SetWindowText(_itot(byGreen, szBuf, 10));
	m_knbGreen.SetPos(byGreen);
	m_knbGreen.RedrawWindow();

	m_BlueValue.SetWindowText(_T(""));
	m_BlueValue.RedrawWindow();
	m_BlueValue.SetWindowText(_itot(byBlue, szBuf, 10));
	m_knbBlue.SetPos(byBlue);
	m_knbBlue.RedrawWindow();

	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Hans Dietrich
Software Developer (Senior) Hans Dietrich Software
United States United States
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.
 
Recently, I have moved to Los Angeles where I am doing consulting and development work.
 
For consulting and custom software development, please see www.hdsoft.org.






| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411023.1 | Last Updated 6 Jul 2003
Article Copyright 2003 by Hans Dietrich
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid