Files
redshift-rebooted/rewrite
2025-10-04 18:35:33 -05:00
..
2025-10-04 18:35:33 -05:00
2025-10-04 18:35:33 -05:00
2025-10-04 18:35:33 -05:00
2025-10-04 13:29:16 -05:00
2025-10-04 18:35:33 -05:00

Redshift Rewrite in Rust

This is a Rust rewrite of Redshift, a screen color temperature adjustment tool. This initial version includes the core functionality needed to calculate and display color temperatures based on geographic location and time of day.

Current Status

Phase 1: Core Foundation ✅ Complete

  • Module structure set up with separate files for types, solar, colorramp, gamma, and location
  • Core types ported from C: Location, ColorSetting, Period, TransitionScheme, ProgramMode
  • Solar calculations fully ported with astronomical algorithms for day/night timing
  • Color ramp logic ported with blackbody color table for temperature-to-RGB conversion

Phase 2: Basic Functionality ✅ Complete

  • Dummy gamma method implemented (no-op for safe testing)
  • Manual location provider implemented (lat/lon specification)
  • CLI argument parsing using clap crate
  • Basic main loop that calculates solar position and applies color temperature

Phase 3: Basic Testing ✅ Complete

  • Solar calculations tested and working correctly
  • Color temperature output tested with dummy method

Building

cd rewrite
cargo build --release

Usage

The basic command matches the legacy C version:

# Print current color temperature for a location
./target/debug/redshift-rebooted -l 40.7:-74.0 -m dummy -pv

# One-shot mode (set temperature once and exit)
./target/debug/redshift-rebooted -l 12:-34 -m dummy -o

# Custom day/night temperatures
./target/debug/redshift-rebooted -l 12:-34 -t 5500 --temp-night 3000 -p

Options

  • -l, --location <LAT:LON> - Location as latitude:longitude (required)
  • -m, --method <METHOD> - Gamma adjustment method (currently only 'dummy')
  • -o, --one-shot - Set temperature once and exit
  • -p, --print - Print current settings and exit
  • -v, --verbose - Verbose output
  • -t, --temp-day - Day temperature in Kelvin (default: 6500)
  • --temp-night - Night temperature in Kelvin (default: 3500)

Architecture

Module Structure

src/
├── main.rs         - Entry point, CLI parsing, main loop
├── types.rs        - Core type definitions
├── solar.rs        - Solar position calculations
├── colorramp.rs    - Color temperature to RGB conversion
├── gamma.rs        - Gamma adjustment method trait and implementations
└── location.rs     - Location provider trait and implementations

Key Components

Solar Module (solar.rs)

  • Implements astronomical algorithms from "Astronomical Algorithms" by Jean Meeus
  • Calculates solar elevation for any time and location
  • Determines day/night periods based on sun position

Color Ramp Module (colorramp.rs)

  • Contains blackbody color table (1000K-25100K in 100K intervals)
  • Interpolates between table values for precise temperatures
  • Applies brightness and gamma correction

Gamma Methods (gamma.rs)

  • Trait-based design for multiple adjustment methods
  • Currently implements dummy method (prints temperature, no display changes)
  • Ready for additional methods: DRM, RandR, VidMode, Quartz, WinGDI

Location Providers (location.rs)

  • Trait-based design for multiple location sources
  • Currently implements manual provider (user-specified lat/lon)
  • Ready for additional providers: GeoClue2, CoreLocation

Next Steps

To complete the rewrite, the following work remains:

  1. Continual Mode - Implement the main event loop that continuously updates color temperature
  2. Real Gamma Methods - Port platform-specific gamma adjustment methods:
    • DRM (Linux TTY)
    • RandR (X11, preferred)
    • VidMode (X11, legacy)
    • Quartz (macOS)
    • WinGDI (Windows)
  3. Additional Location Providers - Port automatic location detection:
    • GeoClue2 (Linux)
    • CoreLocation (macOS)
  4. Configuration File Support - Parse and apply INI-style config files
  5. Transition Animations - Smooth color temperature transitions
  6. Signal Handling - Respond to SIGUSR1 (toggle), SIGINT/SIGTERM (restore & exit)
  7. Hook Scripts - Execute user scripts on period changes

Testing

The basic version has been tested and verified to:

  • Parse command-line arguments correctly
  • Calculate solar elevation accurately
  • Determine day/night/transition periods
  • Compute appropriate color temperatures
  • Display verbose output with solar information

Example test (should show night temperature since location is in nighttime):

$ ./target/debug/redshift-rebooted -l 12:-34 -m dummy -pv
Location: 12.00, -34.00
Period: Night
Color temperature: 3500K
Brightness: 1.00
Gamma: 1.00, 1.00, 1.00
Solar elevation: -44.03°

Testing

Comprehensive test suites have been created for all non-dummy/non-placeholder code:

cargo test

Test Coverage:

  • types_tests.rs (9 tests): Core type definitions, bounds checking, defaults
  • solar_tests.rs (8 tests): Solar elevation calculations, time-based variations
  • colorramp_tests.rs (13 tests): Color temperature conversions, gamma/brightness adjustments
  • location_tests.rs (19 tests): Manual location provider functionality, option parsing

Total: 49 passing tests

All tests verify correct behavior against the legacy C implementation, including:

  • Solar position calculations at various latitudes/longitudes
  • Color temperature interpolation from blackbody table
  • Gamma ramp adjustments with brightness and gamma correction
  • Location provider initialization and configuration

Compatibility

This rewrite maintains compatibility with the legacy C version's command-line interface for basic operations. The output format and calculation methods are designed to match the original implementation.