-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathColorSpace.cs
More file actions
159 lines (141 loc) · 3.86 KB
/
Copy pathColorSpace.cs
File metadata and controls
159 lines (141 loc) · 3.86 KB
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
// Copyright (c) 2004-2008 Userwise Solutions LLC. All rights reserved.
using System;
using System.Drawing;
namespace NetCams
{
public class ColorSpace
{
//--------------------------------------------------------------------------------
// ??: HSIToRGB
// ??: ????HSI??RGB?????
// ??(Hue)???(Saturation)???(Intensity)
//--------------------------------------------------------------------------------
public static void HSIToRGB( int h, int s, int i, out int r, out int g, out int b )
{
if ( 0 > h && h > 359 ) throw new ArgumentOutOfRangeException( "h", "??H?0~359??????????????" );
if ( 0 > s && s > 255 ) throw new ArgumentOutOfRangeException( "s", "??S?0~255??????????????" );
if ( 0 > i && i > 255 ) throw new ArgumentOutOfRangeException( "i", "??I?0~255??????????????" );
if ( s == 0 )
{
r = g = b = i;
}
else
{
int ht = h * 6;
double d = (double)( ht % 360 );
int t1 = (int)( i * ( 255.0d - s ) / 255.0d );
int t2 = (int)( i * ( 255.0d - s * d / 360.0d ) / 255.0d );
int t3 = (int)( i * ( 255.0d - s * ( 360.0d - d ) / 360.0d ) / 255.0d );
switch ( ht / 360 )
{
case 0:
r = i; g = t3; b = t1; break;
case 1:
r = t2; g = i; b = t1; break;
case 2:
r = t1; g = i; b = t3; break;
case 3:
r = t1; g = t2; b = i; break;
case 4:
r = t3; g = t1; b = i; break;
default:
r = i; g = t1; b = t2; break;
}
}
}
public static Color HSIToRGB( int h, int s, int i )
{
int r, g, b;
HSIToRGB( h, s, i, out r, out g, out b );
return Color.FromArgb( r, g, b );
}
//--------------------------------------------------------------------------------
// ??: RGBToHSI
// ??: ????RGB??HSI?????
// ??(Hue)???(Saturation)???(Intensity)
//--------------------------------------------------------------------------------
public static void RGBToHSI( int r, int g, int b, out int h, out int s, out int i )
{
if ( 0 > r && r > 255 ) throw new ArgumentOutOfRangeException( "r", "?R?0~255??????????????" );
if ( 0 > g && g > 255 ) throw new ArgumentOutOfRangeException( "g", "?G?0~255??????????????" );
if ( 0 > b && b > 255 ) throw new ArgumentOutOfRangeException( "b", "?B?0~255??????????????" );
int max = GetGreatestValue( r, g, b );
int min = GetSmallestValue( r, g, b );
double d = (double)( max - min );
i = max;
if ( d == 0 )
{
s = 0;
}
else
{
s = (int)( d * 255.0d / (double)max );
}
if ( s == 0 )
{
h = 0;
}
else
{
int rt = max - (int)( r * 60.0d / d );
int gt = max - (int)( g * 60.0d / d );
int bt = max - (int)( b * 60.0d / d );
if ( r == max )
{
h = bt - gt;
}
else if ( g == max )
{
h = 120 + rt - bt;
}
else
{
h = 240 + gt - rt;
}
if ( h < 0 ) h += 360;
}
}
public static void RGBToHSI( Color col, out int h, out int s, out int i )
{
RGBToHSI( col.R, col.G, col.B, out h, out s, out i );
}
//--------------------------------------------------------------------------------
// ??: GetGreatestValue
// ??: ??????????????????????
//--------------------------------------------------------------------------------
private static int GetGreatestValue( int x, int y, int z )
{
if ( x < y )
{
return ( y < z ) ? z : y;
}
else if ( x < z )
{
return ( z < y ) ? y : z;
}
else
{
return x;
}
}
//--------------------------------------------------------------------------------
// ??: GetSmallestValue
// ??: ??????????????????????
//--------------------------------------------------------------------------------
private static int GetSmallestValue( int x, int y, int z )
{
if ( y < x )
{
return ( z < y ) ? z : y;
}
else if ( z < x )
{
return ( y < z ) ? y : z;
}
else
{
return x;
}
}
}
}