2 ===========================================================================
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
26 ===========================================================================
29 #include "../../idlib/precompiled.h"
32 #include "../../sys/win32/rc/Radiant_resource.h"
33 #include "DialogColorPicker.h"
35 #ifdef ID_DEBUG_MEMORY
43 class CMyColorDialog : public CColorDialog
45 DECLARE_DYNCREATE(CMyColorDialog);
48 CMyColorDialog( COLORREF clrInit = 0, DWORD dwFlags = 0, CWnd *pParentWnd = NULL );
49 virtual int DoModal();
52 enum { NCUSTCOLORS = 16 };
53 static COLORREF c_CustColors[NCUSTCOLORS];
54 static COLORREF c_LastCustColors[NCUSTCOLORS];
55 static bool c_NeedToInitCustColors;
56 static void InitCustColors();
57 static void SaveCustColors();
60 //{{AFX_DATA(CMyColorDialog)
64 // ClassWizard generate virtual function overrides
65 //{{AFX_VIRTUAL(CMyColorDialog)
66 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
69 // Generated message map functions
70 //{{AFX_MSG(CMyColorDialog)
75 IMPLEMENT_DYNCREATE( CMyColorDialog, CColorDialog )
77 bool CMyColorDialog::c_NeedToInitCustColors = true;
78 COLORREF CMyColorDialog::c_CustColors[];
79 COLORREF CMyColorDialog::c_LastCustColors[];
81 #define SECTION _T("Custom Colors")
83 void CMyColorDialog::InitCustColors() {
84 for ( int i = 0; i < NCUSTCOLORS; i++) {
86 entry.Format( "tool_color%d", i);
87 idCVar *cvar = cvarSystem->Find( entry );
89 c_LastCustColors[i] = c_CustColors[i] = cvar->GetInteger();
91 c_LastCustColors[i] = c_CustColors[i] = RGB( 255, 255, 255 );
94 c_NeedToInitCustColors= false;
97 void CMyColorDialog::SaveCustColors() {
98 for (int i = 0; i < NCUSTCOLORS; i++) {
99 if ( c_LastCustColors[i] != c_CustColors[i] ) {
101 entry.Format( "tool_color%d", i );
102 if ( c_CustColors[i] == RGB( 255, 255, 255 ) ) {
103 cvarSystem->SetCVarString( entry, "" );
105 cvarSystem->SetCVarString( entry, va( "%d", c_CustColors[i] ), CVAR_TOOL );
107 c_LastCustColors[i] = c_CustColors[i];
112 CMyColorDialog::CMyColorDialog( COLORREF clrInit, DWORD dwFlags,
113 CWnd* pParentWnd) : CColorDialog(clrInit,dwFlags,pParentWnd)
115 //{{AFX_DATA_INIT(CMyColorDialog)
117 if (c_NeedToInitCustColors) {
120 m_cc.lpCustColors = c_CustColors;
123 int CMyColorDialog::DoModal() {
124 int code = CColorDialog::DoModal();
129 void CMyColorDialog::DoDataExchange(CDataExchange* pDX) {
130 // overridden (calls this base class)
131 CColorDialog::DoDataExchange(pDX);
132 //{{AFX_DATA_MAP(CMyColorDialog)
136 BEGIN_MESSAGE_MAP(CMyColorDialog, CColorDialog)
137 //{{AFX_MSG_MAP(CMyColorDialog)
141 COLORREF DoOldColor(COLORREF cr) {
142 CMyColorDialog dlg(cr, CC_FULLOPEN | CC_RGBINIT | CC_ANYCOLOR);
143 if (dlg.DoModal() == IDOK) {
144 return dlg.GetColor();
151 // Original ColorPicker/DIB source by Rajiv Ramachandran <rrajivram@hotmail.com>
152 // included with Permission from the author
159 #define IN_OVERBRIGHT 3
161 int Distance(CPoint pt1,CPoint pt2);
164 double Slope( CPoint pt1,CPoint pt2 ) {
176 CPoint Intersection(LineDesc l1,LineDesc l2)
181 if(l1.slope == l2.slope)
183 // Parallel lines, no intersection
187 if(l1.slope == BAD_SLOPE )
189 // First Line is vertical, eqn is x=0
190 // Put x = 0 in second line eqn to get y;
192 y = l2.slope * x + l2.c;
195 if(l2.slope == BAD_SLOPE)
197 // second line is vertical Equation of line is x=0;
198 // Put x = 0 in first line eqn to get y;
200 y = l1.slope * l2.x + l1.c;
204 y = ((l1.c * l2.slope) - (l2.c * l1.slope))/(l2.slope - l1.slope);
205 x = (y - l1.c)/l1.slope;
208 return CPoint((int)x,(int)y);
211 double FindC(LineDesc& l)
215 if(l.slope == BAD_SLOPE)
221 c = l.y - l.slope * l.x;
226 CPoint PointOnLine(CPoint pt1,CPoint pt2,int len,int maxlen )
228 double x,y,m,a,c,C,A;
240 m = (double)(pt2.y - pt1.y)/(pt2.x - pt1.x);
243 c = (double)pt1.y - m * (double)pt1.x;
256 x = (B + idMath::Sqrt(B*B - (4.0*A*C)))/(2.0*A);
258 pt = CPoint((int)x,(int)y);
259 if(Distance(pt,pt1) > maxlen || Distance(pt,pt2) > maxlen)
261 x = (B - idMath::Sqrt(B*B - (4.0*A*C)))/(2.0 * A);
263 pt = CPoint((int)x,(int)y);
269 y = idMath::Sqrt(a2);
271 pt = CPoint((int)x,(int)y);
273 if(Distance(pt,pt1) > maxlen || Distance(pt,pt2) > maxlen)
276 pt = CPoint((int)x,(int)y);
285 int Distance(CPoint pt1,CPoint pt2)
296 a = (double)x + (double)y ;
301 double AngleFromPoint(CPoint pt,CPoint center)
305 y = -1 * (pt.y - center.y);
317 CPoint PtFromAngle(double angle,double sat,CPoint center)
319 angle = DEG2RAD(angle);
324 x = sat * cos(angle);
325 y = sat * sin(angle);
329 pt = CPoint((int)x,(int)y);
335 RGBType HSVType::toRGB()
341 rgb.r = rgb.g = rgb.b = v;
344 double min,max,delta,hue;
347 delta = (max * s)/255.0;
351 if(h > 300 || h <= 60)
357 hue = (hue - 360.0)/60.0;
358 rgb.b = (int)((hue * delta - min) * -1);
364 rgb.g = (int)(hue * delta + min);
367 else if(h > 60 && h < 180)
373 hue = (hue/60.0 - 2.0 ) * delta;
374 rgb.r = (int)(min - hue);
379 hue = (hue/60 - 2.0) * delta;
380 rgb.b = (int)(min + hue);
389 hue = (hue/60.0 - 4.0 ) * delta;
390 rgb.g = (int)(min - hue);
395 hue = (hue/60 - 4.0) * delta;
396 rgb.r = (int)(min + hue);
403 HSVType RGBType::toHSV()
407 double min,max,delta,temp;
409 min = __min(r,__min(g,b));
410 max = __max(r,__max(g,b));
421 hsv.s = (int)(temp*255);
425 temp = (double)(g-b)/delta;
430 temp = 2.0 + ((double)(b-r)/delta);
434 temp = 4.0 + ((double)(r-g)/delta);
450 /////////////////////////////////////////////////////////////////////////////
451 // CDialogColorPicker dialog
454 CDialogColorPicker::CDialogColorPicker( COLORREF c, CWnd* pParent /*=NULL*/)
455 : CDialog(CDialogColorPicker::IDD, pParent)
457 //{{AFX_DATA_INIT(CDialogColorPicker)
461 Vertex = CPoint(102,108);
463 Left = CPoint(23,147);
464 Right = CPoint(181,147);
466 color.r = GetRValue(c);
467 color.g = GetGValue(c);
468 color.b = GetBValue(c);
471 hsvColor = color.toHSV();
474 m_bInDrawAll = FALSE;
479 CDialogColorPicker::~CDialogColorPicker()
481 if(m_RgbBitmap.GetSafeHandle())
483 m_RgbBitmap.DeleteObject();
485 if(m_HsbBitmap.GetSafeHandle())
487 m_HsbBitmap.DeleteObject();
492 BEGIN_MESSAGE_MAP(CDialogColorPicker, CDialog)
493 //{{AFX_MSG_MAP(CDialogColorPicker)
497 ON_WM_SYSCOLORCHANGE()
499 ON_EN_CHANGE(IDC_EDIT_BLUE, OnChangeEditBlue)
500 ON_EN_CHANGE(IDC_EDIT_GREEN, OnChangeEditGreen)
501 ON_EN_CHANGE(IDC_EDIT_HUE, OnChangeEditHue)
502 ON_EN_CHANGE(IDC_EDIT_RED, OnChangeEditRed)
503 ON_EN_CHANGE(IDC_EDIT_SAT, OnChangeEditSat)
504 ON_EN_CHANGE(IDC_EDIT_VAL, OnChangeEditVal)
505 ON_EN_CHANGE(IDC_EDIT_OVERBRIGHT, OnChangeEditOverbright)
506 ON_BN_CLICKED(IDC_BTN_OLDCOLOR, OnBtnColor)
511 /////////////////////////////////////////////////////////////////////////////
512 // CDialogColorPicker message handlers
514 void CDialogColorPicker::OnLButtonDown(UINT nFlags, CPoint point) {
515 if(hsbRect.PtInRect(point)) {
517 if(InCircle(point)) {
518 m_nMouseIn = IN_CIRCLE;
519 } else if (InBright(point)) {
520 m_nMouseIn = IN_BRIGHT;
521 } else if (InOverBright(point)) {
522 m_nMouseIn = IN_OVERBRIGHT;
524 m_nMouseIn = IN_NOTHING;
532 else if (rgbRect.PtInRect(point)) {
533 m_nMouseIn = IN_NOTHING;
534 if(rects[RED].PtInRect(point)) {
538 } else if (rects[GREEN].PtInRect(point)) {
542 } else if (rects[BLUE].PtInRect(point)) {
549 CDialog::OnLButtonDown(nFlags, point);
552 void CDialogColorPicker::OnLButtonUp(UINT nFlags, CPoint point)
554 if(GetCapture() == this)
559 CDialog::OnLButtonUp(nFlags, point);
562 void CDialogColorPicker::OnMouseMove(UINT nFlags, CPoint point)
564 if(GetCapture() == this && m_nMouseIn)
568 else if(GetCapture() == this && m_bInMouse)
571 BOOL bChange = FALSE;
575 if(point.y > Vertex.y)
580 val = Distance(point,Vertex);
587 val = (val/RedLen)*255;
590 pt = PointOnLine(Vertex,Top,(color.r*RedLen)/255,RedLen);
591 rects[RED] = CRect(pt.x - RECT_WIDTH ,pt.y-RECT_WIDTH ,pt.x+RECT_WIDTH ,pt.y+RECT_WIDTH );
596 else if(nIndex == GREEN)
598 if(point.x > Vertex.x)
602 point.y = rects[GREEN].top + RECT_WIDTH;
603 val = Distance(point,Vertex);
610 val = (val/GreenLen)*255;
613 pt = PointOnLine(Vertex,Left,(color.g*GreenLen)/255,GreenLen);
614 rects[GREEN] = CRect(pt.x - RECT_WIDTH ,pt.y-RECT_WIDTH ,pt.x+RECT_WIDTH ,pt.y+RECT_WIDTH );
619 else if(nIndex == BLUE)
621 if(point.x < Vertex.x)
625 point.y = rects[BLUE].top + RECT_WIDTH;
626 val = Distance(point,Vertex);
633 val = (val/BlueLen)*255;
636 pt = PointOnLine(Vertex,Right,(color.b*GreenLen)/255,BlueLen);
637 rects[BLUE] = CRect(pt.x - RECT_WIDTH ,pt.y-RECT_WIDTH ,pt.x+RECT_WIDTH ,pt.y+RECT_WIDTH );
644 hsvColor = color.toHSV();
651 InvalidateRect(&brightRect,FALSE);
655 CDialog::OnMouseMove(nFlags, point);
658 void CDialogColorPicker::OnPaint()
660 CPaintDC dc(this); // device context for painting
666 BOOL CDialogColorPicker::OnInitDialog()
668 CDialog::OnInitDialog();
670 GetDlgItem(IDC_STATIC_RGB_RECT)->GetWindowRect(&rgbRect);
671 GetDlgItem(IDC_STATIC_HSB_RECT)->GetWindowRect(&hsbRect);
672 ScreenToClient(&rgbRect);
673 ScreenToClient(&hsbRect);
675 GetDlgItem(IDC_STATIC_NEWCOLOR)->GetWindowRect(&NewColorRect);
676 ScreenToClient(&NewColorRect);
684 memDC.CreateCompatibleDC(&dc);
686 LoadMappedBitmap(m_HsbBitmap,IDB_BITMAP_HSB,bmSize);
687 hsbWidth = bmSize.cx;
688 hsbHeight = bmSize.cy;
690 hsbRect.InflateRect(-5,-5);
694 m_Centre = CPoint(RADIUS,RADIUS);
695 m_Centre += CPoint(hsbRect.left,hsbRect.top);
697 brightRect = CRect(hsbRect.left+hsbWidth+20,hsbRect.top,hsbRect.left+hsbWidth+20+20,hsbRect.top + hsbHeight);
698 overBrightRect = brightRect;
699 overBrightRect.OffsetRect(brightRect.Width() + 5, 0);
708 LoadMappedBitmap(m_RgbBitmap,IDB_BITMAP_RGB,bmSize);
709 rgbWidth = bmSize.cx;
710 rgbHeight = bmSize.cy;
712 rgbRect.InflateRect(-5,-5);
716 CPoint pt = CPoint(rgbRect.left,rgbRect.top);
722 // TODO: Add your specialized code here and/or call the base class
724 RedLen = Distance(Vertex,Top);
725 GreenLen = Distance(Vertex,Left);
726 BlueLen = Distance(Vertex,Right);
736 SetTimer(0, 50, NULL);
738 return TRUE; // return TRUE unless you set the focus to a control
739 // EXCEPTION: OCX Property Pages should return FALSE
742 void CDialogColorPicker::DrawMarkers(CDC *pDC)
744 if(m_CurrentRect.Width())
749 CRect cr = m_CurrentRect;
751 oldPen = (CPen *)pDC->SelectStockObject(WHITE_PEN);
752 oldBrush = (CBrush *)pDC->SelectStockObject(NULL_BRUSH);
754 oldMode = pDC->SetROP2(R2_XORPEN);
758 pen.CreatePen(PS_SOLID,2,RGB(255,255,255));
759 pDC->SelectObject(&pen);
760 pDC->Rectangle(&brightMark);
762 pDC->SelectObject(oldPen);
763 pDC->SelectObject(oldBrush);
764 pDC->SetROP2(oldMode);
769 BOOL CDialogColorPicker::InCircle(CPoint pt)
771 return Distance(pt,m_Centre) <= RADIUS;
774 BOOL CDialogColorPicker::InBright(CPoint pt)
776 return brightRect.PtInRect(pt);
779 BOOL CDialogColorPicker::InOverBright(CPoint pt)
781 return overBrightRect.PtInRect(pt);
784 void CDialogColorPicker::TrackPoint(CPoint pt)
786 if(m_nMouseIn == IN_CIRCLE)
792 hsvColor.h = (int)RAD2DEG(AngleFromPoint(pt,m_Centre));
797 hsvColor.s = (int)SCALETOMAX(Distance(pt,m_Centre));
798 if(hsvColor.s > 255) hsvColor.s = 255;
804 InvalidateRect(&brightRect,FALSE);
808 color = hsvColor.toRGB();
815 else if(m_nMouseIn == IN_BRIGHT)
818 d = brightRect.bottom - pt.y;
820 d /= brightRect.Height();
829 color = hsvColor.toRGB();
837 void CDialogColorPicker::CreateBrightDIB()
839 CDIB& d = m_BrightDIB;
841 d.Create(brightRect.Width(),brightRect.Height(),8);
842 for(int i=0; i < d.Height(); i++)
844 memset(d.GetLinePtr(i),i,d.Width());
848 void CDialogColorPicker::SetDIBPalette()
850 BYTE palette[768],*p;
851 HSVType h = hsvColor;
854 d = 255.0/brightRect.Height();
856 for(int i=brightRect.Height()-1; i >= 0 ;i--,p+=3)
858 h.v = (int)((double)i * d);
859 RGBType rgb = h.toRGB();
864 m_BrightDIB.SetPalette(palette);
867 void CDialogColorPicker::CalcRects()
871 pt = PtFromAngle(hsvColor.h,hsvColor.s,m_Centre);
872 m_CurrentRect = CRect(pt.x - RECT_WIDTH,pt.y - RECT_WIDTH,pt.x+RECT_WIDTH,pt.y + RECT_WIDTH);
876 y = (int)(((double)hsvColor.v/255)*brightRect.Height());
877 y = brightRect.bottom - y;
878 brightMark = CRect(brightRect.left - 2, y - 4, brightRect.right+2,y+4);
882 void CDialogColorPicker::DrawHSB(CDC *pDC)
884 if(m_HsbBitmap.GetSafeHandle())
886 CBitmap *pOldBitmap ;
887 pOldBitmap = (CBitmap *)memDC.SelectObject(&m_HsbBitmap);
888 pDC->BitBlt(hsbRect.left,hsbRect.top,hsbWidth,hsbHeight,&memDC,0,0,SRCCOPY);
889 m_BrightDIB.BitBlt(pDC->m_hDC,brightRect.left,brightRect.top,brightRect.Width(),brightRect.Height(),0,0);
891 memDC.SelectObject(pOldBitmap);
895 void CDialogColorPicker::DrawRGB(CDC *pDC)
897 if(m_RgbBitmap.GetSafeHandle())
899 CBitmap *pOldBitmap ;
900 pOldBitmap = (CBitmap *)memDC.SelectObject(&m_RgbBitmap);
901 pDC->BitBlt(rgbRect.left,rgbRect.top,rgbWidth,rgbHeight,&memDC,0,0,SRCCOPY);
903 memDC.SelectObject(pOldBitmap);
907 void CDialogColorPicker::DrawLines(CDC *pDC)
911 pt[0] = PointOnLine(Vertex,Top,(color.r*RedLen)/255,RedLen);
912 pt[1] = PointOnLine(Vertex,Left,(color.g*GreenLen)/255,GreenLen);
913 pt[2] = PointOnLine(Vertex,Right,(color.b*BlueLen)/255,BlueLen);
915 COLORREF col = RGB(255,255,255);
918 for(int i = 0; i < 3; i++ ) {
919 cr = CRect(pt[i].x - RECT_WIDTH ,pt[i].y-RECT_WIDTH ,pt[i].x+RECT_WIDTH ,pt[i].y+RECT_WIDTH );
927 oldPen = (CPen *)pDC->SelectStockObject(WHITE_PEN);
928 oldMode = pDC->SetROP2(R2_XORPEN);
931 Draw the following lines :
942 pDC->MoveTo(m_Cuboid[1]);
943 pDC->LineTo(m_Cuboid[2]);
944 pDC->LineTo(m_Cuboid[3]);
945 pDC->LineTo(m_Cuboid[4]);
946 pDC->LineTo(m_Cuboid[5]);
947 pDC->LineTo(m_Cuboid[2]);
949 pDC->MoveTo(m_Cuboid[5]);
950 pDC->LineTo(m_Cuboid[6]);
951 pDC->LineTo(m_Cuboid[7]);
952 pDC->LineTo(m_Cuboid[4]);
954 pDC->MoveTo(m_Cuboid[1]);
955 pDC->LineTo(m_Cuboid[6]);
957 pDC->SelectObject(oldPen);
958 pDC->SetROP2(oldMode);
960 DrawFilledColor(pDC,NewColorRect,color.color());
963 void CDialogColorPicker::DrawXorRect(CDC *pDC,CRect& cr)
969 pen.CreatePen(PS_SOLID,1,RGB(255,255,255));
970 oldPen = (CPen *)pDC->SelectObject(&pen);
971 oldBrush = (CBrush *)pDC->SelectStockObject(NULL_BRUSH);
972 oldMode =pDC->SetROP2(R2_XORPEN);
974 pDC->SetROP2(oldMode);
975 pDC->SelectObject(oldPen);
976 pDC->SelectObject(oldBrush);
981 void CDialogColorPicker::CalcSlopes()
983 lines[RED].slope = Slope(Top,Vertex);
984 lines[GREEN].slope = Slope(Left,Vertex);
985 lines[BLUE].slope = Slope(Right,Vertex);
989 for( i = 0; i < 3; i++ ) {
990 lines[i].x = Vertex.x;
991 lines[i].y = Vertex.y;
992 lines[i].c = FindC(lines[i]);
1001 2 = Red Green Intersection
1003 4 = Blue Green Intersection
1004 5 = Blue Green Red Intersection
1005 6 = Red Blue Intersection
1008 Draw the following lines :
1020 void CDialogColorPicker::CalcCuboid()
1022 double rLen,gLen,bLen;
1024 rLen = (double)(color.r*RedLen)/255;
1025 gLen = (double)(color.g*GreenLen)/255;
1026 bLen = (double)(color.b*BlueLen)/255;
1030 m_Cuboid[0] = Vertex;
1031 m_Cuboid[1] = PointOnLine(Vertex,Top,(int)rLen,RedLen);
1032 m_Cuboid[3] = PointOnLine(Vertex,Left,(int)gLen,GreenLen);
1033 m_Cuboid[7] = PointOnLine(Vertex,Right,(int)bLen,BlueLen);
1036 l[1] = lines[GREEN];
1039 l[3].slope = lines[GREEN].slope;
1040 l[3].x = m_Cuboid[1].x;
1041 l[3].y = m_Cuboid[1].y;
1042 l[3].c = FindC(l[3]);
1044 l[4].slope = lines[RED].slope;
1045 l[4].x = m_Cuboid[3].x;
1046 l[4].y = m_Cuboid[3].y;
1047 l[4].c = FindC(l[4]);
1049 l[5].slope = lines[BLUE].slope;
1050 l[5].x = m_Cuboid[3].x;
1051 l[5].y = m_Cuboid[3].y;
1052 l[5].c = FindC(l[5]);
1054 l[6].slope = lines[GREEN].slope;
1055 l[6].x = m_Cuboid[7].x;
1056 l[6].y = m_Cuboid[7].y;
1057 l[6].c = FindC(l[6]);
1059 l[10].slope = lines[BLUE].slope;
1060 l[10].x = m_Cuboid[1].x;
1061 l[10].y = m_Cuboid[1].y;
1062 l[10].c = FindC(l[10]);
1064 l[11].slope = lines[RED].slope;
1065 l[11].x = m_Cuboid[7].x;
1066 l[11].y = m_Cuboid[7].y;
1067 l[11].c = FindC(l[11]);
1069 m_Cuboid[2] = Intersection(l[3],l[4]);
1070 m_Cuboid[4] = Intersection(l[5],l[6]);
1071 m_Cuboid[6] = Intersection(l[10],l[11]);
1073 l[7].slope = lines[RED].slope;
1074 l[7].x = m_Cuboid[4].x;
1075 l[7].y = m_Cuboid[4].y;
1076 l[7].c = FindC(l[7]);
1078 l[8].slope = lines[BLUE].slope;
1079 l[8].x = m_Cuboid[2].x;
1080 l[8].y = m_Cuboid[2].y;
1081 l[8].c = FindC(l[8]);
1083 m_Cuboid[5] = Intersection(l[7],l[8]);
1087 void CDialogColorPicker::SetSpinVals()
1089 ((CSpinButtonCtrl *)GetDlgItem(IDC_SPIN_RED))->SetRange(0,255);
1090 ((CSpinButtonCtrl *)GetDlgItem(IDC_SPIN_GREEN))->SetRange(0,255);
1091 ((CSpinButtonCtrl *)GetDlgItem(IDC_SPIN_BLUE))->SetRange(0,255);
1093 ((CSpinButtonCtrl *)GetDlgItem(IDC_SPIN_HUE))->SetRange(0,360);
1094 ((CSpinButtonCtrl *)GetDlgItem(IDC_SPIN_SAT))->SetRange(0,255);
1095 ((CSpinButtonCtrl *)GetDlgItem(IDC_SPIN_VAL))->SetRange(0,255);
1097 ((CSpinButtonCtrl *)GetDlgItem(IDC_SPIN_OVERBRIGHT))->SetRange(0,1023);
1101 void CDialogColorPicker::SetEditVals()
1103 ((CSpinButtonCtrl *)GetDlgItem(IDC_SPIN_RED))->SetPos(color.r);
1104 ((CSpinButtonCtrl *)GetDlgItem(IDC_SPIN_GREEN))->SetPos(color.g);
1105 ((CSpinButtonCtrl *)GetDlgItem(IDC_SPIN_BLUE))->SetPos(color.b);
1107 ((CSpinButtonCtrl *)GetDlgItem(IDC_SPIN_HUE))->SetPos(hsvColor.h);
1108 ((CSpinButtonCtrl *)GetDlgItem(IDC_SPIN_SAT))->SetPos(hsvColor.s);
1109 ((CSpinButtonCtrl *)GetDlgItem(IDC_SPIN_VAL))->SetPos(hsvColor.v);
1113 void CDialogColorPicker::OnChangeEditBlue()
1117 b = GetDlgItemInt(IDC_EDIT_BLUE);
1118 if( b != color.b && m_bInitOver)
1121 if(color.b < 0) color.b = 0;
1122 if(color.b > 255) color.b = 255;
1123 hsvColor = color.toHSV();
1128 void CDialogColorPicker::OnChangeEditGreen()
1132 g = GetDlgItemInt(IDC_EDIT_GREEN);
1133 if(g != color.g && m_bInitOver)
1136 if(color.g < 0) color.g = 0;
1137 if(color.g > 255) color.g = 255;
1138 hsvColor = color.toHSV();
1143 void CDialogColorPicker::OnChangeEditRed()
1147 r = GetDlgItemInt(IDC_EDIT_RED);
1148 if(r != color.r && m_bInitOver)
1151 if(color.r < 0) color.r = 0;
1152 if(color.r > 255) color.r = 255;
1153 hsvColor = color.toHSV();
1158 void CDialogColorPicker::OnChangeEditHue()
1162 h = GetDlgItemInt(IDC_EDIT_HUE);
1163 if(h != hsvColor.h && m_bInitOver)
1166 if(hsvColor.h < 0) hsvColor.h = 0;
1167 if(hsvColor.h > 359) hsvColor.h = 359;
1168 color = hsvColor.toRGB();
1173 void CDialogColorPicker::OnChangeEditSat()
1177 s = GetDlgItemInt(IDC_EDIT_SAT);
1178 if(s != hsvColor.s && m_bInitOver)
1181 if(hsvColor.s < 0) hsvColor.s = 0;
1182 if(hsvColor.s > 255) hsvColor.s = 255;
1183 color = hsvColor.toRGB();
1188 void CDialogColorPicker::OnChangeEditVal()
1192 v = GetDlgItemInt(IDC_EDIT_VAL);
1193 if(v != hsvColor.v && m_bInitOver)
1196 if(hsvColor.v < 0) hsvColor.v = 0;
1197 if(hsvColor.v > 255) hsvColor.v = 255;
1198 color = hsvColor.toRGB();
1203 void CDialogColorPicker::OnChangeEditOverbright() {
1205 GetDlgItemText(IDC_EDIT_OVERBRIGHT, str);
1207 overBright = atof(str);
1211 void CDialogColorPicker::DrawAll()
1213 if(m_bInitOver && !m_bInDrawAll)
1219 m_bInDrawAll = TRUE;
1226 m_bInDrawAll = FALSE;
1230 void CDialogColorPicker::DrawFilledColor(CDC *pDC,CRect cr,COLORREF c)
1232 pDC->FillSolidRect(&cr,c);
1233 pDC->Draw3dRect(&cr,RGB(0,0,0),RGB(0,0,0));
1234 cr.InflateRect(-1,-1);
1235 pDC->Draw3dRect(&cr,RGB(192,192,192),RGB(128,128,128));
1238 void CDialogColorPicker::LoadMappedBitmap(CBitmap& bitmap,UINT nIdResource,CSize& size)
1240 CBitmap *pOldBitmap;
1242 if(bitmap.GetSafeHandle()) bitmap.DeleteObject();
1244 if(bitmap.LoadBitmap(nIdResource))
1250 ::GetObject(bitmap.m_hObject,sizeof(bmInfo),&bmInfo);
1251 width = bmInfo.bmWidth;
1252 height = bmInfo.bmHeight;
1254 COLORREF colorWindow = ::GetSysColor(COLOR_3DFACE);
1255 COLORREF sourceColor = RGB(192,192,192);
1257 pOldBitmap = (CBitmap *)memDC.SelectObject(&bitmap);
1261 for(i=0; i < height; i++)
1263 for(j=0; j < width; j++)
1265 if(memDC.GetPixel(j,i) == sourceColor)
1267 memDC.SetPixel(j,i,colorWindow);
1272 memDC.SelectObject(&pOldBitmap);
1273 size = CSize(width,height);
1277 void CDialogColorPicker::OnSysColorChange()
1280 LoadMappedBitmap(m_HsbBitmap,IDB_BITMAP_HSB,size);
1281 LoadMappedBitmap(m_RgbBitmap,IDB_BITMAP_RGB,size);
1284 void CDialogColorPicker::OnTimer(UINT nIDEvent) {
1285 if ( UpdateParent ) {
1286 UpdateParent( color.r, color.g, color.b, 1.0f );
1290 void CDialogColorPicker::OnBtnColor() {
1291 COLORREF cr = DoOldColor(GetColor());
1292 color.r = GetRValue(cr);
1293 color.g = GetGValue(cr);
1294 color.b = GetBValue(cr);
1295 hsvColor = color.toHSV();
1299 bool DoNewColor( int* i1, int* i2, int* i3, float *overBright, void (*Update)( float, float, float, float ) ) {
1300 COLORREF cr = (*i1) + ((*i2) <<8) + ((*i3) <<16);
1301 CDialogColorPicker dlg( cr );
1302 //CMyColorDialog dlg(cr, CC_FULLOPEN | CC_RGBINIT | CC_ANYCOLOR);
1304 dlg.UpdateParent = Update;
1306 if ( dlg.DoModal() == IDOK ) {
1307 *i1 = (dlg.GetColor() & 255);
1308 *i2 = ((dlg.GetColor() >> 8) & 255);
1309 *i3 = ((dlg.GetColor() >> 16) & 255);
1310 *overBright = dlg.GetOverBright();