R = Color Mod 256 'read first byte G = (Color \ 256) Mod 256 'lop off first byte - read second B = Color \ 65536 'lop off first and second byte. 'or, alternatively: R = Color And &HFF& 'read first byte G = (Color \ &H100&) And &HFF& 'lop off first byte - read second B = Color \ &H10000& 'lop off first and second byte. |

Fortunately, in .NET, a few things are simpler. Colors are stored in their own Structure, so that means you can still pass them around like values, and you don't have to create a new instance either. A Color can be created by calling the many Fromxxxx Shared (static in C#) functions available. Extracting the R, G, and B component is simple - the Color structure has .A, .B, .G, and .R properties which give the components of each of these values in the color. You can even use .GetHue, .GetSaturation, and .GetBrightness with your color as well.

Dim A, B, G, R As Byte A = C1.A B = C1.B G = C1.G R = C1.R ' Extracting color channels is this easy. |

byte a, b, g, r; a = c1.A; b = c1.B; g = c1.G; r = c1.R; //Extracting color channels is this easy. |

Dim C1, C2, C3, C4, C5 As Color C1 = Color.FromArgb(&HFF000000) C2 = Color.FromArgb(0, 0, 0) C3 = Color.Black C4 = Color.FromKnownColor(KnownColor.Black) C5 = Color.FromName("Black") MessageBox.Show(C1.ToString()) MessageBox.Show(C2.ToString()) 'The two above all return "Color [A = 255, R = 0, G = 0, B = 0]" 'The three remaining colors return "Color [Black]" MessageBox.Show(C3.ToString()) MessageBox.Show(C4.ToString()) MessageBox.Show(C5.ToString()) |

Color c1, c2, c3, c4, c5; c1 = Color.FromArgb(0, 0, 255); c2 = Color.FromArgb(255, Color.Blue); c3 = Color.FromName("blue"); c4 = Color.Blue; c5 = Color.FromKnownColor(KnownColor.Blue); MessageBox.Show(c1.ToString()); MessageBox.Show(c2.ToString()); // The above all return "Color [A = 255, R = 0, G = 0, B = 255]" // The below return "Color [Blue]" MessageBox.Show(c3.ToString()); MessageBox.Show(c4.ToString()); MessageBox.Show(c5.ToString()); |

The good news: you won't likely be using hexadecimal for colors since you can create colors using the other overloads to Color.FromArgb().

Dim C1 As Color C1 = Color.FromArgb(0, 0, 255) 'Here's blue. Let's say we want to add a little bit of green to it. 'We have to do the following: C1 = Color.FromArgb(C1.R, C1.G + 64, C1.B) 'Now we're very susceptible to overflow at this point. 'To protect this operation a little bit, we can do: Dim R, G, B As Integer R = C1.R G = C1.G + 64 B = C1.B 'Route R to be in the 0-255 band. If R > 255 Then R = 255 ElseIf R < 0 Then R = 0 End If 'Same with G. If G > 255 Then G = 255 ElseIf G < 0 Then G = 0 End If 'And then B. If B > 255 Then B = 255 ElseIf B < 0 Then B = 0 End If 'Now, our color must be valid. C1 = Color.FromArgb(R, G, B) |

Color c1; c1 = Color.FromArgb(0, 0, 255); //Since we cannot change the values within the color structure, we have to simply make a new one... //With our new values. c1 = Color.FromArgb(c1.R, c1.G + 64, c1.B); //This is prone to error... we need to fix the values before we continue. int r, g, b; r = c1.R; g = c1.G + 64; b = c1.B; //The three color channels can be restricted to the band of 0-255. if (r > 255) r = 255; else if (r < 0) r = 0; if (g > 255) g = 255; else if (g < 0) g = 0; if (b > 255) b = 255; else if (b < 0) b = 0; // Now we can safely create our new color. c1 = Color.FromArgb(r, g, b); |

'Route all to be in the 0-255 band. R = Filters.BandPass(R, 0, 255) G = Filters.BandPass(G, 0, 255) B = Filters.BandPass(B, 0, 255) 'Now, our color must be valid. C1 = Color.FromArgb(R, G And &HFF, B) |

//The three color channels can be restricted to the band of 0-255. r = filters.BandPass(r, 0, 255); g = filters.BandPass(g, 0, 255); b = filters.BandPass(b, 0, 255); // Now we can safely create our new color. c1 = Color.FromArgb(r, g, b); MessageBox.Show(c1.ToKnownColor().ToString()); |

XML comments added by me.

Public Class Filters ''' <summary> ''' True if the last call to either of the below filters was filtered. ''' </summary> ''' <remarks></remarks> Public Shared LastWasFiltered As Boolean ''' <summary> ''' Only passes numbers that are lower than the pivot; higher numbers become equal to the pivot. ''' </summary> ''' <param name="Number">The number that will be manipulated.</param> ''' <param name="Pivot">The reference point for the LowPass</param> ''' <returns>The number if it is lower than the pivot, returns the pivot otherwise.</returns> ''' <remarks></remarks> Public Shared Function LowPass(ByVal Number As Integer, ByVal Pivot As Integer) As Integer LastWasFiltered = False If Number > Pivot Then Number = Pivot LastWasFiltered = True End If Return Number End Function ''' <summary> ''' ''' </summary> ''' <param name="Number">The number that will be manipulated.</param> ''' <param name="Pivot">The reference point for the HighPass</param> ''' <returns>The number if it is higher than the pivot, returns the pivot otherwise.</returns> ''' <remarks></remarks> Public Shared Function HighPass(ByVal Number As Integer, ByVal Pivot As Integer) As Integer LastWasFiltered = False If Number < Pivot Then Number = Pivot LastWasFiltered = True End If Return Number End Function ''' <summary> ''' ''' </summary> ''' <param name="Number">The number that will be manipulated.</param> ''' <param name="PivotHigh">The highest value within the band.</param> ''' <param name="PivotLow">The lowest value within the band.</param> ''' <returns>The number if it is within the band; returns the lowpivot if the number is below the band and the highpivot if the number is above the band.</returns> ''' <remarks></remarks> Public Shared Function BandPass(ByVal Number As Integer, ByVal PivotHigh As Integer, ByVal PivotLow As Integer) As Integer LastWasFiltered = False If Number < PivotLow Then Number = PivotLow LastWasFiltered = True ElseIf Number > PivotHigh Then Number = PivotHigh LastWasFiltered = True End If Return Number End Function End Class |

public class filters { /// <summary> /// True if the last call to either of the below filters was filtered. /// </summary> public static bool lastwasfiltered; /// <summary> /// Only passes numbers that are lower than the pivot; higher numbers become equal to the pivot. /// </summary> /// <param name="num">The number that will be manipulated.</param> /// <param name="pivot">The reference point for the LowPass</param> /// <returns>The number if it is lower than the pivot, returns the pivot otherwise.</returns> public static int LowPass(int num, int pivot) { if (num > pivot) { num = pivot; } lastwasfiltered = (num > pivot); return num; } /// <summary> /// Only passes numbers that are higher than the pivot; lower number become equal to the pivot. /// </summary> /// <param name="num">The number that will be manipulated.</param> /// <param name="pivot">The reference point for the HighPass</param> /// <returns>The number if it is higher than the pivot, returns the pivot otherwise.</returns> public static int HighPass(int num, int pivot) { if (num < pivot) { num = pivot; } lastwasfiltered = (num < pivot); return num; } /// <summary> /// Only passes numbers that are between the two pivots specifed. If the number is outside, it will be set to the closest pivot. /// </summary> /// <param name="num">The number that will be manipulated.</param> /// <param name="highpivot">The highest value within the band.</param> /// <param name="lowpivot">The lowest value within the band.</param> /// <returns>The number if it is within the band; returns the lowpivot if the number is below the band and the highpivot if the number is above the band.</returns> public static int BandPass(int num, int highpivot, int lowpivot) { lastwasfiltered = false; if (num < lowpivot) { num = lowpivot; lastwasfiltered = true; } else if (num > highpivot) { num = highpivot; lastwasfiltered = true; } return num; } } |

A Font is the object that you use for Drawing and display purposes. A FontFamily is the object that you use to diagnose a Font.

Both of these classes can be a font such as Arial. However, the FontFamily class would describe Arial as a whole, tell you what styles you can use with Arial, and get design unit sizing of the font.

The Font Class, which is used more by me -- for graphics reasons, of course, could have, for example, Arial at size 12 with Bold. A FontFamily can only tell you if the font Arial can be bold, but the Font can actually become Bold Arial.

If F2.IsStyleAvailable(FontStyle.Regular) Then |

if (f2.IsStyleAvailable(FontStyle.Regular)) |

Dim FF() As FontFamily Dim FC As Drawing.Text.InstalledFontCollection FC = New Drawing.Text.InstalledFontCollection() FF = FontFamily.Families 'At this point, FF is equivalent to FC.Families |

FontFamily[] ff; System.Drawing.Text.InstalledFontCollection fc; fc = new System.Drawing.Text.InstalledFontCollection(); ff = FontFamily.Families; // fc.Families is now equivalent to ff. |

F = New Font("Arial", 10.0F) '10pt Arial. It's Style is Regular! |

f = new Font("Arial", 10f); //10pt Arial. The style is Regular! |

F = New Font("Arial", 10.0F, FontStyle.Italic) '10pt Arial. It's Style is Italic! |

f = new Font("Arial", 10f, FontStyle.Italic); //10pt Arial. The style is Italic! |

F = New Font("Arial", 10.0F, FontStyle.Italic Or FontStyle.Bold) '10pt Arial. It's Style is Italic! |

f = new Font("Arial", 10f, FontStyle.Italic | FontStyle.Bold); //10pt Arial. The style is Italic! |

F = New Font(F, FontStyle.Bold) ' My font is bold. F = New Font(F, FontStyle.Italic) ' My font is italic... notice that it is not bold anymore. |

f = new Font(f, FontStyle.Bold); //My font is now bold. f = new Font(f, FontStyle.Italic); //My font is now italic. Note that the font is not bold - it is only Italic, because //we only specified the style to be Italic. |

F = New Font(F, F.Style Xor FontStyle.Bold) ' I've just switched the boldness of my font. |

f = new Font(f, f.Style ^ FontStyle.Bold); //I've switched the boldness of the font. |

F = New Font(F.Name, 24, F.Style) 'This is how you can change the font size. F = New Font(F.Name, F.Size + 4, F.Style) 'Also like this... |

f = new Font(f.Name, 24f, f.Style); //This is the way you change the size. f = new Font(f.Name, f.Size + 4f, f.Style); //Also like this... |

Well, that concludes this presentation on Colors and Fonts. I hope you had fun.