image

package
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 11, 2025 License: GPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Command = &cobra.Command{
	Use:   "image [flags] <image>",
	Short: "Generate colors from a image",
	Args:  cobra.ExactArgs(1),
	PreRun: func(cmd *cobra.Command, _ []string) {
		viper.BindPFlags(cmd.Flags())
	},
	RunE: func(_ *cobra.Command, args []string) error {
		imagePath := args[0]

		cwd, err := os.Getwd()
		if err != nil {
			return err
		}

		imagePath, err = pathutil.FindPath(cwd, imagePath)
		if err != nil {
			return fmt.Errorf("failed to find image path: %w", err)
		}

		slog.Info("Generating color", "from", imagePath)

		quantized, err := cache.LoadCache(imagePath)
		if err != nil {
			if !os.IsNotExist(err) {
				slog.Error("Failed to load cache", "error", err)
			}

			file, err := os.Open(imagePath)
			if err != nil {
				return fmt.Errorf("failed to open image file: %w", err)
			}
			defer file.Close()

			img, _, err := image.Decode(file)
			if err != nil {
				return fmt.Errorf("failed to decode image: %w", err)
			}

			pixels := material.GetPixelsFromImage(img)
			quantized = material.Quantize(pixels)
		}

		cfg, err := material.GetConfig()
		if err != nil {
			return err
		}

		colorMap, wu, err := material.GenerateFromQuantized(quantized, cfg)
		if err != nil {
			return fmt.Errorf("failed to generate colors: %w", err)
		}

		fg, bg := colorMap["on_background"], colorMap["background"]
		based := base16.Generate(fg, bg, wu)

		output := models.NewOutput(imagePath, based, colorMap)

		if err := cache.SaveCache(imagePath, quantized); err != nil {
			slog.Warn("Failed to save colors to cache", "error", err)
		}

		if err := cache.SaveState(imagePath, quantized); err != nil {
			slog.Warn("Failed to save colors to cache", "error", err)
		}

		if viper.GetBool("json") {
			if err := json.NewEncoder(os.Stdout).Encode(output); err != nil {
				slog.Error("Failed to encode output", "error", err)
			}
		}

		if !viper.GetBool("dry-run") {
			return templates.Execute(output)
		}

		return nil
	},
}

Command is the image command

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL