Skip to content

Instantly share code, notes, and snippets.

@amn
Created April 19, 2023 20:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amn/487d02fab0b28e497a1ef4082b1300aa to your computer and use it in GitHub Desktop.
Save amn/487d02fab0b28e497a1ef4082b1300aa to your computer and use it in GitHub Desktop.
A rather quick-and-dirty but functioning script for toggling Windows theme against calculated times of sunset and sunrise at current location
Add-Type -AssemblyName System.Device
$gcw = New-Object System.Device.Location.GeoCoordinateWatcher
$gcw.Start()
$date = Get-Date
$DaysInYear = if ($date.Year % 4) { 366 } else { 365 }
$timezone = Get-TimeZone
# Solar math largely derived from http://gml.noaa.gov/grad/solcalc/solareqns.PDF et al
$gamma = (2 * [Math]::PI / $DaysInYear) * ($date.DayOfYear - 1 + ($date.Hour - 12) / 24)
function DegToRad($deg) {
return $deg * [Math]::PI / 180;
}
function RadToDeg($rad) {
return $rad * 180 / [Math]::PI;
}
$eqtime = 229.18 * (0.000075 + 0.001868 * [Math]::Cos($gamma) - 0.032077 * [Math]::Sin($gamma) - 0.014615 * [Math]::Cos(2 * $gamma) - 0.040849 * [Math]::Sin(2 * $gamma))
$decl = 0.006918 – 0.399912 * [Math]::Cos($gamma) + 0.070257 * [Math]::Sin($gamma) – 0.006758 * [Math]::Cos(2 * $gamma) + 0.000907 * [Math]::Sin(2 * $gamma)
– 0.002697 * [Math]::Cos(3 * $gamma) + 0.00148 * [Math]::Sin(3 * $gamma)
While($gcw.Status -ne "Ready") {
Start-Sleep 1
}
$latitude = DegToRad($gcw.Position.Location.Latitude)
$longitude = DegToRad($gcw.Position.Location.Longitude)
# $time_offset = $eqtime + 4 * (RadToDeg($longitude)) - 60 * $timezone.BaseUTCOffset.Hours
# $tst = $date.Hour * 60 + $date.Minute + $date.Second / 60 + $time_offset
# $ha = ($tst / 4) - 180
# $cos_phi = [Math]::Sin($latitude) * [Math]::Sin($decl) + [Math]::Cos($latitude) * [Math]::Cos($decl) * [Math]::Cos($ha)
$zenith = DegToRad(90.833)
$ha = [Math]::Acos([Math]::Cos($zenith) / ([Math]::Cos($latitude) * [Math]::Cos($decl)) - [Math]::Tan($latitude) * [Math]::Tan($decl))
$sunrise = (Get-Date "00:00Z").AddMinutes(720 - (4 * (RadToDeg($longitude + $ha))) - $eqtime)
$sunset = (Get-Date "00:00Z").AddMinutes(720 - (4 * (RadToDeg($longitude - $ha))) - $eqtime)
# $solar_noon = (Get-Date "00:00Z").AddMinutes(720 - (4 * (RadToDeg($longitude))) - $eqtime)
function ResetTheme($IsLightTheme) {
@("SystemUsesLightTheme", "AppsUseLightTheme") | % { Set-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize -Name $_ $IsLightTheme }
}
ResetTheme($date.Hour -ge $sunrise.Hour -and $date.Hour -lt $sunset.Hour)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment