Skip to content

Commit 8364ef8

Browse files
author
James Brundage
committed
Adding Color.Source.V3.SetColor (Fixes #55)
1 parent e9b267f commit 8364ef8

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
param(
2+
[ValidatePattern('\#(?>[0-9a-f]{8}|[0-9a-f]{6}|[0-9a-f]{4}|[0-9a-f]{3})')]
3+
[string]
4+
$Color
5+
)
6+
7+
$hexChar = [Regex]::new('[0-9a-f]')
8+
$hexColors = @($hexChar.Matches($Color))
9+
10+
switch ($hexColors.Length) {
11+
8 {
12+
#full rgba
13+
$alpha = [byte]::Parse($hexColors[0..1] -join '', 'HexNumber')
14+
$red = [byte]::Parse($hexColors[2..3] -join '', 'HexNumber')
15+
$green = [byte]::Parse($hexColors[4..5] -join '', 'HexNumber')
16+
$blue = [byte]::Parse($hexColors[6..7] -join '', 'HexNumber')
17+
}
18+
6 {
19+
#rgb only, assume ff for alpha
20+
$alpha = 0xff
21+
$red = [byte]::Parse($hexColors[0..1] -join '', 'HexNumber')
22+
$green = [byte]::Parse($hexColors[2..3] -join '', 'HexNumber')
23+
$blue = [byte]::Parse($hexColors[4..5] -join '', 'HexNumber')
24+
}
25+
4 {
26+
#short rgba
27+
$alpha = [byte]::Parse(($hexColors[0],$hexColors[0] -join ''), 'HexNumber')
28+
$red = [byte]::Parse(($hexColors[1],$hexColors[1] -join ''), 'HexNumber')
29+
$green = [byte]::Parse(($hexColors[2],$hexColors[2] -join ''), 'HexNumber')
30+
$blue = [byte]::Parse(($hexColors[3],$hexColors[3] -join ''), 'HexNumber')
31+
}
32+
3 {
33+
#short rgb, assume f for alpha
34+
$alpha = 0xff
35+
$red = [byte]::Parse(($hexColors[0],$hexColors[0] -join ''), 'HexNumber')
36+
$green = [byte]::Parse(($hexColors[1],$hexColors[1] -join ''), 'HexNumber')
37+
$blue = [byte]::Parse(($hexColors[2],$hexColors[2] -join ''), 'HexNumber')
38+
}
39+
0 {
40+
# No color provided, default to transparent black
41+
$alpha = 0
42+
$red = 0
43+
$green = 0
44+
$blue = 0
45+
}
46+
}
47+
48+
$hexColor = ("{0:x2}{1:x2}{2:x2}{3:x2}" -f $alpha, $blue, $green, $red)
49+
50+
$realColor = [uint32]::Parse($hexColor,'HexNumber')
51+
52+
$this | Set-OBSInputSettings -InputSettings ([Ordered]@{color=$realColor})
53+
54+
55+

0 commit comments

Comments
 (0)