LoginSignup
0
3

More than 3 years have passed since last update.

Excel Word PowerPoint VBA 56色カラーパレットの色をRGB分解してリスト化する

Last updated at Posted at 2019-05-19

VBAの共通注意事項

参照設定

FilesystemObjectを使用するので、Microsoft Scripting Runtimeを参照設定してください。

色を使うときはTypeStateMent 構造体 Constructor を必ず使うのでセットする

Application.ColorsプロパティのColorIndex

Office2003以前と互換性がない

原因はExcel2003までは56色しか使えず、フルカラーではなかったため。
なおこのページに一つの値で色が表示された場合の、RGBに分解する方法がある。
このテクニックはあとのマクロでも使う。

カラーパレットの操作 https://www.moug.net/tech/exvba/0110006.html
またOffice2003以前はこのColorIndexを一時的に変更していた。いまのColorScheme、Themecolorと似ている。

またColorIndexの56色にもない
https://blogs.yahoo.co.jp/mizuobb/37540943.html
後方互換を確保するにはRGB関数で指定しなければならない。
Addinを作成された方もいる
http://fnya.cocolog-nifty.com/blog/2009/02/excel-2007-exce.html

手動で変換する例
https://hamachan.info/win7/Excel/color.html

Office2007以降のColorIndex

実際の色は謎だったが、たった今発見した
Docs Office VBA リファレンス Excel Excel Graph Visual Basic リファレンス プロパティ ColorIndex
次に示す表に示すように、輪郭線、フォント、内部の塗りつぶしに適用する色を設定します。 色は、現在のカラー パレットのインデックス、または定数で表します。使用できる定数は、xlColorIndex クラスの xlColorIndexAutomatic または xlColorIndexNone です。 値の取得と設定が可能なバリアント型 (Variant) の値です。
Interior 内部を塗りつぶす色です。 ColorIndex に xlColorIndexNone を指定すると、塗りつぶしは行われません。 ColorIndex に xlColorIndexAutomatic を指定すると、自動になります (図形オブジェクトのみ)。
https://docs.microsoft.com/ja-jp/office/vba/images/colorin_za06050819.gif

image.png

ColorIndexプロパティ値一覧
ColorIndex プロパティ (Excel)
https://docs.microsoft.com/ja-jp/office/vba/api/excel.interior.colorindex

注意

Visual Basic のFormatConditionでカラーを使用したい場合は、Interior.ColorIndexプロパティを参照してください。

このパターンはPowerPointも同じ

これと似た表記がパワーポイントにもありこのxlColorIndexAutMaticという定数も使う。
つまりこのColorIndexを使用している。

色を登録する

カラーパレットに塗りつぶしの色を登録する
https://hamachan.info/win7/Office/color.html

この方法をVBAでやっているのがPowerpointのStackOverFlowのコードだと考えられる。

テーマを変更すると、色は変わります。Word、Excel、PowerPointなど、同じ操作です

つまりテーマを使うものはすべて使用できる。

Option Explicit

Type RGBColorCollection
Rd As Long
Gr As Long
Bl As Long
End Type
'CMYK4つの値の入れ物myCMYK


Type myCMYK
iC As Double
iM As Double
iY As Double
iK As Double
End Type
'RGB3つの値の入れ物myRGB


Type myRGB
iRed As Integer
iGreen As Integer
iBlue As Integer
End Type
'https://blogs.yahoo.co.jp/gogowaten/13189136.html
'http://www.officetanaka.net/excel/vba/tips/tips146.htm
Sub SepareteXlRgbColorenumerationToRgb()
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet: Set ws = ActiveSheet
Dim iRow As Long, iCol As Long
Dim i As Long
Dim C As RGBColorCollection
Dim N As Long

Dim intKeyColor As Integer
Dim LastRow As Long
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
iRow = 1
iCol = 4
ws.Cells(1, iCol).Value = "RED": iCol = iCol + 1
ws.Cells(1, iCol).Value = "Green": iCol = iCol + 1
ws.Cells(1, iCol).Value = "Blue": iCol = iCol + 1
ws.Cells(1, iCol).Value = "ColorImage": iCol = iCol + 1
For i = 2 To LastRow
N = ws.Range("B" & i).Value
C.Bl = Int(N / 65536)
C.Gr = Int((N - (C.Bl * 65536)) / 256)
C.Rd = N - (C.Gr * 256) - (C.Bl * 65536)
iCol = 4
ws.Cells(i, iCol).Value = C.Rd: iCol = iCol + 1
ws.Cells(i, iCol).Value = C.Gr: iCol = iCol + 1
ws.Cells(i, iCol).Value = C.Bl: iCol = iCol + 1
ws.Range("G" & i).Interior.Color = RGB(C.Rd, C.Gr, C.Bl)
Next
End Sub
Sub Show56ColorPallet()
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet: Set ws = ActiveSheet
Dim iRow As Long, iCol As Long
Dim i As Long
Dim C As RGBColorCollection
Dim N As Long
Dim FSOR As New Scripting.FileSystemObject
Dim TS As TextStream
ws.UsedRange.Clear
If FSOR.FileExists("C:\hoge\Excel56ColorPalletList.csv") Then FSOR.DeleteFile "C:\hoge\Excel56ColorPalletList.csv"
Set TS = FSOR.OpenTextFile("C:\hoge\Excel56ColorPalletList.csv", ForAppending, True)

Const vC = ","
iCol = 1
ws.Cells(1, iCol).Value = "ColorIndex": iCol = iCol + 1
ws.Cells(1, iCol).Value = "N": iCol = iCol + 1
ws.Cells(1, iCol).Value = "RED": iCol = iCol + 1
ws.Cells(1, iCol).Value = "Green": iCol = iCol + 1
ws.Cells(1, iCol).Value = "Blue": iCol = iCol + 1
ws.Cells(1, iCol).Value = "ColorImage": iCol = iCol + 1
ws.Cells(1, iCol).Value = "Cyan": iCol = iCol + 1
ws.Cells(1, iCol).Value = "Magenta": iCol = iCol + 1
ws.Cells(1, iCol).Value = "Yellow": iCol = iCol + 1
ws.Cells(1, iCol).Value = "Key Plate": iCol = iCol + 1
TS.WriteLine "ColorIndex" & vC & "N" & vC & "RGB:RED" & vC & "RGB:Green" & vC & "RGB:Blue" & vC & "Cyan" & vC & "Magenta" & vC & "Yellow" & vC & "Key Plate" & vC & "CMYKToRGMImage"

iRow = 2
For i = 1 To 56
iCol = 1
N = wb.Colors(i)
clcCMYK = Color2CMYK(N)
C.Bl = Int(N / 65536)
C.Gr = Int((N - (C.Bl * 65536)) / 256)
C.Rd = N - (C.Gr * 256) - (C.Bl * 65536)
ws.Cells(iRow, iCol).Value = i: iCol = iCol + 1
ws.Cells(iRow, iCol).Value = N: iCol = iCol + 1
ws.Cells(iRow, iCol).Value = C.Rd: iCol = iCol + 1
ws.Cells(iRow, iCol).Value = C.Gr: iCol = iCol + 1
ws.Cells(iRow, iCol).Value = C.Bl: iCol = iCol + 1
ws.Range(Cells(iRow, iCol).Address).Interior.ColorIndex = i: iCol = iCol + 1
ws.Cells(iRow, iCol).Value = clcCMYK.iC * 100: iCol = iCol + 1
ws.Cells(iRow, iCol).Value = clcCMYK.iM * 100: iCol = iCol + 1
ws.Cells(iRow, iCol).Value = clcCMYK.iY * 100: iCol = iCol + 1
ws.Cells(iRow, iCol).Value = clcCMYK.iK * 100: iCol = iCol + 1
intKeyColor = 255 * (100 - clcCMYK.iK) / 100
ws.Range(Cells(iRow, iCol).Address).Interior.Color = RGB(intKeyColor * (100 - clcCMYK.iC * 100) / 100, _
intKeyColor * (100 - clcCMYK.iM * 100) / 100, _
intKeyColor * (100 - clcCMYK.iY * 100) / 100)


TS.WriteLine i & vC & N & vC & C.Rd & vC & C.Gr & vC & C.Bl & vC & clcCMYK.iC & vC & clcCMYK.iM & vC & clcCMYK.iY & vC & clcCMYK.iK
iRow = iRow + 1

Next
TS.Close
End Sub

VBA,VBSの8つの組み込み定数指定

vbBlack
vbBlue
vbCyan
vbGreen
vbMagenta
vbRed
vbWhite
vbYellow

と塗りつぶしなし
xlColorIndexNone
この色はOffice2003とそれ以降で同じかは不明だが、おそらく同じなのだろう。

RGBに分解すると

http://dz11.hatenadiary.jp/entry/2017/12/07/085840
ここに一覧表があるがColorIndexの1から8が組み込み定数と一致している

143色の色指定

ここまでをまとめると
色指定はこれから紹介するものをふくめ6つある

  1. ColorPallet Office2003以前のもの。現在は使えず、互換性の問題となっている。
  2. ColorIndex Office2007以降 56色指定 最初の1から8が組み込み定数。これは互換性がない。
  3. RGB関数 Red Green Blueで指定する。16進数も使う。互換性はあるが可読性は低い。
  4. RGB関数を一つの数値にしたもの。RGBに分解するテクニックが必要。互換性がある。16進数にすると少し見やすいが可読性は10進数だとまったくない。
  5. 組み込み定数 8色、これに塗りつぶしなし、自動をあわせて使う。色数は少ないが、互換性の問題はおそらくなく、可読性が高い。
  6. そしてこれはあまり使われないがColorShemeを使う方法、これはあらかじめ登録していることが前提。これも互換性はない。
  7. 条件付き書式で使われるColorScale これはこの条件付き書式専用で、グラデーションなどを指定するもの。互換性はない。
  8. そしてこれから紹介するOffice2007 以降のRGBcolorの3つがある。ただしこの方法は条件付き書式では使えない。

この仕様はWord Excel PowerPoint、Word共通
登録を変更するにはテーマカラーを使う
Office2003とOffice2007以降ではカラーパレットの仕様が異なり、互換性が問題となる。
互換性を回避するにはRGB関数を使うか、8色の組み込み定数を使う

公式のリスト
https://docs.microsoft.com/en-us/office/vba/api/excel.xlrgbcolor

色順に並べたXlRgbColor定数の一覧表
http://www.thom.jp/vbainfo/xlrgbcolor.html
xlRgbColorを使う場合の書式がある

Colorプロパティの設定値一覧
https://excel-ubara.com/excelvba4/EXCEL285.html
一覧表としてはこちらがよい

Colorプロパティの設定値 XlRgbColor定数と色の対応表(色順)
https://officek.net/excelvba/v-range/vr-format/vrf-xlrgbcolor-01/

https://thom.hateblo.jp/entry/2016/08/22/222314
ポイント
Navy BlueとNavyは名前が違うが同じ色。そういうのがいくつかある
RGB関数より可読性が上がる(きたみ氏)
RGBに分解して16進数に直すと、HPの色指定に使える。(ubara氏)
ただし条件付き書式ではおそらく使えない。先に引用した公式のFormatCondition云々はそういう意味だろう。

PowerPoint

PowerPointは色指定がExcelによっているらしい
定数がxlから始まっているからだ。
またテーマカラーも使用できる

Interior.ColorIndex property (PowerPoint)

06/08/2017
Returns or sets the color of the interior. Read/write Variant.
Syntax

expression.ColorIndex

expression A variable that represents an 'Interior' object.
Remarks

The color is specified as an index value into the current color palette, or as one of the following XlColorIndex constants:

xlColorIndexAutomatic

xlColorIndexNone

See also

Interior Object
Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.

XlColorIndex enumeration (PowerPoint)
https://docs.microsoft.com/en-us/office/vba/api/powerpoint.xlcolorindex
06/08/2017
2 minutes to read
Specifies the color of a selected feature, such as a border, font, or fill.
Name Value Description
xlColorIndexAutomatic -4105 Automatic color.
xlColorIndexNone -4142 No color.

XlRgbColor enumeration (Excel)
https://docs.microsoft.com/en-us/office/vba/api/excel.xlrgbcolor

PowerPointの色の出しかた

Microsoft Word

基本は同じだが、独自の定数がある

WdColor 60色

https://docs.microsoft.com/en-us/office/vba/api/word.wdcolor
24 bitを前提としたもの
Specifies the 24-bit color to apply.
Name Value Description
wdColorAqua 13421619 Aqua color.
wdColorAutomatic -16777216 Automatic color. Default; usually black.
wdColorBlack 0 Black color.
wdColorBlue 16711680 Blue color.
wdColorBlueGray 10053222 Blue-gray color.
wdColorBrightGreen 65280 Bright green color.
wdColorBrown 13209 Brown color.
wdColorDarkBlue 8388608 Dark blue color.
wdColorDarkGreen 13056 Dark green color.
wdColorDarkRed 128 Dark red color.
wdColorDarkTeal 6697728 Dark teal color.
wdColorDarkYellow 32896 Dark yellow color.
wdColorGold 52479 Gold color.
wdColorGray05 15987699 Shade 05 of gray color.
wdColorGray10 15132390 Shade 10 of gray color.
wdColorGray125 14737632 Shade 125 of gray color.
wdColorGray15 14277081 Shade 15 of gray color.
wdColorGray20 13421772 Shade 20 of gray color.
wdColorGray25 12632256 Shade 25 of gray color.
wdColorGray30 11776947 Shade 30 of gray color.
wdColorGray35 10921638 Shade 35 of gray color.
wdColorGray375 10526880 Shade 375 of gray color.
wdColorGray40 10066329 Shade 40 of gray color.
wdColorGray45 9211020 Shade 45 of gray color.
wdColorGray50 8421504 Shade 50 of gray color.
wdColorGray55 7566195 Shade 55 of gray color.
wdColorGray60 6710886 Shade 60 of gray color.
wdColorGray625 6316128 Shade 625 of gray color.
wdColorGray65 5855577 Shade 65 of gray color.
wdColorGray70 5000268 Shade 70 of gray color.
wdColorGray75 4210752 Shade 75 of gray color.
wdColorGray80 3355443 Shade 80 of gray color.
wdColorGray85 2500134 Shade 85 of gray color.
wdColorGray875 2105376 Shade 875 of gray color.
wdColorGray90 1644825 Shade 90 of gray color.
wdColorGray95 789516 Shade 95 of gray color.
wdColorGreen 32768 Green color.
wdColorIndigo 10040115 Indigo color.
wdColorLavender 16751052 Lavender color.
wdColorLightBlue 16737843 Light blue color.
wdColorLightGreen 13434828 Light green color.
wdColorLightOrange 39423 Light orange color.
wdColorLightTurquoise 16777164 Light turquoise color.
wdColorLightYellow 10092543 Light yellow color.
wdColorLime 52377 Lime color.
wdColorOliveGreen 13107 Olive green color.
wdColorOrange 26367 Orange color.
wdColorPaleBlue 16764057 Pale blue color.
wdColorPink 16711935 Pink color.
wdColorPlum 6697881 Plum color.
wdColorRed 255 Red color.
wdColorRose 13408767 Rose color.
wdColorSeaGreen 6723891 Sea green color.
wdColorSkyBlue 16763904 Sky blue color.
wdColorTan 10079487 Tan color.
wdColorTeal 8421376 Teal color.
wdColorTurquoise 16776960 Turquoise color.
wdColorViolet 8388736 Violet color.
wdColorWhite 16777215 White color.
wdColorYellow 65535 Yellow color.

TypeStateMentがあるというぜんていで

Sub SepareteWdColorenumerationToRgb()
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet: Set ws = ActiveSheet
Dim iRow As Long, iCol As Long
Dim i As Long
Dim C As RGBColorCollection
Dim N As Long

Dim intKeyColor As Integer
Dim LastRow As Long
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
iRow = 1
iCol = 4
ws.Cells(1, iCol).Value = "RED": iCol = iCol + 1
ws.Cells(1, iCol).Value = "Green": iCol = iCol + 1
ws.Cells(1, iCol).Value = "Blue": iCol = iCol + 1
ws.Cells(1, iCol).Value = "ColorImage": iCol = iCol + 1
For i = 2 To LastRow
If i = 3 Then GoTo NexRow
N = ws.Range("B" & i).Value
C.Bl = Int(N / 65536)
C.Gr = Int((N - (C.Bl * 65536)) / 256)
C.Rd = N - (C.Gr * 256) - (C.Bl * 65536)
iCol = 4
ws.Cells(i, iCol).Value = C.Rd: iCol = iCol + 1
ws.Cells(i, iCol).Value = C.Gr: iCol = iCol + 1
ws.Cells(i, iCol).Value = C.Bl: iCol = iCol + 1
ws.Range("G" & i).Interior.Color = RGB(C.Rd, C.Gr, C.Bl)
NextRow:
Next
End Sub

image.png

WdColorIndex

https://docs.microsoft.com/en-us/office/vba/api/word.wdcolorindex
日本語版だと値すら誤訳するので、英語版を参照して整理した
こちらはハイライトの指定用

名前 値 説明
wdauto 0 自動設定。 通常の既定値は黒です。
wdblack 1 黒
wdblue 2 青
wdb右グリーン 4 明るい緑 wdBrightGreenの誤訳
wdByAuthor -1 文書の作成者が定義した色
wdDarkBlue 9 濃い青
wdDarkRed 13 濃い赤
wdDarkYellow 14 濃い黄
wdGray25 16 網かけ 25 の灰色
wdGray50 15 網かけ 50 の灰色
wdgreen 11 緑
wdnohighlight 0 適用されている強調表示を解除します。
wdpink 5 ピンク
wdred 6 赤
wdteal 10 青緑
wdturquoise 3 水色
wdviolet 12 紫
wdwhite 8 白
wdYellow 7 黄

Publisher

この中でPublisherだけがCMYK指定ができる

colorcmykオブジェクトを取得するのにには、 ColorFormatオブジェクトのCMYKプロパティを使用します。 ColorCMYK オブジェクトの シアン 、 マゼンタ 、 黄色 、および 黒 のプロパティを使用すると、CMYK カラー値で 4 つの色のそれぞれを個別に設定します。 ColorCMYK オブジェクトに SetCMYK メソッドを使用して、4 色すべてを一度に設定します。

次の使用例は、1 番目の図形の塗りつぶしの CMYK 値を取得して、ほかの CMYK 値に変更します


Dim cmykColor As ColorCMYK Set cmykColor = ActiveDocument.Pages(1).Shapes(1).Fill.ForeColor.CMYK cmykColor.SetCMYK Cyan:=0, Magenta:=255, Yellow:=255, Black:=50

ColorFormatを使えばCMYKも読めそうだが、今後確認して追記する
https://docs.microsoft.com/ja-jp/office/vba/api/publisher.colorformat.basecmyk

0
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3