VBA script to convert NCS to RGB

Brynolf

New Member
Joined
Mar 28, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Is there any way to convert NCS (Natural Color System) codes to RGB using Excel VBA?
I would appreciate greatly if someone can provide me with a viable solution using VBA!

//Brynolf
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
How about this?

ncs2rgb.xlsm
AB
1NCSRGB
20300-N247, 247, 247
30500-N242, 242, 242
40502-B242, 237, 237
50502-B50G237, 242, 242
60502-G242, 237, 237
70502-G50Y240, 242, 237
80502-R242, 237, 237
90502-R50B237, 242, 237
100502-Y242, 237, 237
110502-Y20R242, 241, 237
120502-Y50R242, 240, 237
130502-Y80R242, 238, 237
140505-B242, 230, 230
150505-B20G230, 235, 242
160505-B50G230, 242, 242
170505-B80G230, 242, 235
180505-G242, 230, 230
190505-G10Y231, 242, 230
200505-G20Y233, 242, 230
210505-G30Y234, 242, 230
220505-G40Y235, 242, 230
230505-G50Y236, 242, 230
240505-G60Y237, 242, 230
250505-G70Y239, 242, 230
260505-G80Y240, 242, 230
270505-G90Y241, 242, 230
280505-R242, 230, 230
290505-R10B242, 235, 230
300505-R20B242, 240, 230
310505-R30B240, 242, 230
320505-R40B235, 242, 230
330505-R50B230, 242, 230
340505-R60B230, 242, 235
350505-R70B230, 242, 240
360505-R80B230, 240, 242
370505-R90B230, 235, 242
380505-Y242, 230, 230
390505-Y10R242, 241, 230
400505-Y20R242, 240, 230
410505-Y30R242, 239, 230
420505-Y40R242, 237, 230
430505-Y50R242, 236, 230
440505-Y60R242, 235, 230
450505-Y70R242, 234, 230
460505-Y80R242, 233, 230
470505-Y90R242, 231, 230
480507-B242, 225, 225
490507-B20G225, 232, 242
500507-B80G225, 242, 232
510507-G242, 225, 225
Sheet2
Cell Formulas
RangeFormula
B2:B51B2=ncs2rgb(A2)


VBA Code:
Public hues As Object

Function ncs2rgb(s As String)
Set hues = CreateObject("Scripting.Dictionary")
hues("r") = 0
hues("y") = 60
hues("g") = 120
hues("b") = 240
hsv = ncs2hsv(s)
ncs2rgb = Join(hsv2rgb(hsv(0), hsv(1), hsv(2)), ", ")
End Function

Function ncs2hsv(st As String)
Dim h As Variant
Dim v As Single
Dim p1 As Single
Dim p2 As Single
Dim frac As Single

st = LCase(st)
v = 100 - Int(Left(st, 2))
s = Int(Mid(st, 3, 2))
h = Right(st, Len(st) - 5)

If Len(h) = 1 Then
    h = hues(h)
Else
    p1 = hues(Right(h, 1))
    p2 = hues(Left(h, 1))
    frac = Int(Mid(h, 2, 2)) * 0.01
    h = Round(p1 * frac + p2 * (1 - frac), 0)
    
End If

ncs2hsv = Array(h, s * 0.01, v * 0.01)
End Function

Function hsv2rgb(h As Variant, s As Variant, v As Variant)
Dim r As Double
Dim g As Double
Dim b As Double
Dim i As Integer
Dim f As Double
Dim p As Double
Dim q As Double
Dim t As Double
Dim hTemp As Double

h = h Mod 360
hTemp = h / 60
i = Application.WorksheetFunction.Floor(hTemp, 1)
f = hTemp - i
p = v * (1 - s)
q = v * (1 - (s * f))
t = v * (1 - (s * (1 - f)))

Select Case i
    Case 0
        r = v
        g = t
        b = p
    Case 1
        r = q
        g = v
        b = p
    Case 2
        r = p
        g = v
        b = t
    Case 3
        r = p
        g = q
        b = v
    Case 4
        r = t
        g = p
        b = v
    Case 5
        r = v
        g = p
        b = q
End Select

hsv2rgb = Array(Round(r * 255, 0), Round(g * 255, 0), Round(b * 255, 0))
End Function
 
Upvote 0
Thank you very much for your suggestion!:cool:
However, regrettably it doesn't quite work out. When I copy/paste VBA code into the VBA editor I get the error response "#NAME?" in the B column.
I'll have to confess that I am by no standards a phantom of VBA coding as such and I yeild to better knowledge, but where should I paste all the code on "Sheet1" or in a separate module in the VBA editor?

Kind regards
Magnus
 
Upvote 0
Thank you very much for your suggestion!:cool:
However, regrettably it doesn't quite work out. When I copy/paste VBA code into the VBA editor I get the error response "#NAME?" in the B column.
I'll have to confess that I am by no standards a phantom of VBA coding as such and I yeild to better knowledge, but where should I paste all the code on "Sheet1" or in a separate module in the VBA editor?

Kind regards
Magnus
Out of curiosity, I tested this out just now.
I copied the VBA code into a new Standard Module. If you copy the Table Data before declaring the undeclared variables, you'll see those errors you reported due to Column B containing the formulas already.

There were two Variables that weren't declared, so I took a guess and settled on:

For the following Function, I just made it a Variant.
VBA Code:
Function ncs2rgb(s As String)
Dim hsv 'Wasn't declared
For this Function, I went with Integer.
VBA Code:
Function ncs2hsv(st As String)
Dim s As Integer 'Wasn't declared
After that, the table data was able to complete. I was able to test the two functions as well...briefly.

1736242974612.png
 
Upvote 0
Thank you once again for your response ;) I admire your patience with me, but it still doesn't work! I've copied all your inputs and followed your instructions to the best of my knowledge. You see my interpretation below!
- If you still can bare with me, I'd appreciate if you can go through the "Module1" coding and help me to get it right (I've made some minor structural changes )!

Rgds
//Magnus
ncs2rgb.xlsm
AB
1NCSRGB
20300-N#VALUE!
30500-N
40502-B
50502-B50G
60502-G
70502-G50Y
80502-R
90502-R50B
100502-Y
110502-Y20R
120502-Y50R
130502-Y80R
140505-B
150505-B20G
160505-B50G
170505-B80G
180505-G
190505-G10Y
200505-G20Y
210505-G30Y
220505-G40Y
230505-G50Y
240505-G60Y
250505-G70Y
260505-G80Y
270505-G90Y
280505-R
290505-R10B
300505-R20B
310505-R30B
320505-R40B
330505-R50B
340505-R60B
350505-R70B
360505-R80B
370505-R90B
380505-Y
390505-Y10R
400505-Y20R
410505-Y30R
420505-Y40R
430505-Y50R
440505-Y60R
450505-Y70R
460505-Y80R
470505-Y90R
480507-B
490507-B20G
500507-B80G
510507-G
Blad1
Cell Formulas
RangeFormula
B2B2=ncs2rgb(A2)


VBA Code:
Option Explicit
Public hues As Object

Function ncs2rgb(s As String)

    Dim hsv
    Set hues = CreateObject("Scripting.Dictionary")
    hues("r") = 0
    hues("y") = 60
    hues("g") = 120
    hues("b") = 240
    hsv = ncs2hsv(s)
    ncs2rgb = Join(hsv2rgb(hsv(0), hsv(1), hsv(2)), ", ")

End Function

Function ncs2hsv(st As String)

    Dim h As Variant
    Dim s As Integer
    Dim v, p1, p2, frac As Single
    st = LCase(st)
    v = 100 - Int(Left(st, 2))
    s = Int(Mid(st, 3, 2))
    h = Right(st, Len(st) - 5)
        If Len(h) = 1 Then
        h = hues(h)
            Else
            p1 = hues(Right(h, 1))
            p2 = hues(Left(h, 1))
            frac = Int(Mid(h, 2, 2)) * 0.01
            h = Round(p1 * frac + p2 * (1 - frac), 0)
        End If
    ncs2hsv = Array(h, s * 0.01, v * 0.01)

End Function

Function hsv2rgb(h As Variant, s As Variant, v As Variant)

    Dim r, g, b, i, f, p, q, t, hTemp As Double
    h = h Mod 360
    hTemp = h / 60
    i = Application.WorksheetFunction.Floor(hTemp, 1)
    Application.WorksheetFunction.f
    f = hTemp - i
    p = v * (1 - s)
    q = v * (1 - (s * f))
    t = v * (1 - (s * (1 - f)))
        Select Case i
        Case 0
        r = v
        g = t
        b = p
            Case 1
            r = q
            g = v
            b = p
                Case 2
                r = p
                g = v
                b = t
                Case 3
                r = p
                g = q
                b = v
            Case 4
            r = t
            g = p
            b = v
        Case 5
        r = v
        g = p
        b = q
        End Select
    hsv2rgb = Array(Round(r * 255, 0), Round(g * 255, 0), Round(b * 255, 0))

End Function
 
Upvote 0
Solution
(I've made some minor structural changes )!
Hi, it looks to me like you've introduced an erroneous line of code in the hsv2rgb() function during that process - try deleting this line Application.WorksheetFunction.f from that function.
 
Upvote 0
Thank you for the reminder! That resolved the whole issue!
Have a nice one...

//Magnus
 
Upvote 0

Forum statistics

Threads
1,225,739
Messages
6,186,743
Members
453,370
Latest member
juliewar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top