如何执行更好的着色功能?

时间:2009-05-20 00:37:30

标签: vb.net visual-studio-2005 image

重复https://stackoverflow.com/questions/885696/how-do-i-perform-a-better-colorize-function

我在vb2005中使用此功能为像素着色,但是当用户选择颜色> 50时,我开始丢失图像中的细节,任何想法如何解决这个问题?

Private badcolor As Color = Color.FromArgb(0, 0, 0, 0)
  Public Function grayscalePixel(ByVal basecolor As Color) As Color

        Return grayscalePixel(basecolor, 0.3, 0.59, 0.11)

    End Function

    Public Function grayscalePixel(ByVal basecolor As Color, ByVal RedMix As Double, ByVal GreenMix As Double, ByVal BlueMix As Double) As Color
        If basecolor.A = 0 Then
            Return badcolor
        End If
        If (RedMix + GreenMix + BlueMix > 1) Or (RedMix + GreenMix + BlueMix <= 0) Then
            Return grayscalePixel(basecolor)
        End If

        Dim grayval As Integer = basecolor.R * RedMix + basecolor.G * GreenMix + basecolor.B * BlueMix

        Return Color.FromArgb(basecolor.A, grayval, grayval, grayval)
    End Function
    Public Function colorizePixel(ByVal basecolor As Color, ByVal colorize As Color) As Color
        If basecolor.A = 0 Then
            Return badcolor
        End If
        Dim grayval As Color = grayscalePixel(basecolor)

        Dim r As Integer = Convert.ToInt32(grayval.R) + Convert.ToInt32(colorize.R)
        Dim g As Integer = Convert.ToInt32(grayval.R) + Convert.ToInt32(colorize.G)
        Dim b As Integer = Convert.ToInt32(grayval.R) + Convert.ToInt32(colorize.B)

        If r > 255 Then
            r = 255
        End If
        If g > 255 Then
            g = 255
        End If
        If b > 255 Then
            b = 255
        End If
        If r < 0 Then
            r = 0
        End If
        If g < 0 Then
            g = 0
        End If
        If b < 0 Then
            b = 0
        End If

        Return Color.FromArgb(basecolor.A, r, g, b)
    End Function

1 个答案:

答案 0 :(得分:0)

嗯,我主要是一个C#家伙,但一个好的公式是这样的:

rNew = (grayVal.R / 2) + (colorize.R / 2)

或者如果您更喜欢浮点数arithmatec:

rNew = (0.5F * grayval.R) + (0.5F * colorize.R)

第二个是设置为50/50混合的一般叠加功能。您可以更改常量以获得不同的比率。请注意,如果启用了Option Explicit,则必须将grayval.R和colorize.R强制转换为浮点数!