color.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package rgb
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/spf13/cobra"
  6. "github.com/wobkey/rgb/internal/rgb"
  7. "github.com/wobkey/rgb/internal/via"
  8. )
  9. func NewColorCmd() *cobra.Command {
  10. return &cobra.Command{
  11. Use: "color <hex>",
  12. Short: "Set solid color",
  13. Long: "Set the RGB color using a 6-digit hex code (e.g. \"ff0000\" for red).",
  14. Args: cobra.ExactArgs(1),
  15. Run: func(cmd *cobra.Command, args []string) {
  16. c, err := rgb.ParseHexColor(args[0])
  17. if err != nil {
  18. fmt.Fprintf(os.Stderr, "Error: %v\n", err)
  19. os.Exit(1)
  20. }
  21. proto, _, err := OpenDevice()
  22. if err != nil {
  23. fmt.Fprintf(os.Stderr, "Error: %v\n", err)
  24. os.Exit(1)
  25. }
  26. defer proto.Close()
  27. if err := proto.EnableHandshake(); err != nil {
  28. fmt.Fprintf(os.Stderr, "Handshake failed: %v\n", err)
  29. os.Exit(1)
  30. }
  31. if err := proto.SetValue(via.RGBLight, uint8(rgb.Mode), uint8(rgb.Static)); err != nil {
  32. fmt.Fprintf(os.Stderr, "Error setting mode to static: %v\n", err)
  33. os.Exit(1)
  34. }
  35. h, s, _ := c.HSV()
  36. if err := proto.SetValue(via.RGBLight, uint8(rgb.Hue), h); err != nil {
  37. fmt.Fprintf(os.Stderr, "Error setting hue: %v\n", err)
  38. os.Exit(1)
  39. }
  40. if err := proto.SetValue(via.RGBLight, uint8(rgb.Saturation), s); err != nil {
  41. fmt.Fprintf(os.Stderr, "Error setting saturation: %v\n", err)
  42. os.Exit(1)
  43. }
  44. fmt.Printf("Color set to %s (static mode)\n", args[0])
  45. },
  46. }
  47. }