1+ -- Theme Switcher for Quenty's Class Converter Plugin
2+ -- provides support for Roblox Studio's Themes
3+ -- defaults to light theme if Roblox Studio ever supports more themes
4+ -- @author presssssure
5+
6+ local ThemeSwitcher = {}
7+
8+ local Studio = settings ().Studio
9+
10+ local DockWidget = nil
11+
12+ local LightColors = {
13+ Background = Color3 .fromRGB (255 , 255 , 255 ),
14+ BackgroundOnHover = Color3 .fromRGB (242 , 242 , 242 ),
15+
16+ Text = Color3 .fromRGB (40 , 40 , 40 ),
17+ DropDownText = Color3 .fromRGB (27 , 42 , 53 ),
18+ DropDownMouseOverLerp = Color3 .fromRGB (0 , 0 , 0 ),
19+ TextBoxText = Color3 .fromRGB (49 , 49 , 49 ),
20+
21+ Line = Color3 .fromRGB (230 , 230 , 230 ),
22+ ScrollBar = Color3 .fromRGB (230 , 230 , 230 ),
23+ ScrollBarOnHover = Color3 .fromRGB (161 , 161 , 161 ),
24+ Selected = Color3 .fromRGB (90 , 142 , 243 ),
25+
26+ ButtonStyle = Enum .ButtonStyle .RobloxRoundDropdownButton ,
27+ }
28+ local DarkColors = {
29+ Background = Color3 .fromRGB (46 , 46 , 46 ),
30+ BackgroundOnHover = Color3 .fromRGB (56 , 56 , 56 ),
31+
32+ Text = Color3 .fromRGB (204 , 204 , 204 ),
33+ DropDownText = Color3 .fromRGB (191 , 206 , 217 ),
34+ DropDownMouseOverLerp = Color3 .fromRGB (255 , 255 , 255 ),
35+ TextBoxText = Color3 .fromRGB (213 , 213 , 213 ),
36+
37+ Line = Color3 .fromRGB (21 , 21 , 21 ),
38+ ScrollBar = Color3 .fromRGB (76 , 76 , 76 ),
39+ ScrollBarOnHover = Color3 .fromRGB (96 , 96 , 96 ),
40+ Selected = Color3 .fromRGB (90 , 142 , 243 ),
41+
42+ ButtonStyle = Enum .ButtonStyle .RobloxButton ,
43+ }
44+ local ConvertButtonTextColor = Color3 .fromRGB (255 , 255 , 255 )
45+
46+
47+ -- find theme colors (defaults to light if other themes are available besides light and dark)
48+ local function GetColorPalette (theme )
49+ if theme == Enum .UITheme .Dark then
50+ return DarkColors
51+ end
52+ return LightColors
53+ end
54+
55+
56+ -- determines if a button is a button in the dropdown menu
57+ local function isInDropDown (obj )
58+ return (obj .Parent .Parent .Parent .Parent and obj .Parent .Parent .Parent .Parent .Name == " DropDown" )
59+ end
60+
61+
62+ -- determines if something is a "Line" (really skinny Frame that stretches across the page)
63+ local function isLine (obj )
64+ return obj .AbsoluteSize .Y == 1 or obj .AbsoluteSize .Y == 2
65+ end
66+
67+
68+ -- changes the theme of a given ui element
69+ function ThemeSwitcher .SwitchObject (obj , theme )
70+ if not obj :IsA (" GuiBase" ) then return end
71+ theme = theme or Studio [" UI Theme" ]
72+
73+ local NewPalette = GetColorPalette (theme )
74+
75+ -- handle special cases first
76+ if isLine (obj ) then -- lines in the main view
77+ obj .BackgroundColor3 = NewPalette .Line
78+ return
79+ elseif obj .Name == " Scrollbar" then -- scroll bar in the drop down
80+ obj .BackgroundColor3 = NewPalette .ScrollBar
81+ return
82+ elseif obj .Name == " CheckButton" then -- check boxes in the main view
83+ obj .Style = NewPalette .ButtonStyle
84+ -- no return on purpose, text color still needs to be changed
85+ elseif obj .Name == " ConvertButton" then -- ConvertButton's text color is always the same
86+ obj .TextColor3 = ConvertButtonTextColor
87+ return
88+ end
89+
90+ -- then change the text
91+ if obj .ClassName :find (" Text" ) then
92+ local NewTextColor
93+
94+ if obj :IsA (" TextBox" ) then
95+ NewTextColor = NewPalette .TextBoxText
96+ elseif isInDropDown (obj ) then
97+ NewTextColor = NewPalette .DropDownText
98+ else
99+ NewTextColor = NewPalette .Text
100+ end
101+
102+ obj .TextColor3 = NewTextColor
103+ end
104+
105+ -- lastly change the background
106+ obj .BackgroundColor3 = NewPalette .Background
107+ end
108+
109+
110+ -- switches every ui element within
111+ local function SwitchAllObjects (NewTheme )
112+ for _ , obj in pairs (DockWidget :GetDescendants ()) do
113+ ThemeSwitcher .SwitchObject (obj , NewTheme )
114+ end
115+ end
116+
117+
118+ -- for when the user changes theme while having the window open
119+ Studio .ThemeChanged :Connect (function ()
120+ SwitchAllObjects (Studio [" UI Theme" ])
121+ end )
122+
123+
124+ -- switches the plugin dock widget and all the objects within it
125+ function ThemeSwitcher .SetDockWidget (NewDockWidget )
126+ DockWidget = NewDockWidget
127+
128+ SwitchAllObjects (Studio [" UI Theme" ])
129+ DockWidget .DescendantAdded :Connect (function (obj )
130+ ThemeSwitcher .SwitchObject (obj , Studio [" UI Theme" ])
131+ end )
132+ end
133+
134+
135+ -- allow other scripts to access colors
136+ function ThemeSwitcher .GetColorFor (ElementString )
137+ local Palette = GetColorPalette (Studio [" UI Theme" ])
138+ return Palette [ElementString ]
139+ end
140+
141+ return ThemeSwitcher
0 commit comments