' Classes/System/Drawing/Color.ab

#ifndef __SYSTEM_DRAWING_COLOR_AB__
#define __SYSTEM_DRAWING_COLOR_AB__

'#include <Classes/System/Math.ab>
#include <Classes/System/Drawing/Imaging/misc.ab>

Class Color
Public
	Sub Color()
		argb = MakeARGB(255, 0, 0, 0) ' Black
	End Sub

	Sub Color(ByRef c As Color)
		argb = c.argb
	End Sub

	Sub Color(r As Byte, g As Byte, b As Byte)
		argb = MakeARGB(255, r, g, b)
	End Sub

	Sub Color(a As Byte, r As Byte, g As Byte, b As Byte)
		argb = MakeARGB(a, r, g, b)
	End Sub

	Sub Color(newArgb As ARGB)
		argb = newArgb
	End Sub
/*
	Sub Operator =(c As Color)
		argb = c.argb
	End Sub
*/
	Function Operator ==(c As Color) As Boolean
		Return Equals(c)
	End Function

	Function Operator <>(c As Color) As Boolean
		Return Not Equals(c)
	End Function

	Function A() As Byte
		Return (argb >> ALPHA_SHIFT) As Byte
	End Function

	Function R() As Byte
		Return (argb >> RED_SHIFT) As Byte
	End Function

	Function G() As Byte
		Return (argb >> GREEN_SHIFT) As Byte
	End Function

	Function B() As Byte
		Return (argb >> BLUE_SHIFT) As Byte
	End Function

	Function Value() As ARGB
		Return argb
	End Function

	Sub Value(value As ARGB)
		argb = value
	End Sub

	Sub SetFromCOLORREF(rgb As COLORREF)
		If (rgb And &hff000000) = &h01000000 Then
			Exit Sub ' パレットインデックス指定は無効
		Else
			argb = MakeARGB(255, GetRValue(rgb) As Byte, GetGValue(rgb) As Byte, GetBValue(rgb) As Byte)
		End If
	End Sub

	Function ToCOLORREF() As COLORREF
		ToCOLORREF = RGB(R, G, B)
	End Function
/*
	Function ToArgb() As ARGB
		ToArgb = argb
	End Function
*/
	Static Function FromArgb(argb As ARGB) As Color
		Return New Color(argb)
	End Function

	Static Function FromArgb(a As Byte, base As Color) As Color
		Return New Color(a, base.R, base.G, base.B)
	End Function

	Static Function FromArgb(r As Byte, g As Byte, b As Byte) As Color
		Return New Color(r, g, b)
	End Function

	Static Function FromArgb(a As Byte, r As Byte, g As Byte, b As Byte) As Color
		Return New Color(a, r, g, b)
	End Function

	Override Function GetHashCode() As Long
		Return argb As Long
	End Function

	Function Equals(c As Color) As Boolean
		Return argb = c.argb
	End Function

	' HSBを求める式はhttp://ofo.jp/osakana/cgtips/hsb.phtmlを参考にしました。
	Function GetHue() As Single
		Dim max As Long, min As Long, d As Long
		Dim r = R
		Dim g = G
		Dim b = B
		max = System.Math.Max(System.Math.Max(r, g), b)
		min = System.Math.Min(System.Math.Min(r, g), b)
		d = max - min
		If g = max Then
			Return ((b - r) As Double / d * 60.0 + 120.0) As Single
		ElseIf b = max Then
			Return ((r - g) As Double / d * 60 + 240) As Single
		ElseIf g < b Then
			Return ((g - b) As Double / d * 60 + 360) As Single
		Else
			Return ((g - b) As Double / d * 60) As Single
		EndIf
	End Function

	Function GetSaturation() As Single
		Dim r = R
		Dim g = G
		Dim b = B
		Dim max = System.Math.Max(System.Math.Max(r, g), b) As Long
		Dim min = System.Math.Min(System.Math.Min(r, g), b) As Long
		Return (max - min) / max
	End Function

	Function GetBrightness() As Single
		Dim r = R
		Dim g = G
		Dim b = B
		Dim max = System.Math.Max(System.Math.Max(r, g), b)
		Return max * (1 / 255)
	End Function

/*
	' Common color constants
	Const Enum
		AliceBlue            = &hFFF0F8FF
		AntiqueWhite         = &hFFFAEBD7
		Aqua                 = &hFF00FFFF
		Aquamarine           = &hFF7FFFD4
		Azure                = &hFFF0FFFF
		Beige                = &hFFF5F5DC
		Bisque               = &hFFFFE4C4
		Black                = &hFF000000
		BlanchedAlmond       = &hFFFFEBCD
		Blue                 = &hFF0000FF
		BlueViolet           = &hFF8A2BE2
		Brown                = &hFFA52A2A
		BurlyWood            = &hFFDEB887
		CadetBlue            = &hFF5F9EA0
		Chartreuse           = &hFF7FFF00
		Chocolate            = &hFFD2691E
		Coral                = &hFFFF7F50
		CornflowerBlue       = &hFF6495ED
		Cornsilk             = &hFFFFF8DC
		Crimson              = &hFFDC143C
		Cyan                 = &hFF00FFFF
		DarkBlue             = &hFF00008B
		DarkCyan             = &hFF008B8B
		DarkGoldenrod        = &hFFB8860B
		DarkGray             = &hFFA9A9A9
		DarkGreen            = &hFF006400
		DarkKhaki            = &hFFBDB76B
		DarkMagenta          = &hFF8B008B
		DarkOliveGreen       = &hFF556B2F
		DarkOrange           = &hFFFF8C00
		DarkOrchid           = &hFF9932CC
		DarkRed              = &hFF8B0000
		DarkSalmon           = &hFFE9967A
		DarkSeaGreen         = &hFF8FBC8B
		DarkSlateBlue        = &hFF483D8B
		DarkSlateGray        = &hFF2F4F4F
		DarkTurquoise        = &hFF00CED1
		DarkViolet           = &hFF9400D3
		DeepPink             = &hFFFF1493
		DeepSkyBlue          = &hFF00BFFF
		DimGray              = &hFF696969
		DodgerBlue           = &hFF1E90FF
		Firebrick            = &hFFB22222
		FloralWhite          = &hFFFFFAF0
		ForestGreen          = &hFF228B22
		Fuchsia              = &hFFFF00FF
		Gainsboro            = &hFFDCDCDC
		GhostWhite           = &hFFF8F8FF
		Gold                 = &hFFFFD700
		Goldenrod            = &hFFDAA520
		Gray                 = &hFF808080
		Green                = &hFF008000
		GreenYellow          = &hFFADFF2F
		Honeydew             = &hFFF0FFF0
		HotPink              = &hFFFF69B4
		IndianRed            = &hFFCD5C5C
		Indigo               = &hFF4B0082
		Ivory                = &hFFFFFFF0
		Khaki                = &hFFF0E68C
		Lavender             = &hFFE6E6FA
		LavenderBlush        = &hFFFFF0F5
		LawnGreen            = &hFF7CFC00
		LemonChiffon         = &hFFFFFACD
		LightBlue            = &hFFADD8E6
		LightCoral           = &hFFF08080
		LightCyan            = &hFFE0FFFF
		LightGoldenrodYellow = &hFFFAFAD2
		LightGray            = &hFFD3D3D3
		LightGreen           = &hFF90EE90
		LightPink            = &hFFFFB6C1
		LightSalmon          = &hFFFFA07A
		LightSeaGreen        = &hFF20B2AA
		LightSkyBlue         = &hFF87CEFA
		LightSlateGray       = &hFF778899
		LightSteelBlue       = &hFFB0C4DE
		LightYellow          = &hFFFFFFE0
		Lime                 = &hFF00FF00
		LimeGreen            = &hFF32CD32
		Linen                = &hFFFAF0E6
		Magenta              = &hFFFF00FF
		Maroon               = &hFF800000
		MediumAquamarine     = &hFF66CDAA
		MediumBlue           = &hFF0000CD
		MediumOrchid         = &hFFBA55D3
		MediumPurple         = &hFF9370DB
		MediumSeaGreen       = &hFF3CB371
		MediumSlateBlue      = &hFF7B68EE
		MediumSpringGreen    = &hFF00FA9A
		MediumTurquoise      = &hFF48D1CC
		MediumVioletRed      = &hFFC71585
		MidnightBlue         = &hFF191970
		MintCream            = &hFFF5FFFA
		MistyRose            = &hFFFFE4E1
		Moccasin             = &hFFFFE4B5
		NavajoWhite          = &hFFFFDEAD
		Navy                 = &hFF000080
		OldLace              = &hFFFDF5E6
		Olive                = &hFF808000
		OliveDrab            = &hFF6B8E23
		Orange               = &hFFFFA500
		OrangeRed            = &hFFFF4500
		Orchid               = &hFFDA70D6
		PaleGoldenrod        = &hFFEEE8AA
		PaleGreen            = &hFF98FB98
		PaleTurquoise        = &hFFAFEEEE
		PaleVioletRed        = &hFFDB7093
		PapayaWhip           = &hFFFFEFD5
		PeachPuff            = &hFFFFDAB9
		Peru                 = &hFFCD853F
		Pink                 = &hFFFFC0CB
		Plum                 = &hFFDDA0DD
		PowderBlue           = &hFFB0E0E6
		Purple               = &hFF800080
		Red                  = &hFFFF0000
		RosyBrown            = &hFFBC8F8F
		RoyalBlue            = &hFF4169E1
		SaddleBrown          = &hFF8B4513
		Salmon               = &hFFFA8072
		SandyBrown           = &hFFF4A460
		SeaGreen             = &hFF2E8B57
		SeaShell             = &hFFFFF5EE
		Sienna               = &hFFA0522D
		Silver               = &hFFC0C0C0
		SkyBlue              = &hFF87CEEB
		SlateBlue            = &hFF6A5ACD
		SlateGray            = &hFF708090
		Snow                 = &hFFFFFAFA
		SpringGreen          = &hFF00FF7F
		SteelBlue            = &hFF4682B4
		Tan                  = &hFFD2B48C
		Teal                 = &hFF008080
		Thistle              = &hFFD8BFD8
		Tomato               = &hFFFF6347
		Transparent          = &h00FFFFFF
		Turquoise            = &hFF40E0D0
		Violet               = &hFFEE82EE
		Wheat                = &hFFF5DEB3
		White                = &hFFFFFFFF
		WhiteSmoke           = &hFFF5F5F5
		Yellow               = &hFFFFFF00
		YellowGreen          = &hFF9ACD32
	End Enum

	' Shift count and bit mask for A, R, G, B components

	Const Enum
		AlphaShift  = 24
		RedShift    = 16
		GreenShift  = 8
		BlueShift   = 0
	End Enum

	Const Enum
		AlphaMask   = &hff000000
		RedMask     = &h00ff0000
		GreenMask   = &h0000ff00
		BlueMask    = &h000000ff
	End Enum
*/
	Static Function MakeARGB(a As Byte, r As Byte, g As Byte, b As Byte) As ARGB
		MakeARGB = (((b As ARGB) <<  BLUE_SHIFT) Or _
		            ((g As ARGB) << GREEN_SHIFT) Or _
		            ((r As ARGB) <<   RED_SHIFT) Or _
		            ((a As ARGB) << ALPHA_SHIFT))
	End Function

Protected
	argb As ARGB
End Class

#endif '__SYSTEM_DRAWING_COLOR_AB__
