BotNavSim  v0.4.3
Mobile Robot Simulation
HSBColor Struct Reference

Utility struct for transforming RGB Color to HSB Color (sourced from a previous project) More...

Public Member Functions

 HSBColor (float h, float s, float b, float a)
 
 HSBColor (float h, float s, float b)
 
 HSBColor (Color col)
 
Color ToColor ()
 
override string ToString ()
 

Static Public Member Functions

static HSBColor FromColor (Color color)
 
static Color ToColor (HSBColor hsbColor)
 
static HSBColor Lerp (HSBColor a, HSBColor b, float t)
 
static void Test ()
 

Public Attributes

float h
 
float s
 
float b
 
float a
 

Detailed Description

Utility struct for transforming RGB Color to HSB Color (sourced from a previous project)

Definition at line 8 of file HSBColor.cs.

Constructor & Destructor Documentation

HSBColor.HSBColor ( float  h,
float  s,
float  b,
float  a 
)

Definition at line 15 of file HSBColor.cs.

16  {
17  this.h = h;
18  this.s = s;
19  this.b = b;
20  this.a = a;
21  }
float s
Definition: HSBColor.cs:11
float h
Definition: HSBColor.cs:10
float b
Definition: HSBColor.cs:12
float a
Definition: HSBColor.cs:13

Here is the caller graph for this function:

HSBColor.HSBColor ( float  h,
float  s,
float  b 
)

Definition at line 23 of file HSBColor.cs.

24  {
25  this.h = h;
26  this.s = s;
27  this.b = b;
28  this.a = 1f;
29  }
float s
Definition: HSBColor.cs:11
float h
Definition: HSBColor.cs:10
float b
Definition: HSBColor.cs:12
float a
Definition: HSBColor.cs:13
HSBColor.HSBColor ( Color  col)

Definition at line 31 of file HSBColor.cs.

32  {
33  HSBColor temp = FromColor(col);
34  h = temp.h;
35  s = temp.s;
36  b = temp.b;
37  a = temp.a;
38  }
float s
Definition: HSBColor.cs:11
float h
Definition: HSBColor.cs:10
float b
Definition: HSBColor.cs:12
static HSBColor FromColor(Color color)
Definition: HSBColor.cs:40
float a
Definition: HSBColor.cs:13
Utility struct for transforming RGB Color to HSB Color (sourced from a previous project) ...
Definition: HSBColor.cs:8

Here is the call graph for this function:

Member Function Documentation

static HSBColor HSBColor.FromColor ( Color  color)
static

Definition at line 40 of file HSBColor.cs.

41  {
42  HSBColor ret = new HSBColor(0f, 0f, 0f, color.a);
43 
44  float r = color.r;
45  float g = color.g;
46  float b = color.b;
47 
48  float max = Mathf.Max(r, Mathf.Max(g, b));
49 
50  if (max <= 0)
51  {
52  return ret;
53  }
54 
55  float min = Mathf.Min(r, Mathf.Min(g, b));
56  float dif = max - min;
57 
58  if (max > min)
59  {
60  if (g == max)
61  {
62  ret.h = (b - r) / dif * 60f + 120f;
63  }
64  else if (b == max)
65  {
66  ret.h = (r - g) / dif * 60f + 240f;
67  }
68  else if (b > g)
69  {
70  ret.h = (g - b) / dif * 60f + 360f;
71  }
72  else
73  {
74  ret.h = (g - b) / dif * 60f;
75  }
76  if (ret.h < 0)
77  {
78  ret.h = ret.h + 360f;
79  }
80  }
81  else
82  {
83  ret.h = 0;
84  }
85 
86  ret.h *= 1f / 360f;
87  ret.s = (dif / max) * 1f;
88  ret.b = max;
89 
90  return ret;
91  }
float s
Definition: HSBColor.cs:11
float h
Definition: HSBColor.cs:10
float b
Definition: HSBColor.cs:12
HSBColor(float h, float s, float b, float a)
Definition: HSBColor.cs:15
Utility struct for transforming RGB Color to HSB Color (sourced from a previous project) ...
Definition: HSBColor.cs:8

Here is the call graph for this function:

Here is the caller graph for this function:

static HSBColor HSBColor.Lerp ( HSBColor  a,
HSBColor  b,
float  t 
)
static

Definition at line 163 of file HSBColor.cs.

164  {
165  float h,s;
166 
167  //check special case black (color.b==0): interpolate neither hue nor saturation!
168  //check special case grey (color.s==0): don't interpolate hue!
169  if(a.b==0){
170  h=b.h;
171  s=b.s;
172  }else if(b.b==0){
173  h=a.h;
174  s=a.s;
175  }else{
176  if(a.s==0){
177  h=b.h;
178  }else if(b.s==0){
179  h=a.h;
180  }else{
181  // works around bug with LerpAngle
182  float angle = Mathf.LerpAngle(a.h * 360f, b.h * 360f, t);
183  while (angle < 0f)
184  angle += 360f;
185  while (angle > 360f)
186  angle -= 360f;
187  h=angle/360f;
188  }
189  s=Mathf.Lerp(a.s,b.s,t);
190  }
191  return new HSBColor(h, s, Mathf.Lerp(a.b, b.b, t), Mathf.Lerp(a.a, b.a, t));
192  }
float s
Definition: HSBColor.cs:11
float h
Definition: HSBColor.cs:10
float b
Definition: HSBColor.cs:12
float a
Definition: HSBColor.cs:13
HSBColor(float h, float s, float b, float a)
Definition: HSBColor.cs:15

Here is the call graph for this function:

static void HSBColor.Test ( )
static

Definition at line 194 of file HSBColor.cs.

195  {
196  HSBColor color;
197 
198  color = new HSBColor(Color.red);
199  Debug.Log("red: " + color);
200 
201  color = new HSBColor(Color.green);
202  Debug.Log("green: " + color);
203 
204  color = new HSBColor(Color.blue);
205  Debug.Log("blue: " + color);
206 
207  color = new HSBColor(Color.grey);
208  Debug.Log("grey: " + color);
209 
210  color = new HSBColor(Color.white);
211  Debug.Log("white: " + color);
212 
213  color = new HSBColor(new Color(0.4f, 1f, 0.84f, 1f));
214  Debug.Log("0.4, 1f, 0.84: " + color);
215 
216  Debug.Log("164,82,84 .... 0.643137f, 0.321568f, 0.329411f :" + ToColor(new HSBColor(new Color(0.643137f, 0.321568f, 0.329411f))));
217  }
Color ToColor()
Definition: HSBColor.cs:153
HSBColor(float h, float s, float b, float a)
Definition: HSBColor.cs:15
Utility struct for transforming RGB Color to HSB Color (sourced from a previous project) ...
Definition: HSBColor.cs:8

Here is the call graph for this function:

static Color HSBColor.ToColor ( HSBColor  hsbColor)
static

Definition at line 93 of file HSBColor.cs.

94  {
95  float r = hsbColor.b;
96  float g = hsbColor.b;
97  float b = hsbColor.b;
98  if (hsbColor.s != 0)
99  {
100  float max = hsbColor.b;
101  float dif = hsbColor.b * hsbColor.s;
102  float min = hsbColor.b - dif;
103 
104  float h = hsbColor.h * 360f;
105 
106  if (h < 60f)
107  {
108  r = max;
109  g = h * dif / 60f + min;
110  b = min;
111  }
112  else if (h < 120f)
113  {
114  r = -(h - 120f) * dif / 60f + min;
115  g = max;
116  b = min;
117  }
118  else if (h < 180f)
119  {
120  r = min;
121  g = max;
122  b = (h - 120f) * dif / 60f + min;
123  }
124  else if (h < 240f)
125  {
126  r = min;
127  g = -(h - 240f) * dif / 60f + min;
128  b = max;
129  }
130  else if (h < 300f)
131  {
132  r = (h - 240f) * dif / 60f + min;
133  g = min;
134  b = max;
135  }
136  else if (h <= 360f)
137  {
138  r = max;
139  g = min;
140  b = -(h - 360f) * dif / 60 + min;
141  }
142  else
143  {
144  r = 0;
145  g = 0;
146  b = 0;
147  }
148  }
149 
150  return new Color(Mathf.Clamp01(r),Mathf.Clamp01(g),Mathf.Clamp01(b),hsbColor.a);
151  }
float s
Definition: HSBColor.cs:11
float h
Definition: HSBColor.cs:10
float b
Definition: HSBColor.cs:12
float a
Definition: HSBColor.cs:13

Here is the caller graph for this function:

Color HSBColor.ToColor ( )

Definition at line 153 of file HSBColor.cs.

154  {
155  return ToColor(this);
156  }
Color ToColor()
Definition: HSBColor.cs:153

Here is the caller graph for this function:

override string HSBColor.ToString ( )

Definition at line 158 of file HSBColor.cs.

159  {
160  return "H:" + h + " S:" + s + " B:" + b;
161  }
float s
Definition: HSBColor.cs:11
float h
Definition: HSBColor.cs:10
float b
Definition: HSBColor.cs:12

Member Data Documentation

float HSBColor.a

Definition at line 13 of file HSBColor.cs.

float HSBColor.b

Definition at line 12 of file HSBColor.cs.

float HSBColor.h

Definition at line 10 of file HSBColor.cs.

float HSBColor.s

Definition at line 11 of file HSBColor.cs.


The documentation for this struct was generated from the following file: