Range에서 RGB 값을 반환합니다.내부.색상(또는 다른 색상 속성)입니다.
셀의 배경색을 점차 검은색으로 바꾸려다가 Range를 발견했습니다.내부.Color method는 외관상 임의인 Long을 반환합니다.MSDN의 문서를 보면 이 숫자가 무엇을 나타내는지 거의 알 수 없습니다.이 정도 길이의 RGB 값을 반환할 수 있는 방법이 있나요?RGB(빨강, 초록, 파랑) 기능과는 정반대의 기능이 필요합니다.
이 "임의" 숫자는 RGB 값(B256^2 + G256 + R)과 16진수 색상 값을 10진수(기본값 16에서 10진수)로 변환하는 수학적인 조합입니다.단지 다른 기반일 뿐입니다.아래는 엑셀용으로 작성한 XLAM 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 = rng.Cells(1, 1).Interior.Color
Select Case formatType
Case 1
Color = WorksheetFunction.Dec2Hex(colorVal, 6)
Case 2
Color = (colorVal Mod 256) & ", " & ((colorVal \ 256) Mod 256) & ", " & (colorVal \ 65536)
Case 3
Color = rng.Cells(1, 1).Interior.ColorIndex
Case Else
Color = colorVal
End Select
End Function
와이어트 씨가 RGB에 빠른 컬러 방법을 사용하는 것을 보니 좋네요.
R = C Mod 256
G = C \ 256 Mod 256
B = C \ 65536 Mod 256
이는 일부에서 권장하는 왼쪽 중간 오른쪽에 있는 16진 스트링을 사용하는 것보다 몇 배나 빠릅니다.
간단한 답변입니다.
이 기능에 대한 기본 제공 기능은 없습니다.함수를 직접 작성해야 합니다.
긴 답변입니다.
내무반에서 돌아온 긴 길이입니다.색상 속성은 html의 색에 익숙한 일반적인 16진수 숫자의 십진수 변환입니다.'66FF66'이요.또한 상수 xlNone(-4142)을 전달하여 셀이 배경에 색상이 없도록 설정할 수 있지만 이러한 셀은 흰색으로 표시됩니다.RGB(255, 255, 255)에서요Get소유물.이를 알고 있으면 적절한 RGB 값 중 하나 또는 전부를 반환하는 함수를 작성할 수 있습니다.
운 좋게도 친절한 앨런 와이어트 씨가 바로 여기서 그렇게 해주셨어요!
다른 답변은 제게 효과가 없었습니다.제가 찾은 건요
R = C And 255
G = C \ 256 And 255
B = C \ 256 ^ 2 And 255
제대로 작동했어요
이것은 고양이 가죽을 벗기는 또 다른 방법입니다.
'
' Type definition in declarations
'
Type RGBcolor
r As Long
g As Long
b As Long
End Type
'
' Inverse RGB function
'
Function GetRGB(ByVal x As Long) As RGBcolor
With GetRGB
.r = x Mod 256
x = x \ 256
.g = x Mod 256
x = x \ 256
.b = x Mod 256
End With
End Function
'
' Sub to test the GetRGB function
'
Sub test(x As Long)
Dim c As RGBcolor
c = GetRGB(x) ' returns RGB values: c.r, c.g, c.b
Debug.Print "Original", "Red", "Green", "Blue", "Recombined value"
Debug.Print x, c.r, c.g, c.b, RGB(c.r, c.g, c.b)
End Sub
'
'
***** IMMEDIATE WINDOW *****
test 1000
Original Red Green Blue Recombined value
1000 232 3 0 1000
16진수 값의 경우 HTML로 내보내는 경우 기호가 발생합니다.
ColorVal 번호에서 16진수를 반환하는 대신 개별 색상에서 16진수 문자열을 생성하는 것이 이상적입니다.
그 이유는 셀이 녹색/파란색과 같은 '순수' 색일 경우 잘못된 16진수를 얻을 수 있기 때문입니다.
빨간색 - RGB(255,0,0)는 'FF'를 반환하며 '를 반환해야 합니다.FF0000'입니다.
파란색 - RGB(0,0,255)가 '를 반환합니다.FF00000' - '0000'을 반환해야 합니다.FF'입니다.
HTML/CSS 색상 출력을 생성하기 위해 이러한 색상을 사용하면 파란색 셀에 대해 빨간색이 표시됩니다.
RGB 값을 기반으로 두 개의 16진수 'chunk'를 조합하기 위해 스크립트를 수정했습니다. UDF는 선두 0으로 패딩되어 한 문자의 출력이 반환됩니다(이것을 읽고 있다면 비슷한 것을 만들 수 있을 것입니다).
Color = ZeroPad(Hex((colorVal Mod 256)), 2) & ZeroPad(Hex(((colorVal \ 256) Mod 256)), 2) & ZeroPad(Hex((colorVal \ 65536)), 2)
: UDF에 대한 코드를 포함하지 않았습니다...--편집 : 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
BTW - 양식 편집기에 표시된 16진수 코드를 원하는 경우(일반 HTML 16진수 색상과 별개로 자체 표준이 있음)
Case 4 ' ::: VBA FORM HEX :::
Color = "&H00" & ZeroPad(Hex((colorVal \ 65536)), 2) & ZeroPad(Hex(((colorVal \ 256) Mod 256)), 2) & ZeroPad(Hex((colorVal Mod 256)), 2) & "&"
간단한 답변입니다.
이 세 가지 설명식 원라이너를 모듈에 넣은 다음 VBA 또는 워크시트 공식에 사용합니다.
Function rr(rgbCode): rr = rgbCode Mod 256: End Function
Function g(rgbCode): g = (rgbCode \ 256) Mod 256: End Function
Function b(rgbCode): b = rgbCode \ 65536: End Function
(Goto & Name + 대화상자에서 사용하도록 예약되어 있기 때문에 red의 함수 이름에 단일 이름을 사용할F5 R수 없습니다.)
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
언급URL : https://stackoverflow.com/questions/24132665/return-rgb-values-from-range-interior-color-or-any-other-color-property 입니다.
'programing' 카테고리의 다른 글
| .gitignore의 예외 (0) | 2023.05.05 |
|---|---|
| WPF 데이터 그리드에서 선택 사용 안 함 (0) | 2023.05.05 |
| 화면에 stdout을 유지하면서 파이프에 연결하는 방법(출력 파일이 아님) (0) | 2023.04.25 |
| Swift에서 고유한 장치 ID를 얻는 방법은 무엇입니까? (0) | 2023.04.25 |
| IIS Express를 IP 주소로 바인딩합니다. (0) | 2023.04.25 |