PDA

View Full Version : How to convert number of color to RGB?



vangog
10-13-2022, 03:25 AM
Hello. I am stuck on this problem: Using this code I have obtained style color in a chart:



For j = 1 To ActiveChart.SeriesCollection.Count
r = r + 1
With ActiveChart.SeriesCollection(j)
wsT.Cells(r, 1) = .Fill.ForeColor
wsT.Cells(r, 2) = .MarkerStyle
wsT.Cells(r, 3) = .MarkerSize
wsT.Cells(r, 4) = .MarkerForegroundColor
End With
Next

For example from

ActiveChart.SeriesCollection(j).Fill.ForeColor.Fill.ForeColor
I got this number:
10077403
Now I need to set the color back to the dataseries. So there is this assigment:
ActiveSheet.ChartObjects.Chart.SeriesCollection(l).Fill.ForeColor = table.Rows(l).Columns.Cells(1).Value

But I see that .Fill.ForeColor is RGB ... so how can I convert it from number to RGB? Currently I got error wrong number of arguments or invalid assigment property which I think could be because RGB(?,?,?) expected...

georgiboy
10-13-2022, 03:43 AM
Have you looked at your other thread?

arnelgp
10-13-2022, 04:08 AM
have you tried:

ActiveSheet.ChartObjects(1).Chart.SeriesCollection(l).Format.Fill.ForeColor .RGB = table.Rows(l).Columns.Cells(1).Value

vangog
10-13-2022, 04:24 AM
have you tried:

ActiveSheet.ChartObjects(1).Chart.SeriesCollection(l).Format.Fill. ...

I don't have the property .Fill present because I use very old version.

vangog
10-13-2022, 04:27 AM
Have you looked at your other thread?
Thank you for your notice. To your code:

.Format.Line.ForeColor.RGB = rRng(r, 1).Value
Is this not the same as mine?

.Fill.ForeColor = table.Rows(l).Columns.Cells(1).Value
You use different object of the modern version. I am user of the old system Win XP, so I cannot update to your version.

georgiboy
10-13-2022, 05:29 AM
Maybe record a macro of you changing the colour and study the recorded macro - it should give you a clue as to how your system does it. You should be able to amend it from there. I do not have an old version to test on.

arnelgp
10-13-2022, 07:02 AM
try:

ActiveSheet.ChartObjects(1).Chart.SeriesCollection(1).Interior.Color = vbRed

vangog
10-13-2022, 08:32 AM
Maybe record a macro of you changing the colour and study the recorded macro - it should give you a clue as to how your system does it. You should be able to amend it from there. I do not have an old version to test on.

Good idea. This is the result when I do it manually.


' Individual point:
Sub tetss()
ActiveChart.SeriesCollection(7).Points(190).Select
With Selection.Border
.ColorIndex = 10
.Weight = xlThin
.LineStyle = xlContinuous
End With
With Selection
.MarkerBackgroundColorIndex = xlNone
.MarkerForegroundColorIndex = 50
.MarkerStyle = xlSquare
.MarkerSize = 5
.Shadow = False
End With
End Sub
' Data series:
Sub tessty()
ActiveChart.SeriesCollection(14).Select
With Selection.Border
.ColorIndex = 46
.Weight = xlThin
.LineStyle = xlContinuous
End With
With Selection
.MarkerBackgroundColorIndex = xlAutomatic
.MarkerForegroundColorIndex = 46
.MarkerStyle = xlDash
.Smooth = False
.MarkerSize = 5
.Shadow = False
End With
End Sub

vangog
10-14-2022, 03:27 AM
Based on https://exceloffthegrid.com/convert-color-codes/#Long I have created this procedure:

Sub LongToRGB(longColor As Long, ByRef R As Integer, ByRef G As Integer, ByRef B As Integer)
R = GetRGBFromLong = (longColor Mod 256)
G = GetRGBFromLong = (longColor \ 256) Mod 256
B = GetRGBFromLong = (longColor \ 65536) Mod 256
End Sub

But how to call it correctly? I expected
LongToRGB(.Cells(1).Value, R, G, B) ' obtain R, G, B from long
it.Chart.SeriesCollection(l).Fill.ForeColor = RGB(R, G, B)

Error: Expected: =

arnelgp
10-14-2022, 04:06 AM
first, if you're color is already in Long integer, you do not need to pass it to LongToRGB
and convert it back using RGB().

you can assign it immediately:

it.Chart.SeriesCollection(l).Fill.Color = .Cells(1).Value

but if you still insists on this redundancy:

LongToRGB .Cells(1).Value, R, G, B

or

Call LongToRGB(.Cells(1).Value, R, G, B)

vangog
10-14-2022, 09:09 AM
but if you still insists on this redundancy:

LongToRGB .Cells(1).Value, R, G, B

or

Call LongToRGB(.Cells(1).Value, R, G, B)

Then I have another error: Byref argument type mishmash.

arnelgp
10-14-2022, 09:14 PM
then change your function declaration:

Sub LongToRGB(Byval longColor As Long, ByRef R As Integer, ByRef G As Integer, ByRef B As Integer)
R = GetRGBFromLong = (longColor Mod 256)
G = GetRGBFromLong = (longColor \ 256) Mod 256
B = GetRGBFromLong = (longColor \ 65536) Mod 256
End Sub

vangog
10-15-2022, 12:30 AM
then change your function declaration:


It did not work. The ByRef mishmash error is still present even if I remove the Byref like that:


Sub LongToRGB(longColor As Long, R As Integer, G As Integer, B As Integer)
R = GetRGBFromLong = (longColor Mod 256)
G = GetRGBFromLong = (longColor \ 256) Mod 256
B = GetRGBFromLong = (longColor \ 65536) Mod 256
End Sub


Call LongToRGB(.Cells(1).Value, R, G, B)

arnelgp
10-15-2022, 05:27 AM
you have "Error" in your LongToRGB sub, it should be:


Sub LongToRGB(ByVal longColor As Long, r As Integer, g As Integer, b As Integer)
r = (longColor Mod 256)
g = (longColor \ 256) Mod 256
b = (longColor \ 65536) Mod 256
End Sub

to test:


Private Sub test()
Dim r As Integer, g As Integer, b As Integer
Call LongToRGB(vbRed, r, g, b)
Debug.Print r, g, b
End Sub

vangog
10-15-2022, 06:08 AM
I had an error in declaration.
Dim R , G , B As Integer
instead

Dim R As Integer, G As Integer, B As Integer

now passed but

Call LongToRGB(.Cells(1).Value, R, G, B)
it.Chart.SeriesCollection(l).Fill.ForeColor = RGB(R, G, B)
error wrong number of arguments in the color value assignment