首页 文章

从Range.Interior.Color(或任何其他颜色属性)返回RGB值

提问于
浏览
15

我试图逐渐将单元格的背景颜色更改为黑色,我发现Range.Interior.Color方法返回一个看似随意的Long . 查看MSDN上的文档,几乎没有关于此数字代表什么的信息 . 有没有办法从这个长度返回RGB值 . 我实际上需要与RGB(红色,绿色,蓝色)功能相反的功能 .

6 回答

  • 0

    这个“任意”数字是RGB值(B * 256 ^ 2 G * 256 R)的数学组合和十六进制颜色值到十进制数(基数16到基数10)的转换,具体取决于您想要的方式看看它 . 只是不同的基础 . 下面是我在为Excel编写的XLAM addin文件中使用的方法 . 这种方法已经多次派上用场了 . 我已将文档包含在我的addin文件中 .

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '   Function            Color
    '   Purpose             Determine the Background Color Of a Cell
    '   @Param rng          Range to Determine Background Color of
    '   @Param formatType   Default Value = 0
    '                       0   Integer
    '                       1   Hex
    '                       2   RGB
    '                       3   Excel Color Index
    '   Usage               Color(A1)      -->   9507341
    '                       Color(A1, 0)   -->   9507341
    '                       Color(A1, 1)   -->   91120D
    '                       Color(A1, 2)   -->   13, 18, 145
    '                       Color(A1, 3)   -->   6
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Function Color(rng As Range, Optional formatType As Integer = 0)     As Variant
        Dim colorVal As Variant
        colorVal = Cells(rng.Row, rng.Column).Interior.Color
        Select Case formatType
            Case 1
                Color = Hex(colorVal)
            Case 2
                Color = (colorVal Mod 256) & ", " & ((colorVal \ 256) Mod 256) & ", " & (colorVal \ 65536)
            Case 3
                Color = Cells(rng.Row, rng.Column).Interior.ColorIndex
            Case Else
                Color = colorVal
        End Select
    End Function
    
  • 2

    Short Answer:

    没有内置的功能 . 你必须编写自己的函数 .

    Long Answer:

    从Interior.Color属性返回的long是我们习惯在html中查看颜色的典型十六进制数的十进制转换,例如"66FF66" . 另外,常量xlNone(-4142)可以传递给设置单元格以在背景中没有颜色,但是这些单元格从 Get 属性标记为白色 RGB(255, 255, 255) . 知道了这一点,我们就可以编写一个函数来返回一个或所有适当的RGB值 .

    幸运的是,Allan Wyatt先生在这里做到了!

    Determining the RGB Value of a Color

  • 38

    很高兴看到怀亚特先生使用快速的RGB颜色方法

    R = C Mod 256
    G = C \ 256 Mod 256
    B = C \ 65536 Mod 256
    

    这比使用六角形str的那些快几倍,有些人推荐

  • 12

    另一个答案对我不起作用 . 我找到:

    R = C And 255
    G = C \ 256 And 255
    B = C \ 256 ^ 2 And 255
    

    它运作正常 .

  • 12

    Mark Balhoff的VBA脚本运行良好 . 所有的积分都归他所有 .

    如果你想获得条件格式化单元格的颜色代码/索引,代码可能会像这样修改:

    '----------------------------------------------------------------
    '   Function            Color
    '   Purpose             Determine the Background Color Of a Cell
    '   @Param rng          Range to Determine Background Color of
    '   @Param formatType   Default Value = 0
    '                       0   Integer             color of cell, not considering conditional formatting color
    '                       1   Hex                 color of cell, not considering conditional formatting color
    '                       2   RGB                 color of cell, not considering conditional formatting color
    '                       3   Excel Color Index   color of cell, not considering conditional formatting color
    '                       4   Integer             "real" visible color of cell (as the case may be the conditional formatting color)
    '                       5   Hex                 "real" visible color of cell (as the case may be the conditional formatting color)
    '                       6   RGB                 "real" visible color of cell (as the case may be the conditional formatting color)
    '                       7   Excel Color Index   "real" visible color of cell (as the case may be the conditional formatting color)
    '   Usage               Color(A1)      -->   9507341
    '                       Color(A1, 0)   -->   9507341
    '                       Color(A1, 1)   -->   91120D
    '                       Color(A1, 2)   -->   13, 18, 145
    '                       Color(A1, 3)   -->   6
    '-----------------------------------------------------------------
    Function Color(rng As Range, Optional formatType As Integer = 0) As Variant
        Dim colorVal As Variant
        Select Case formatType
            Case 0 To 3
                colorVal = Cells(rng.Row, rng.Column).Interior.Color
            Case 4 To 7
                colorVal = Cells(rng.Row, rng.Column).DisplayFormat.Interior.Color
        End Select
        Select Case formatType
            Case 0
                Color = colorVal
            Case 1
                Color = Hex(colorVal)
            Case 2
                Color = (colorVal Mod 256) & ", " & ((colorVal \ 256) Mod 256) & ", " & (colorVal \ 65536)
            Case 3
                Color = Cells(rng.Row, rng.Column).Interior.ColorIndex
            Case 4
                Color = colorVal
            Case 5
                Color = Hex(colorVal)
            Case 6
                Color = (colorVal Mod 256) & ", " & ((colorVal \ 256) Mod 256) & ", " & (colorVal \ 65536)
            Case 7
                Color = Cells(rng.Row, rng.Column).DisplayFormat.Interior.ColorIndex
        End Select
    End Function
    
  • 0

    应该注意,对于十六进制值,如果您要导出为HTML,那么您也会得到怪癖 .

    理想情况下,您可以从单个颜色创建十六进制字符串,而不是从ColorVal数字返回十六进制 .

    原因是如果单元格是绿色/蓝色的“纯”颜色,则可以获得一些无效的十六进制数字

    RED - RGB(255,0,0)返回'FF' - 它应返回'FF0000'

    蓝色 - RGB(0,0,255)返回'FF00000' - 它应该返回'0000FF'

    enter image description here

    如果您使用它们来创建HTML / CSS颜色输出,那么任何蓝色单元格都会获得RED .

    我修改了脚本,根据RGB值组装每个两个字符的十六进制'chunk',一个UDF只用一个前导0填充,返回一个字符的输出(希望如果你正在阅读它,你可以做类似的东西)

    Color = ZeroPad(Hex((colorVal Mod 256)), 2) & ZeroPad(Hex(((colorVal \ 256) Mod 256)), 2) & ZeroPad(Hex((colorVal \ 65536)), 2)
    
    • 编辑:忘了包含UDF的代码......
    Function ZeroPad(text As String, Cnt As Integer) As String
    'Text is the string to pad
    'Cnt is the length to pad to, for example  ZeroPad(12,3) would return a string '012' , Zeropad(12,8) would return '00000012' etc..
    Dim StrLen As Integer, StrtString As String, Padded As String, LP As Integer
    
    
    StrLen = Len(Trim(text))
    
    
        If StrLen < Cnt Then
    
            For LP = 1 To Cnt - StrLen
    
                Padded = Padded & "0"
    
            Next LP
    
        End If
    
    ZeroPad = Padded & Trim(text)
    
    ENDOF:
    
    
    End Function
    

相关问题