-
Notifications
You must be signed in to change notification settings - Fork 335
/
Copy pathConvertLayersToGrayscale.rvb
90 lines (72 loc) · 2.57 KB
/
ConvertLayersToGrayscale.rvb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' ConvertLayersToGrayscale.rvb -- November 2015
' If this code works, it was written by Dale Fugier.
' If not, I don't know who wrote it.
' Works with Rhino 5.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Explicit
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Converts the colors of layers to grayscale
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub ConvertLayersToGrayscale()
Dim arrLayers, strLayer, lngColor, nAlgorithm
Dim nGray, nRed, nGreen, nBlue
Dim arrAlgorithms, strAlgorithm
arrAlgorithms = Array("Average", "BT601", "BT709", "Desaturate", "Luminance", "NTSC")
strAlgorithm = Rhino.ListBox(arrAlgorithms, "Select a grayscale algorithm", "Layer Color")
If IsNull(strAlgorithm) Then Exit Sub
Call Rhino.EnableRedraw(False)
arrLayers = Rhino.LayerNames
For Each strLayer In arrLayers
lngColor = Rhino.LayerColor(strLayer)
nRed = Rhino.ColorRedValue(lngColor)
nGreen = Rhino.ColorGreenValue(lngColor)
nBlue = Rhino.ColorBlueValue(lngColor)
If strAlgorithm = "Average" Then
nGray = Average(nRed, nGreen, nBlue)
ElseIf strAlgorithm = "BT601" Then
nGray = BT601(nRed, nGreen, nBlue)
ElseIf strAlgorithm = "BT709" Then
nGray = BT709(nRed, nGreen, nBlue)
ElseIf strAlgorithm = "Desaturate" Then
nGray = Desaturate(nRed, nGreen, nBlue)
ElseIf strAlgorithm = "Luminance" Then
nGray = Luminance(nRed, nGreen, nBlue)
Else
nGray = NTSC(nRed, nGreen, nBlue)
End If
Call Rhino.LayerColor(strlayer, RGB(nGray, nGray, nGray))
Next
Call Rhino.EnableRedraw(True)
End Sub
Function Average(nRed, nGreen, nBlue)
Dim nGray
nGray = (nRed + nGreen + nBlue) / 3
Average = CInt(nGray)
End Function
Function BT601(nRed, nGreen, nBlue)
Dim nGray
nGray = (nRed * 0.299) + (nGreen * 0.587) + (nBlue * 0.114)
BT601 = CInt(nGray)
End Function
Function BT709(nRed, nGreen, nBlue)
Dim nGray
nGray = (nRed * 0.2126) + (nGreen * 0.7152) + (nBlue * 0.0722)
BT709 = CInt(nGray)
End Function
Function Desaturate(nRed, nGreen, nBlue)
Dim nGray
nGray = (Rhino.Max(Array(nRed, nGreen, nBlue)) + Rhino.Min(Array(nRed, nGreen, nBlue))) / 2
Desaturate = CInt(nGray)
End Function
Function Luminance(nRed, nGreen, nBlue)
Dim nGray
nGray = (nRed * 0.21) + (nGreen * 0.72) + (nBlue * 0.07)
Luminance = CInt(nGray)
End Function
Function NTSC(nRed, nGreen, nBlue)
Dim nGray
nGray = (nRed * 0.3) + (nGreen * 0.59) + (nBlue * 0.11)
NTSC = CInt(nGray)
End Function
ConvertLayersToGrayscale