summaryrefslogtreecommitdiff
path: root/vgui2/matsys_controls/colorpickerpanel.cpp
blob: 848f78a574fe6943ca0a22d6c443931f97b5d345 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//===========================================================================//

#include "matsys_controls/colorpickerpanel.h"
#include "matsys_controls/matsyscontrols.h"
#include "matsys_controls/proceduraltexturepanel.h"
#include "materialsystem/imaterialsystem.h"
#include "materialsystem/itexture.h"
#include "pixelwriter.h"
#include "VGuiMatSurface/IMatSystemSurface.h"
#include "vgui_controls/Button.h"
#include "vgui_controls/TextEntry.h"
#include "vgui_controls/RadioButton.h"
#include "vgui/IInput.h"
#include "tier1/KeyValues.h"
#include "bitmap/imageformat.h"

using namespace vgui;


//-----------------------------------------------------------------------------
// Color picker
//-----------------------------------------------------------------------------
enum ColorType_t
{
	COLOR_TYPE_RGB = 0,
	COLOR_TYPE_HSV,
};

enum ColorChannel_t
{
	CHANNEL_RED = 0,
	CHANNEL_GREEN,
	CHANNEL_BLUE,

	CHANNEL_HUE = 0,
	CHANNEL_SATURATION,
	CHANNEL_VALUE,
};


//-----------------------------------------------------------------------------
// Converts RGB to normalized
//-----------------------------------------------------------------------------
static void RGB888ToVector( RGB888_t inColor, Vector *pOutVector )
{
	pOutVector->Init( inColor.r / 255.0f, inColor.g / 255.0f, inColor.b / 255.0f ); 
}

static void VectorToRGB888( const Vector &inVector, RGB888_t &outColor )
{
	int r = (int)((inVector.x * 255.0f) + 0.5f);
	int g = (int)((inVector.y * 255.0f) + 0.5f);
	int b = (int)((inVector.z * 255.0f) + 0.5f);
	outColor.r = clamp( r, 0, 255 );
	outColor.g = clamp( g, 0, 255 );
	outColor.b = clamp( b, 0, 255 );
}


//-----------------------------------------------------------------------------
// Convert RGB to HSV
//-----------------------------------------------------------------------------
static inline void RGBtoHSV( const RGB888_t &rgb, Vector &hsv )
{
	Vector vecRGB;
	RGB888ToVector( rgb, &vecRGB );
	RGBtoHSV( vecRGB, hsv );
}


//-----------------------------------------------------------------------------
// Convert HSV to RGB
//-----------------------------------------------------------------------------
static inline void HSVtoRGB( const Vector &hsv, RGB888_t &rgb )
{
	Vector vecRGB;
	HSVtoRGB( hsv, vecRGB );
	VectorToRGB888( vecRGB, rgb );
}


//-----------------------------------------------------------------------------
// This previews the 'xy' color
//-----------------------------------------------------------------------------
class CColorXYPreview : public CProceduralTexturePanel
{
	DECLARE_CLASS_SIMPLE( CColorXYPreview, CProceduralTexturePanel );

public:
	// constructor
	CColorXYPreview( vgui::Panel *pParent, const char *pName );

	virtual void RegenerateTextureBits( ITexture *pTexture, IVTFTexture *pVTFTexture, Rect_t *pRect );
 	virtual void Paint( void );
	virtual void OnMousePressed( vgui::MouseCode code );
	virtual void OnMouseReleased( vgui::MouseCode code );
	virtual void OnCursorMoved( int x, int y );

	void SetMode( ColorType_t type, ColorChannel_t channel );
	void SetColor( const RGB888_t &color, const Vector &hsvColor );

private:
	// Computes a color given a particular x,y value 
	void ComputeColorForPoint( int x, int y, RGB888_t &color );
	void ComputeHSVColorForPoint( int x, int y, Vector &vscHSV );

	// Updates the color based on the mouse position
	void UpdateColorFromMouse( int x, int y );

	static ColorChannel_t s_pHSVRemapX[3];
	static ColorChannel_t s_pHSVRemapY[3];
	static ColorChannel_t s_pRGBRemapX[3];
	static ColorChannel_t s_pRGBRemapY[3];

	ColorType_t m_Type;
	ColorChannel_t m_Channel;
	RGB888_t m_CurrentColor;
	Vector m_CurrentHSVColor;
	vgui::HCursor m_hPickerCursor;
	bool m_bDraggingMouse;
};


ColorChannel_t CColorXYPreview::s_pHSVRemapX[3] =
{
	CHANNEL_SATURATION, CHANNEL_HUE, CHANNEL_HUE
};

ColorChannel_t CColorXYPreview::s_pHSVRemapY[3] = 
{
	CHANNEL_VALUE, CHANNEL_VALUE, CHANNEL_SATURATION
};

ColorChannel_t CColorXYPreview::s_pRGBRemapX[3] =
{
	CHANNEL_BLUE, CHANNEL_BLUE, CHANNEL_RED
};

ColorChannel_t CColorXYPreview::s_pRGBRemapY[3] = 
{
	CHANNEL_GREEN, CHANNEL_RED, CHANNEL_GREEN
};

	
//-----------------------------------------------------------------------------
// Constructor
//-----------------------------------------------------------------------------
CColorXYPreview::CColorXYPreview( vgui::Panel *pParent, const char *pName ) : BaseClass( pParent, pName )
{
	Init( 256, 256, false );
	m_CurrentColor.r = m_CurrentColor.g = m_CurrentColor.b = 255;
	SetMode( COLOR_TYPE_HSV, CHANNEL_HUE );
	SetMouseInputEnabled( true );
	m_hPickerCursor = surface()->CreateCursorFromFile( "resource/colorpicker.cur" );
	SetCursor( m_hPickerCursor );
	m_bDraggingMouse = false;
}

	
//-----------------------------------------------------------------------------
// Sets the mode for the preview
//-----------------------------------------------------------------------------
void CColorXYPreview::SetMode( ColorType_t type, ColorChannel_t channel )
{
	if ( m_Type != type || m_Channel != channel )
	{
		m_Type = type;
		m_Channel = channel;
		DownloadTexture();
	}
}

void CColorXYPreview::SetColor( const RGB888_t &color, const Vector &hsvColor )
{
	if ( color != m_CurrentColor || m_CurrentHSVColor != hsvColor )
	{
		m_CurrentColor = color;
		m_CurrentHSVColor = hsvColor;
		DownloadTexture();
	}
}

	
//-----------------------------------------------------------------------------
// Computes a color given a particular x,y value 
//-----------------------------------------------------------------------------
void CColorXYPreview::ComputeColorForPoint( int x, int y, RGB888_t &color )
{
	color = m_CurrentColor;
	((unsigned char*)&color)[ s_pRGBRemapX[m_Channel] ] = x;
	((unsigned char*)&color)[ s_pRGBRemapY[m_Channel] ] = GetImageHeight() - y - 1;
}

void CColorXYPreview::ComputeHSVColorForPoint( int x, int y, Vector &vscHSV )
{
	vscHSV = m_CurrentHSVColor;
	vscHSV[ s_pHSVRemapX[m_Channel] ] = (float)x / 255.0f;
	vscHSV[ s_pHSVRemapY[m_Channel] ] = (float)(GetImageHeight() - y - 1) / 255.0f;
	if ( vscHSV.y == 0.0f )
	{
		vscHSV.x = -1.0f;
	}

	if ( m_Channel != CHANNEL_HUE )
	{
		if ( vscHSV.x != -1.0f )
		{
			vscHSV.x *= 360.0f;
		}
	}
}


//-----------------------------------------------------------------------------
// Fills the texture w/ the image buffer 
//-----------------------------------------------------------------------------
void CColorXYPreview::RegenerateTextureBits( ITexture *pTexture, IVTFTexture *pVTFTexture, Rect_t *pRect )
{
	Assert( pVTFTexture->FrameCount() == 1 );
	Assert( pVTFTexture->FaceCount() == 1 );
	Assert( !pTexture->IsMipmapped() );

	int nWidth, nHeight, nDepth;
	pVTFTexture->ComputeMipLevelDimensions( 0, &nWidth, &nHeight, &nDepth );
	Assert( nDepth == 1 );
	Assert( nWidth == m_nWidth && nHeight == m_nHeight );

	CPixelWriter pixelWriter;
	pixelWriter.SetPixelMemory( pVTFTexture->Format(), 
		pVTFTexture->ImageData( 0, 0, 0 ), pVTFTexture->RowSizeInBytes( 0 ) );

	for ( int y = 0; y < nHeight; ++y )
	{
		pixelWriter.Seek( 0, y );

		for ( int x = 0; x < nWidth; ++x )
		{
			RGB888_t color;
			if ( m_Type != COLOR_TYPE_RGB )
			{
				Vector vecHSV;
				ComputeHSVColorForPoint( x, y, vecHSV );
				HSVtoRGB( vecHSV, color );
			}
			else
			{
				ComputeColorForPoint( x, y, color );
			}

			pixelWriter.WritePixel( color.r, color.g, color.b, 255 );
		}
	}
}


//-----------------------------------------------------------------------------
// Paints a circle over the currently selected color
//-----------------------------------------------------------------------------
void CColorXYPreview::Paint( void )
{
	BaseClass::Paint();

	int x, y;
	if ( m_Type != COLOR_TYPE_RGB )
	{
		Vector vecHSVNormalized = m_CurrentHSVColor;
		if ( vecHSVNormalized.x != -1.0f )
		{
			vecHSVNormalized.x *= 255.0f / 360.0f;
		}
		vecHSVNormalized.y *= 255.0f;
		vecHSVNormalized.z *= 255.0f;

		x = (int)( vecHSVNormalized[ s_pHSVRemapX[m_Channel] ] + 0.5f);
		y = GetImageHeight() - 1 - (int)( vecHSVNormalized[ s_pHSVRemapY[m_Channel] ] + 0.5f );
	}
	else
	{
		x = ((unsigned char*)&m_CurrentColor)[ s_pRGBRemapX[m_Channel] ];
		y = GetImageHeight() - 1 - ((unsigned char*)&m_CurrentColor)[ s_pRGBRemapY[m_Channel] ];
	}
			   
	// Renormalize x, y to actual size
	int w, h;
	GetSize( w, h );
	x = (int)( w * (float)x / 255.0f + 0.5f );
	y = (int)( h * (float)y / 255.0f + 0.5f );
	vgui::surface()->DrawSetColor( 255, 255, 255, 255 );
	vgui::surface()->DrawOutlinedCircle( x, y, 5, 8 );
	vgui::surface()->DrawSetColor( 0, 0, 0, 255 );
	vgui::surface()->DrawOutlinedCircle( x, y, 6, 8 );
}


//-----------------------------------------------------------------------------
// Updates the color based on the mouse position
//-----------------------------------------------------------------------------
void CColorXYPreview::UpdateColorFromMouse( int x, int y )
{
	int w, h;
	GetSize( w, h );

	float flNormalizedX = (float)x / (w-1);
	float flNormalizedY = (float)y / (h-1);
	flNormalizedX = clamp( flNormalizedX, 0.0f, 1.0f );
	flNormalizedY = clamp( flNormalizedY, 0.0f, 1.0f );

	int tx = (int)( (GetImageWidth()-1) * flNormalizedX + 0.5f );
	int ty = (int)( (GetImageHeight()-1) * flNormalizedY + 0.5f );
	if ( m_Type != COLOR_TYPE_RGB )
	{
		Vector vecHSV;
		ComputeHSVColorForPoint( tx, ty, vecHSV );

		KeyValues *pKeyValues = new KeyValues( "HSVSelected" );
		pKeyValues->SetFloat( "hue", vecHSV.x );
		pKeyValues->SetFloat( "saturation", vecHSV.y );
		pKeyValues->SetFloat( "value", vecHSV.z );
		PostActionSignal( pKeyValues );
		
		// This prevents a 1-frame lag in the current color position
		RGB888_t color;
		HSVtoRGB( vecHSV, color );
		SetColor( color, vecHSV );
	}
	else
	{
		RGB888_t color;
		ComputeColorForPoint( tx, ty, color );

		Color c( color.r, color.g, color.b, 255 );
		KeyValues *pKeyValues = new KeyValues( "ColorSelected" );
		pKeyValues->SetColor( "color", c );
		PostActionSignal( pKeyValues );

		// This prevents a 1-frame lag in the current color position
		Vector vecHSV;
		RGBtoHSV( color, vecHSV );
		SetColor( color, vecHSV );
	}
}


//-----------------------------------------------------------------------------
// Handle input
//-----------------------------------------------------------------------------
void CColorXYPreview::OnMousePressed( vgui::MouseCode code )
{
	BaseClass::OnMousePressed( code );

	if ( code == MOUSE_LEFT )
	{
		if ( !m_bDraggingMouse )
		{
			m_bDraggingMouse = true;
			input()->SetMouseCapture(GetVPanel());

			int x, y;
			input()->GetCursorPos( x, y );
			ScreenToLocal( x, y );

			UpdateColorFromMouse( x, y );
		}
	}
}

void CColorXYPreview::OnMouseReleased( vgui::MouseCode code )
{
	BaseClass::OnMouseReleased( code );

	if ( code == MOUSE_LEFT )
	{
		if ( m_bDraggingMouse )
		{
			m_bDraggingMouse = false;
			input()->SetMouseCapture( (VPANEL)0 );
		}
	}
}

void CColorXYPreview::OnCursorMoved( int x, int y )
{
	BaseClass::OnCursorMoved( x, y );

	if ( m_bDraggingMouse )
	{
		UpdateColorFromMouse( x, y );
	}
}


//-----------------------------------------------------------------------------
// This previews the 'z' color
//-----------------------------------------------------------------------------
class CColorZPreview : public CProceduralTexturePanel
{
	DECLARE_CLASS_SIMPLE( CColorZPreview, CProceduralTexturePanel );

public:
	// constructor
	CColorZPreview( vgui::Panel *pParent, const char *pName );

	virtual void RegenerateTextureBits( ITexture *pTexture, IVTFTexture *pVTFTexture, Rect_t *pRect );
 	virtual void PerformLayout();
	virtual void Paint( void );
	virtual void OnCursorMoved( int x,int y );
	virtual void OnMousePressed( vgui::MouseCode code );
	virtual void OnMouseReleased( vgui::MouseCode code );

	void SetMode( ColorType_t type, ColorChannel_t channel );
	void SetColor( const RGB888_t &color, const Vector &hsvColor );

	// Computes a color given a particular x,y value 
	void ComputeColorForPoint( int y, RGB888_t &color );
	void ComputeHSVColorForPoint( int y, bool bProceduralTexture, Vector &vecHSV );

private:
	// Updates the color based on the mouse position
	void UpdateColorFromMouse( int x, int y );

	ColorType_t m_Type;
	ColorChannel_t m_Channel;
	RGB888_t m_CurrentColor;
	Vector m_CurrentHSVColor;
	bool m_bDraggingMouse;
};

#define MARKER_WIDTH 6


//-----------------------------------------------------------------------------
// Constructor
//-----------------------------------------------------------------------------
CColorZPreview::CColorZPreview( vgui::Panel *pParent, const char *pName ) : BaseClass( pParent, pName )
{
	Init( 8, 256, false );
	m_CurrentColor.r = m_CurrentColor.g = m_CurrentColor.b = 255;

	Vector vecRGB;
	RGB888ToVector( m_CurrentColor, &vecRGB );
	RGBtoHSV( vecRGB, m_CurrentHSVColor );
    m_bDraggingMouse = false;

	SetMouseInputEnabled( true );
	SetMode( COLOR_TYPE_HSV, CHANNEL_HUE );
}

	
//-----------------------------------------------------------------------------
// Sets the mode for the preview
//-----------------------------------------------------------------------------
void CColorZPreview::SetMode( ColorType_t type, ColorChannel_t channel )
{
	if ( m_Type != type || m_Channel != channel )
	{
		m_Type = type;
		m_Channel = channel;
		DownloadTexture();
	}
}

void CColorZPreview::SetColor( const RGB888_t &color, const Vector &hsvColor )
{
	if ( color != m_CurrentColor || m_CurrentHSVColor != hsvColor )
	{
		m_CurrentColor = color;
		m_CurrentHSVColor = hsvColor;
		DownloadTexture();
	}
}

	
//-----------------------------------------------------------------------------
// Lays out the panel
//-----------------------------------------------------------------------------
void CColorZPreview::PerformLayout()
{
	BaseClass::PerformLayout();

	int w, h;
	GetSize( w, h );
	Rect_t r;
	r.x = MARKER_WIDTH;
	r.y = MARKER_WIDTH;
	r.width = w - (MARKER_WIDTH*2);
	r.height = h - (MARKER_WIDTH*2);

	SetPaintRect( &r );
}

	
//-----------------------------------------------------------------------------
// Computes a color given a particular x,y value 
//-----------------------------------------------------------------------------
void CColorZPreview::ComputeColorForPoint( int y, RGB888_t &color )
{
	color = m_CurrentColor;
	((unsigned char*)&color)[ m_Channel ] = GetImageHeight() - y - 1;
}

void CColorZPreview::ComputeHSVColorForPoint( int y, bool bProceduralTexture, Vector &vecHSV )
{
	vecHSV = m_CurrentHSVColor;
	vecHSV[ m_Channel ] = (float)(GetImageHeight() - y - 1) / 255.0f;
	if ( m_Channel == CHANNEL_HUE )
	{
		if ( vecHSV.x != -1.0f )
		{
			vecHSV.x *= 360.0f;
		}

		if ( bProceduralTexture )
		{
			vecHSV.y = 1.0f;
			vecHSV.z = 1.0f;
		}
	}
}


//-----------------------------------------------------------------------------
// Fills the texture w/ the image buffer 
//-----------------------------------------------------------------------------
void CColorZPreview::RegenerateTextureBits( ITexture *pTexture, IVTFTexture *pVTFTexture, Rect_t *pRect )
{
	Assert( pVTFTexture->FrameCount() == 1 );
	Assert( pVTFTexture->FaceCount() == 1 );
	Assert( !pTexture->IsMipmapped() );

	int nWidth, nHeight, nDepth;
	pVTFTexture->ComputeMipLevelDimensions( 0, &nWidth, &nHeight, &nDepth );
	Assert( nDepth == 1 );
	Assert( nWidth == m_nWidth && nHeight == m_nHeight );

	CPixelWriter pixelWriter;
	pixelWriter.SetPixelMemory( pVTFTexture->Format(), 
		pVTFTexture->ImageData( 0, 0, 0 ), pVTFTexture->RowSizeInBytes( 0 ) );

	for ( int y = 0; y < nHeight; ++y )
	{
		pixelWriter.Seek( 0, y );

		RGB888_t color;
		if ( m_Type != COLOR_TYPE_RGB )
		{
			Vector vecHSV;
			ComputeHSVColorForPoint( y, true, vecHSV );
			HSVtoRGB( vecHSV, color );
		}
		else
		{
			ComputeColorForPoint( y, color );
		}

		for ( int x = 0; x < nWidth; ++x )
		{
			pixelWriter.WritePixel( color.r, color.g, color.b, 255 );
		}
	}
}


//-----------------------------------------------------------------------------
// Updates the color based on the mouse position
//-----------------------------------------------------------------------------
void CColorZPreview::UpdateColorFromMouse( int x, int y )
{
	int w, h;
	GetSize( w, h );
	h -= 2 * MARKER_WIDTH;

	float flNormalizedY = (float)( y - MARKER_WIDTH ) / (h-1);
	flNormalizedY = clamp( flNormalizedY, 0.0f, 1.0f );

	int ty = (int)( (GetImageHeight() - 1) * flNormalizedY + 0.5f );
	if ( m_Type != COLOR_TYPE_RGB )
	{
		Vector vecHSV;
		ComputeHSVColorForPoint( ty, false, vecHSV );

		KeyValues *pKeyValues = new KeyValues( "HSVSelected" );
		pKeyValues->SetFloat( "hue", vecHSV.x );
		pKeyValues->SetFloat( "saturation", vecHSV.y );
		pKeyValues->SetFloat( "value", vecHSV.z );
		PostActionSignal( pKeyValues );
		
		// This prevents a 1-frame lag in the current color position
		RGB888_t color;
		HSVtoRGB( vecHSV, color );
		SetColor( color, vecHSV );
	}
	else
	{
		RGB888_t color;
		ComputeColorForPoint( ty, color );

		Color c( color.r, color.g, color.b, 255 );
		KeyValues *pKeyValues = new KeyValues( "ColorSelected" );
		pKeyValues->SetColor( "color", c );
		PostActionSignal( pKeyValues );

		// This prevents a 1-frame lag in the current color position
		Vector vecHSV;
		RGBtoHSV( color, vecHSV );
		SetColor( color, vecHSV );
	}
}


//-----------------------------------------------------------------------------
// Handle input
//-----------------------------------------------------------------------------
void CColorZPreview::OnMousePressed( vgui::MouseCode code )
{
	BaseClass::OnMousePressed( code );

	if ( code == MOUSE_LEFT )
	{
		if ( !m_bDraggingMouse )
		{
			m_bDraggingMouse = true;
			input()->SetMouseCapture(GetVPanel());

			int x, y;
			input()->GetCursorPos( x, y );
			ScreenToLocal( x, y );

			UpdateColorFromMouse( x, y );
		}
	}
}

void CColorZPreview::OnMouseReleased( vgui::MouseCode code )
{
	BaseClass::OnMouseReleased( code );

	if ( code == MOUSE_LEFT )
	{
		if ( m_bDraggingMouse )
		{
			m_bDraggingMouse = false;
			input()->SetMouseCapture( (VPANEL)0 );
		}
	}
}

void CColorZPreview::OnCursorMoved( int x, int y )
{
	BaseClass::OnCursorMoved( x, y );

	if ( m_bDraggingMouse )
	{
		UpdateColorFromMouse( x, y );
	}
}


//-----------------------------------------------------------------------------
// Paints the panel (the two arrows, specifically)
//-----------------------------------------------------------------------------
void CColorZPreview::Paint( void )
{
	BaseClass::Paint();

	int y;
	if ( m_Type != COLOR_TYPE_RGB )
	{
		Vector vecHSVNormalized = m_CurrentHSVColor;
		if ( vecHSVNormalized.x != -1.0f )
		{
			vecHSVNormalized.x *= 255.0f / 360.0f;
		}
		vecHSVNormalized.y *= 255.0f;
		vecHSVNormalized.z *= 255.0f;

		y = GetImageHeight() - 1 - (int)( vecHSVNormalized[ m_Channel ] + 0.5f );
	}
	else
	{
		y = GetImageHeight() - 1 - ((unsigned char*)&m_CurrentColor)[ m_Channel ];
	}

	// Renormalize y to actual size
	int w, h;
	GetSize( w, h );
	h -= 2 * MARKER_WIDTH;
	y = (int)( h * (float)y / 255.0f + 0.5f );

	vgui::surface()->DrawSetColor( 255, 255, 255, 255 );

	int px[3] = { 0, 0, MARKER_WIDTH };
	int py[3] = { MARKER_WIDTH + y - MARKER_WIDTH, MARKER_WIDTH + y + MARKER_WIDTH, MARKER_WIDTH + y };
	vgui::surface()->DrawPolyLine( px, py, 3 );

	px[0] = px[1] = w-1;
	px[2] = w - 1 - MARKER_WIDTH;
	vgui::surface()->DrawPolyLine( px, py, 3 );
}


//-----------------------------------------------------------------------------
//
// Color picker panel
//
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// constructor
//-----------------------------------------------------------------------------
CColorPickerPanel::CColorPickerPanel( vgui::Panel *pParent, const char *pName ) :
	BaseClass( pParent, pName )
{
	m_pColorXYPreview = new CColorXYPreview( this, "ColorXYPreview" );
	m_pColorZPreview = new CColorZPreview( this, "ColorZPreview" );
	m_pColorXYPreview->AddActionSignalTarget( this );

	m_pHueRadio = new RadioButton( this, "HueRadio", "H" );
	m_pSaturationRadio = new RadioButton( this, "SaturationRadio", "S" );
	m_pValueRadio = new RadioButton( this, "ValueRadio", "V" );
	m_pRedRadio = new RadioButton( this, "RedRadio", "R" );
	m_pGreenRadio = new RadioButton( this, "GreenRadio", "G" );
	m_pBlueRadio = new RadioButton( this, "BlueRadio", "B" );

	m_pHueText = new TextEntry( this, "HueText" );
	m_pSaturationText = new TextEntry( this, "SaturationText");
	m_pValueText = new TextEntry( this, "ValueText" );
	m_pRedText = new TextEntry( this, "RedText" );
	m_pGreenText = new TextEntry( this, "GreenText" );
	m_pBlueText = new TextEntry( this, "BlueText" );
	m_pAlphaText= new TextEntry( this, "AlphaText" );

	m_pInitialColor = new Panel( this, "InitialColor" );
	m_pCurrentColor = new Panel( this, "CurrentColor" );
	m_pInitialColor->SetVisible( true );
	m_pCurrentColor->SetVisible( true );
	m_pInitialColor->SetPaintBackgroundEnabled( true );
	m_pCurrentColor->SetPaintBackgroundEnabled( true );

	m_pInitialColor->SetMouseInputEnabled( false );
	SetMouseInputEnabled( true );

	Color c( 255, 255, 255, 255 );
	SetInitialColor( c );

	LoadControlSettings( "resource/colorpicker.res" );
}


//-----------------------------------------------------------------------------
// Sets the initial color
//-----------------------------------------------------------------------------
void CColorPickerPanel::SetInitialColor( Color initialColor )
{
	m_InitialColor.r = initialColor.r();
	m_InitialColor.g = initialColor.g();
	m_InitialColor.b = initialColor.b();

	m_CurrentAlpha = initialColor.a();
	m_InitialAlpha = m_CurrentAlpha;
	m_CurrentColor = m_InitialColor;

	RGBtoHSV( m_CurrentColor, m_CurrentHSVColor );
	if ( m_CurrentHSVColor.x == -1 )
	{
		m_CurrentHSVColor.x = 0;
	}
	OnColorChanged();
}


//-----------------------------------------------------------------------------
// Handle input
//-----------------------------------------------------------------------------
void CColorPickerPanel::OnMousePressed( vgui::MouseCode code )
{
	BaseClass::OnMousePressed( code );

	if ( code == MOUSE_LEFT )
	{
		// Clicking inside the initial color window
		// resets the current color to the initial color
		int x, y;
		input()->GetCursorPos( x, y );
		ScreenToLocal( x, y );

		int cx, cy, cw, ch;
		m_pInitialColor->GetBounds( cx, cy, cw, ch );
		if ( ( cx <= x ) && ( cx+cw > x ) && ( cy <= y ) && ( cy+ch > y ) )
		{
			m_CurrentColor = m_InitialColor;
			m_CurrentAlpha = m_InitialAlpha;
			RGBtoHSV( m_CurrentColor, m_CurrentHSVColor );
			if ( m_CurrentHSVColor.x == -1 )
			{
				m_CurrentHSVColor.x = 0;
			}
			OnColorChanged();
		}
	}
}


//-----------------------------------------------------------------------------
// Gets the current/initial color
//-----------------------------------------------------------------------------
void CColorPickerPanel::GetCurrentColor( Color *pColor )
{
	pColor->SetColor( m_CurrentColor.r, m_CurrentColor.g, m_CurrentColor.b, m_CurrentAlpha );
}

void CColorPickerPanel::GetInitialColor( Color *pColor )
{
	pColor->SetColor( m_InitialColor.r, m_InitialColor.g, m_InitialColor.b, m_InitialAlpha );
}


//-----------------------------------------------------------------------------
// Updates the preview colors
//-----------------------------------------------------------------------------
void CColorPickerPanel::UpdatePreviewColors()
{
	Color c;
	c.SetColor( m_InitialColor.r, m_InitialColor.g, m_InitialColor.b, 255 );
	m_pInitialColor->SetBgColor( c );
	c.SetColor( m_CurrentColor.r, m_CurrentColor.g, m_CurrentColor.b, 255 );
	m_pCurrentColor->SetBgColor( c );
}


//-----------------------------------------------------------------------------
// Used to make sure we win over the scheme settings 
//-----------------------------------------------------------------------------
void CColorPickerPanel::ApplySchemeSettings(IScheme *pScheme)
{
	// Need to override the scheme settings for this button
	BaseClass::ApplySchemeSettings( pScheme );
	UpdatePreviewColors();
}


//-----------------------------------------------------------------------------
// Callbacks from the color preview dialogs
//-----------------------------------------------------------------------------
void CColorPickerPanel::OnHSVSelected( KeyValues *data )
{
	m_CurrentHSVColor.x = data->GetFloat( "hue" );
	m_CurrentHSVColor.y = data->GetFloat( "saturation" );
	m_CurrentHSVColor.z = data->GetFloat( "value" );
	HSVtoRGB( m_CurrentHSVColor, m_CurrentColor );
	OnColorChanged();
}

void CColorPickerPanel::OnColorSelected( KeyValues *data )
{
	Color c = data->GetColor( "color" );
	m_CurrentColor.r = c.r();
	m_CurrentColor.g = c.g();
	m_CurrentColor.b = c.b();
	RGBtoHSV( m_CurrentColor, m_CurrentHSVColor );
	OnColorChanged();
}


//-----------------------------------------------------------------------------
// Radio buttons
//-----------------------------------------------------------------------------
void CColorPickerPanel::OnRadioButtonChecked( KeyValues *pKeyValues )
{
	// NOTE: The radio button command strings are defined in the colorpicker.res file
	// in game/platform/resource.
	vgui::Panel *pPanel = (vgui::Panel *)pKeyValues->GetPtr( "panel" );
	if ( pPanel == m_pRedRadio )
	{
		m_pColorXYPreview->SetMode( COLOR_TYPE_RGB, CHANNEL_RED );
		m_pColorZPreview->SetMode( COLOR_TYPE_RGB, CHANNEL_RED );
	}
	else if ( pPanel == m_pGreenRadio )
	{
		m_pColorXYPreview->SetMode( COLOR_TYPE_RGB, CHANNEL_GREEN );
		m_pColorZPreview->SetMode( COLOR_TYPE_RGB, CHANNEL_GREEN );
	}
	else if ( pPanel == m_pBlueRadio )
	{
		m_pColorXYPreview->SetMode( COLOR_TYPE_RGB, CHANNEL_BLUE );
		m_pColorZPreview->SetMode( COLOR_TYPE_RGB, CHANNEL_BLUE );
	}
	else if ( pPanel == m_pHueRadio )
	{
		m_pColorXYPreview->SetMode( COLOR_TYPE_HSV, CHANNEL_HUE );
		m_pColorZPreview->SetMode( COLOR_TYPE_HSV, CHANNEL_HUE );
	}
	else if ( pPanel == m_pSaturationRadio )
	{
		m_pColorXYPreview->SetMode( COLOR_TYPE_HSV, CHANNEL_SATURATION );
		m_pColorZPreview->SetMode( COLOR_TYPE_HSV, CHANNEL_SATURATION );
	}
	else if ( pPanel == m_pValueRadio )
	{
		m_pColorXYPreview->SetMode( COLOR_TYPE_HSV, CHANNEL_VALUE );
		m_pColorZPreview->SetMode( COLOR_TYPE_HSV, CHANNEL_VALUE );
	}
}


//-----------------------------------------------------------------------------
// Called when the color changes
//-----------------------------------------------------------------------------
void CColorPickerPanel::OnColorChanged( vgui::TextEntry *pChanged )
{
	char temp[256];

	if ( pChanged != m_pRedText )
	{
		Q_snprintf( temp, sizeof(temp), "%d", m_CurrentColor.r );
		m_pRedText->SetText( temp );
	}
	if ( pChanged != m_pGreenText )
	{
		Q_snprintf( temp, sizeof(temp), "%d", m_CurrentColor.g );
		m_pGreenText->SetText( temp );
	}
	if ( pChanged != m_pBlueText )
	{
		Q_snprintf( temp, sizeof(temp), "%d", m_CurrentColor.b );
		m_pBlueText->SetText( temp );
	}
	if ( pChanged != m_pAlphaText )
	{
		Q_snprintf( temp, sizeof( temp ), "%d", m_CurrentAlpha );
		m_pAlphaText->SetText( temp );
	}

	if ( pChanged != m_pHueText )
	{
		Q_snprintf( temp, sizeof(temp), "%d", (int)(m_CurrentHSVColor.x + 0.5f) );
		m_pHueText->SetText( temp );
	}
	if ( pChanged != m_pSaturationText )
	{
		Q_snprintf( temp, sizeof(temp), "%d", (int)(m_CurrentHSVColor.y * 100 + 0.5f) );
		m_pSaturationText->SetText( temp );
	}
	if ( pChanged != m_pValueText )
	{
		Q_snprintf( temp, sizeof(temp), "%d", (int)(m_CurrentHSVColor.z * 100 + 0.5f) );
		m_pValueText->SetText( temp );
	}

	m_pColorXYPreview->SetColor( m_CurrentColor, m_CurrentHSVColor );
	m_pColorZPreview->SetColor( m_CurrentColor, m_CurrentHSVColor );
	UpdatePreviewColors();
	PostActionSignal( new KeyValues( "command", "command", "preview" ) );
}

	
//-----------------------------------------------------------------------------
// Called when the color text entry panels change
//-----------------------------------------------------------------------------
void CColorPickerPanel::OnTextChanged( KeyValues *data )
{
	Panel *pPanel = (Panel *)data->GetPtr( "panel", NULL );

	float flHue = m_CurrentHSVColor.x;

	char buf[256];
	if ( pPanel == m_pRedText )
	{
		m_pRedText->GetText( buf, sizeof(buf) );
		int val = atoi( buf );
		m_CurrentColor.r = clamp( val, 0, 255 );
		RGBtoHSV( m_CurrentColor, m_CurrentHSVColor );
	}
	else if ( pPanel == m_pGreenText )
	{
		m_pGreenText->GetText( buf, sizeof(buf) );
		int val = atoi( buf );
		m_CurrentColor.g = clamp( val, 0, 255 );
		RGBtoHSV( m_CurrentColor, m_CurrentHSVColor );
	}
	else if ( pPanel == m_pBlueText )
	{
		m_pBlueText->GetText( buf, sizeof(buf) );
		int val = atoi( buf );
		m_CurrentColor.b = clamp( val, 0, 255 );
		RGBtoHSV( m_CurrentColor, m_CurrentHSVColor );
	}
	else if ( pPanel == m_pAlphaText )
	{
		m_pAlphaText->GetText( buf, sizeof( buf ) );
		int val = atoi( buf );
		m_CurrentAlpha = clamp( val, 0, 255 );
	}
	else if ( pPanel == m_pHueText )
	{
		m_pHueText->GetText( buf, sizeof(buf) );
		int val = atoi( buf );
		m_CurrentHSVColor.x = clamp( val, 0, 360 );
		HSVtoRGB( m_CurrentHSVColor, m_CurrentColor );
	}
	else if ( pPanel == m_pSaturationText )
	{
		m_pSaturationText->GetText( buf, sizeof(buf) );
		int val = atoi( buf );
		val = clamp( val, 0, 100 );
		m_CurrentHSVColor.y = (float)val / 100.0f;
		HSVtoRGB( m_CurrentHSVColor, m_CurrentColor );
	}
	else if ( pPanel == m_pValueText )
	{
		m_pValueText->GetText( buf, sizeof(buf) );
		int val = atoi( buf );
		val = clamp( val, 0, 100 );
		m_CurrentHSVColor.z = (float)val / 100.0f;
		HSVtoRGB( m_CurrentHSVColor, m_CurrentColor );
	}

	// Preserve hue
	if ( m_CurrentHSVColor.x == -1 )
	{
		m_CurrentHSVColor.x = flHue;
	}
	OnColorChanged( static_cast<vgui::TextEntry*>(pPanel) );
}


//-----------------------------------------------------------------------------
//
// Purpose: Modal picker frame
//
//-----------------------------------------------------------------------------
CColorPickerFrame::CColorPickerFrame( vgui::Panel *pParent, const char *pTitle ) : 
	BaseClass( pParent, "ColorPickerFrame" )
{
	m_pContextKeys = NULL;
	SetDeleteSelfOnClose( true );
	m_pPicker = new CColorPickerPanel( this, "ColorPicker" );
	m_pPicker->AddActionSignalTarget( this );
	m_pOpenButton = new Button( this, "OkButton", "Ok", this, "Ok" );
	m_pCancelButton = new Button( this, "CancelButton", "#FileOpenDialog_Cancel", this, "Cancel" );
	SetBlockDragChaining( true );

	LoadControlSettings( "resource/colorpickerframe.res" );

	int w, h;
	GetSize( w, h );
	SetMinimumSize( w, h );

	SetTitle( pTitle, false );
}

CColorPickerFrame::~CColorPickerFrame()
{
	CleanUpMessage();
}


//-----------------------------------------------------------------------------
// Deletes the message
//-----------------------------------------------------------------------------
void CColorPickerFrame::CleanUpMessage()
{
	if ( m_pContextKeys )
	{
		m_pContextKeys->deleteThis();
		m_pContextKeys = NULL;
	}
}


//-----------------------------------------------------------------------------
// Purpose: Activate the dialog
//-----------------------------------------------------------------------------
void CColorPickerFrame::DoModal( Color initialColor, KeyValues *pContextKeys )
{
	CleanUpMessage();
	m_pPicker->SetInitialColor( initialColor );
	m_pContextKeys = pContextKeys;

	BaseClass::DoModal();
}


//-----------------------------------------------------------------------------
// Gets the initial color
//-----------------------------------------------------------------------------
void CColorPickerFrame::GetInitialColor( Color *pColor )
{
	m_pPicker->GetInitialColor( pColor );
}


//-----------------------------------------------------------------------------
// On command
//-----------------------------------------------------------------------------
void CColorPickerFrame::OnCommand( const char *pCommand )
{
	if ( !Q_stricmp( pCommand, "Ok" ) )
	{
		Color c;
		m_pPicker->GetCurrentColor( &c );

		KeyValues *pActionKeys = new KeyValues( "ColorPickerPicked" );
		pActionKeys->SetColor( "color", c );
		if ( m_pContextKeys )
		{
			pActionKeys->AddSubKey( m_pContextKeys );
			m_pContextKeys = NULL;
		}
		CloseModal();
		PostActionSignal( pActionKeys );
		return;
	}

	if ( !Q_stricmp( pCommand, "Cancel" ) )
	{
		vgui::input()->ReleaseAppModalSurface();
		KeyValues *pActionKeys = new KeyValues( "ColorPickerCancel" );
		if ( m_pContextKeys )
		{
			pActionKeys->AddSubKey( m_pContextKeys );
			m_pContextKeys = NULL;
		}
		CloseModal();
		PostActionSignal( pActionKeys );
		return;
	}

	if ( !Q_stricmp( pCommand, "Preview" ) )
	{
		Color c;
		m_pPicker->GetCurrentColor( &c );

		KeyValues *pActionKeys = new KeyValues( "ColorPickerPreview" );
		pActionKeys->SetColor( "color", c );
		if ( m_pContextKeys )
		{
			pActionKeys->AddSubKey( m_pContextKeys->MakeCopy() );
		}
		PostActionSignal( pActionKeys );
		return;
	}

	BaseClass::OnCommand( pCommand );
}


//-----------------------------------------------------------------------------
//
// Purpose: A button which brings up the color picker
//
//-----------------------------------------------------------------------------
CColorPickerButton::CColorPickerButton( vgui::Panel *pParent, const char *pName, vgui::Panel *pActionSignalTarget ) :
	BaseClass( pParent, pName, "" )
{
	m_CurrentColor.SetColor( 255, 255, 255, 255 );
	if ( pActionSignalTarget )
	{
		AddActionSignalTarget( pActionSignalTarget );
	}
}

CColorPickerButton::~CColorPickerButton()
{
}

void CColorPickerButton::ApplySchemeSettings( vgui::IScheme *pScheme )
{
	BaseClass::ApplySchemeSettings( pScheme );
	UpdateButtonColor();
}


//-----------------------------------------------------------------------------
// Called when the picker gets a new color
//-----------------------------------------------------------------------------
void CColorPickerButton::OnPicked( KeyValues *data )
{
	SetColor( data->GetColor( "color" ) );

	// Fire normal action signal messages
	KeyValues *pMessage = new KeyValues( "ColorPickerPicked" );
	pMessage->SetColor( "color", m_CurrentColor );
	PostActionSignal( pMessage );
	PlayButtonReleasedSound();
	SetSelected( false );
}


//-----------------------------------------------------------------------------
// Called when a color is previewed
//-----------------------------------------------------------------------------
void CColorPickerButton::OnPreview( KeyValues *data )
{
	KeyValues *pMessage = new KeyValues( "ColorPickerPreview" );
	pMessage->SetColor( "color", data->GetColor( "color" ) );
	PostActionSignal( pMessage );
}


//-----------------------------------------------------------------------------
// Called when cancel is hit in the picker
//-----------------------------------------------------------------------------
void CColorPickerButton::OnCancelled( )
{
	SetSelected( false );

	KeyValues *pMessage = new KeyValues( "ColorPickerCancel" );
	pMessage->SetColor( "startingColor", m_CurrentColor );
	PostActionSignal( pMessage );
}


//-----------------------------------------------------------------------------
// Perform the click
//-----------------------------------------------------------------------------
void CColorPickerButton::DoClick()
{
	SetSelected( true );

	CColorPickerFrame *pColorPickerDialog = new CColorPickerFrame( this, "Select Color" );
	pColorPickerDialog->AddActionSignalTarget( this );
	pColorPickerDialog->DoModal( m_CurrentColor );
}


//-----------------------------------------------------------------------------
// Set current color
//-----------------------------------------------------------------------------
void CColorPickerButton::SetColor( const Color& clr )
{
	m_CurrentColor = clr;
	UpdateButtonColor();
}

void CColorPickerButton::SetColor( int r, int g, int b, int a )
{
	m_CurrentColor.SetColor( r, g, b, a );
	UpdateButtonColor();
}


//-----------------------------------------------------------------------------
// Update button color
//-----------------------------------------------------------------------------
void CColorPickerButton::UpdateButtonColor()
{
	SetDefaultColor( m_CurrentColor, m_CurrentColor );
	SetArmedColor( m_CurrentColor, m_CurrentColor );
	SetDepressedColor( m_CurrentColor, m_CurrentColor );
}