C#: Create a lighter/darker color based on a system color

前端 未结 11 1420
春和景丽
春和景丽 2020-12-07 18:26

Duplicate

How do I adjust the brightness of a color?
How do I determine darker or lighter color variant of a given color?
Programmat

11条回答
  •  死守一世寂寞
    2020-12-07 19:09

    I changed Pavel Vladov function to modify even RGB component, to get shades on any combination of R/G/B directions:

    Public Function ChangeColorShades(color As Color, correctionFactor As Single, bR As Boolean, bG As Boolean, bB As Boolean) As Color
    
    
        Dim red As Single = CSng(color.R)
        Dim green As Single = CSng(color.G)
        Dim blue As Single = CSng(color.B)
    
        If (correctionFactor < 0) Then
    
            correctionFactor = 1 + correctionFactor
            If bR Then
                red *= correctionFactor
            End If
            If bG Then
                green *= correctionFactor
            End If
            If bB Then
                blue *= correctionFactor
            End If
    
    
        Else
            If bR Then
                red = (255 - red) * correctionFactor + red
            End If
            If bG Then
                green = (255 - green) * correctionFactor + green
            End If
            If bB Then
                blue = (255 - blue) * correctionFactor + blue
            End If
    
        End If
    
        Return color.FromArgb(color.A, CInt(red), CInt(green), CInt(blue))
    End Function
    

提交回复
热议问题