2022/07/07(Thu) 09:15:03 編集(投稿者)
■No100139 (伝説のカレー さん) に返信
> HSL色空間で計算すれば色相だけ変えられます
「HLS色空間」
https://ja.wikipedia.org/wiki/HLS%E8%89%B2%E7%A9%BA%E9%96%93
から計算式を拝借
Public Class HLSColor
Private ReadOnly isUndefined As Boolean
Public ReadOnly Property A As Byte
Public ReadOnly Property H As Single
Public ReadOnly Property L As Single
Public ReadOnly Property S As Single
Public Sub New(color As Color)
A = color.A
Dim R As Integer = color.R
Dim G As Integer = color.G
Dim B As Integer = color.B
Dim MAX As Single = Math.Max(R, Math.Max(G, B))
Dim MIN As Single = Math.Min(R, Math.Min(G, B))
If MIN = MAX Then
isUndefined = True
ElseIf MIN = B Then
H = 60 * ((G - R) / (MAX - MIN)) + 60
ElseIf MIN = R Then
H = 60 * ((B - G) / (MAX - MIN)) + 180
Else
H = 60 * ((R - B) / (MAX - MIN)) + 300
End If
L = (MAX + MIN) / 2
S = MAX - MIN
End Sub
Public Sub New(A As Byte, H As Single, L As Single, S As Single)
Me.A = A
Me.H = H
Me.L = L
Me.S = S
End Sub
Public Function ToColor() As Color
Dim R, G, B As Single
Dim MAX As Single = L + S / 2
Dim MIN As Single = L - S / 2
If isUndefined Then
R = MAX
G = MAX
B = MAX
ElseIf H < 60 Then
R = MAX
G = MIN + (MAX - MIN) * (H / 60)
B = MIN
ElseIf H < 120 Then
R = MIN + (MAX - MIN) * ((120 - H) / 60)
G = MAX
B = MIN
ElseIf H < 180 Then
R = MIN
G = MAX
B = MIN + (MAX - MIN) * ((H - 120) / 60)
ElseIf H < 240 Then
R = MIN
G = MIN + (MAX - MIN) * ((240 - H) / 60)
B = MAX
ElseIf H < 300 Then
R = MIN + (MAX - MIN) * ((H - 240) / 60)
G = MIN
B = MAX
Else
R = MAX
G = MIN
B = MIN + (MAX - MIN) * ((360 - H) / 60)
End If
Return Color.FromArgb(A, CInt(R), CInt(G), CInt(B))
End Function
End Class
Public Class Form1
Protected Overrides Sub OnPaint(e As PaintEventArgs)
Dim rect As New Rectangle(10, 10, 20, 20)
For count As Integer = 3 To 20
Dim hlsColor As New HLSColor(Color.Red)
Dim A As Byte = hlsColor.A
Dim H As Single = hlsColor.H
Dim L As Single = hlsColor.L
Dim S As Single = hlsColor.S
For i As Integer = 1 To count
Dim color As Color = New HLSColor(A, H, L, S).ToColor()
Using b As New SolidBrush(color)
e.Graphics.FillRectangle(b, rect)
End Using
H += 360 / count
While H >= 360
H -= 360
End While
rect.X += 30
Next
rect.X = 10
rect.Y += 30
Next
End Sub
End Class