color.go 857 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package rgb
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/spf13/cobra"
  6. "github.com/wobkey/rgb/internal/rgb"
  7. )
  8. func NewColorCmd() *cobra.Command {
  9. return &cobra.Command{
  10. Use: "color <hex>",
  11. Short: "Set RGB color",
  12. Long: "Set the RGB color using a 6-digit hex code (e.g. \"ff0000\" for red).",
  13. Args: cobra.ExactArgs(1),
  14. Run: func(cmd *cobra.Command, args []string) {
  15. c, err := rgb.ParseHexColor(args[0])
  16. if err != nil {
  17. fmt.Fprintf(os.Stderr, "Error: %v\n", err)
  18. os.Exit(1)
  19. }
  20. proto, _, err := OpenDevice()
  21. if err != nil {
  22. fmt.Fprintf(os.Stderr, "Error: %v\n", err)
  23. os.Exit(1)
  24. }
  25. defer proto.Close()
  26. h, s, _ := c.HSV()
  27. if err := proto.SetLightingColor(h, s); err != nil {
  28. fmt.Fprintf(os.Stderr, "Error setting color: %v\n", err)
  29. os.Exit(1)
  30. }
  31. fmt.Printf("Color set to %s\n", args[0])
  32. },
  33. }
  34. }