Skip to content

Instantly share code, notes, and snippets.

@amn
Last active March 16, 2025 20:26
Show Gist options
  • Save amn/487d02fab0b28e497a1ef4082b1300aa to your computer and use it in GitHub Desktop.
Save amn/487d02fab0b28e497a1ef4082b1300aa to your computer and use it in GitHub Desktop.

Revisions

  1. amn revised this gist Mar 16, 2025. 1 changed file with 2 additions and 7 deletions.
    9 changes: 2 additions & 7 deletions toggle-windows-theme.ps1
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    # Calculate local sunset time and set Windows theme to "dark" if it's past sunset time or "light" if it's before sunset time
    # Author: Armen Michaeli <[email protected]>

    # May be run on a schedule (e.g. every 15 minutes) or manually.

    Add-Type -AssemblyName System.Device
    @@ -34,20 +36,13 @@ While($gcw.Status -ne "Ready") {
    $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 }
    }
  2. amn revised this gist Sep 4, 2024. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions toggle-windows-theme.ps1
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    # Calculate local sunset time and set Windows theme to "dark" if it's past sunset time or "light" if it's before sunset time
    # May be run on a schedule (e.g. every 15 minutes) or manually.

    Add-Type -AssemblyName System.Device

    $gcw = New-Object System.Device.Location.GeoCoordinateWatcher
  3. amn revised this gist Sep 4, 2024. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions toggle-windows-theme.ps1
    Original file line number Diff line number Diff line change
    @@ -22,8 +22,7 @@ function RadToDeg($rad) {

    $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.0069180.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)
    $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
  4. amn created this gist Apr 19, 2023.
    53 changes: 53 additions & 0 deletions toggle-windows-theme.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    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.0069180.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)