Convert Figma logo to code with AI

fatih logocolor

Color package for Go (golang)

7,254
615
7,254
15

Top Related Projects

1,496

🎨 Terminal color rendering library, support 8/16 colors, 256 colors, RGB color rendering output, support Print/Sprintf methods, compatible with Windows. GO CLI 控制台颜色渲染工具库,支持16色,256色,RGB色彩渲染输出,使用类似于 Print/Sprintf,兼容并支持 Windows 环境的色彩渲染

1,707

Advanced ANSI style & color support for your terminal applications

1,416

Golang ultimate ANSI-colors that supports Printf/Sprintf methods

Quick Overview

fatih/color is a Go package that provides a simple way to add color and style to console output. It supports both ANSI color escape codes and Windows console colors, making it cross-platform compatible. The library offers a straightforward API for adding foreground and background colors, as well as text styles like bold and underline.

Pros

  • Cross-platform compatibility (works on Unix-like systems and Windows)
  • Simple and intuitive API
  • Supports a wide range of colors and text styles
  • Automatically detects if the terminal supports colors

Cons

  • Limited to console output (not suitable for GUI applications)
  • May not work correctly in all terminal emulators
  • Doesn't support true color (24-bit) output

Code Examples

  1. Basic color output:
import "github.com/fatih/color"

color.Blue("This text is blue")
color.Red("This text is red")
  1. Combining colors and styles:
import "github.com/fatih/color"

// Bold cyan text on a white background
color.New(color.FgCyan, color.Bold).Add(color.BgWhite).Println("Bold cyan on white")
  1. Creating custom color functions:
import "github.com/fatih/color"

// Create a custom color function
warning := color.New(color.FgYellow).Add(color.Bold).SprintFunc()
fmt.Printf("%s: %s\n", warning("Warning"), "This is a warning message")

Getting Started

To use fatih/color in your Go project, follow these steps:

  1. Install the package:

    go get github.com/fatih/color
    
  2. Import the package in your Go file:

    import "github.com/fatih/color"
    
  3. Use the color functions in your code:

    color.Green("This is a green text")
    color.Red("Error:", err)
    

That's it! You can now start adding colorful output to your Go applications.

Competitor Comparisons

1,496

🎨 Terminal color rendering library, support 8/16 colors, 256 colors, RGB color rendering output, support Print/Sprintf methods, compatible with Windows. GO CLI 控制台颜色渲染工具库,支持16色,256色,RGB色彩渲染输出,使用类似于 Print/Sprintf,兼容并支持 Windows 环境的色彩渲染

Pros of gookit/color

  • More comprehensive color manipulation features, including RGB/HSL conversion and color mixing
  • Supports Windows CMD and PowerShell with true color
  • Includes additional utility functions for color-related operations

Cons of gookit/color

  • Slightly more complex API, which may have a steeper learning curve
  • Less widespread adoption compared to color

Code Comparison

color:

color.New(color.FgBlue).Add(color.Bold).Println("Hello, World!")

gookit/color:

color.Style{color.FgBlue, color.OpBold}.Println("Hello, World!")

Both libraries offer similar functionality for basic color output, but gookit/color provides a more extensive set of features for advanced color manipulation and styling. color is known for its simplicity and widespread use, while gookit/color offers a broader range of capabilities at the cost of a slightly more complex API.

The choice between the two libraries depends on the specific requirements of your project. If you need basic color output with a straightforward API, color might be the better choice. However, if you require more advanced color manipulation features or better Windows support, gookit/color could be the more suitable option.

1,707

Advanced ANSI style & color support for your terminal applications

Pros of termenv

  • Supports a wider range of color spaces (RGB, ANSI, ANSI256, TrueColor)
  • Provides additional features like cursor movement and screen clearing
  • Offers more advanced styling options, including gradients and hyperlinks

Cons of termenv

  • Less widely adopted compared to color
  • May have a steeper learning curve due to more advanced features
  • Slightly larger package size

Code Comparison

color:

color.New(color.FgBlue).Add(color.Bold).Println("Hello, World!")

termenv:

p := termenv.ColorProfile()
s := termenv.String("Hello, World!").Foreground(p.Color("blue")).Bold()
fmt.Println(s)

Both libraries provide similar functionality for basic color output, but termenv offers more advanced features and flexibility in color spaces. color has a simpler API for basic use cases, while termenv provides more options for complex terminal styling. The choice between the two depends on the specific requirements of your project and the level of terminal manipulation needed.

1,416

Golang ultimate ANSI-colors that supports Printf/Sprintf methods

Pros of Aurora

  • Supports 256 colors and true color (24-bit), offering a wider range of color options
  • Provides color mixing functionality, allowing for more complex color combinations
  • Includes built-in support for gradients and color fading effects

Cons of Aurora

  • Less actively maintained compared to Color (last commit was in 2019)
  • Fewer GitHub stars and contributors, potentially indicating a smaller community
  • Documentation is less comprehensive and may be outdated

Code Comparison

Aurora:

aurora.Red("This is a red string").Bold()
aurora.BgGreen("This has a green background")
aurora.Cyan("This is cyan").Italic().BgBrightWhite()

Color:

color.Red("This is a red string")
color.Set(color.FgGreen, color.BgBlack)
fmt.Println("This has a green foreground and black background")
color.Unset()

Both libraries offer similar functionality for basic color output, but Aurora provides more advanced features like chaining and color mixing. Color, on the other hand, has a simpler API and is more widely adopted in the Go community. The choice between the two depends on the specific requirements of your project and the level of color customization needed.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

color PkgGoDev

Color lets you use colorized outputs in terms of ANSI Escape Codes in Go (Golang). It has support for Windows too! The API can be used in several ways, pick one that suits you.

Color

Install

go get github.com/fatih/color

Examples

Standard colors

// Print with default helper functions
color.Cyan("Prints text in cyan.")

// A newline will be appended automatically
color.Blue("Prints %s in blue.", "text")

// These are using the default foreground colors
color.Red("We have red")
color.Magenta("And many others ..")

Mix and reuse colors

// Create a new color object
c := color.New(color.FgCyan).Add(color.Underline)
c.Println("Prints cyan text with an underline.")

// Or just add them to New()
d := color.New(color.FgCyan, color.Bold)
d.Printf("This prints bold cyan %s\n", "too!.")

// Mix up foreground and background colors, create new mixes!
red := color.New(color.FgRed)

boldRed := red.Add(color.Bold)
boldRed.Println("This will print text in bold red.")

whiteBackground := red.Add(color.BgWhite)
whiteBackground.Println("Red text with white background.")

Use your own output (io.Writer)

// Use your own io.Writer output
color.New(color.FgBlue).Fprintln(myWriter, "blue color!")

blue := color.New(color.FgBlue)
blue.Fprint(writer, "This will print text in blue.")

Custom print functions (PrintFunc)

// Create a custom print function for convenience
red := color.New(color.FgRed).PrintfFunc()
red("Warning")
red("Error: %s", err)

// Mix up multiple attributes
notice := color.New(color.Bold, color.FgGreen).PrintlnFunc()
notice("Don't forget this...")

Custom fprint functions (FprintFunc)

blue := color.New(color.FgBlue).FprintfFunc()
blue(myWriter, "important notice: %s", stars)

// Mix up with multiple attributes
success := color.New(color.Bold, color.FgGreen).FprintlnFunc()
success(myWriter, "Don't forget this...")

Insert into noncolor strings (SprintFunc)

// Create SprintXxx functions to mix strings with other non-colorized strings:
yellow := color.New(color.FgYellow).SprintFunc()
red := color.New(color.FgRed).SprintFunc()
fmt.Printf("This is a %s and this is %s.\n", yellow("warning"), red("error"))

info := color.New(color.FgWhite, color.BgGreen).SprintFunc()
fmt.Printf("This %s rocks!\n", info("package"))

// Use helper functions
fmt.Println("This", color.RedString("warning"), "should be not neglected.")
fmt.Printf("%v %v\n", color.GreenString("Info:"), "an important message.")

// Windows supported too! Just don't forget to change the output to color.Output
fmt.Fprintf(color.Output, "Windows support: %s", color.GreenString("PASS"))

Plug into existing code

// Use handy standard colors
color.Set(color.FgYellow)

fmt.Println("Existing text will now be in yellow")
fmt.Printf("This one %s\n", "too")

color.Unset() // Don't forget to unset

// You can mix up parameters
color.Set(color.FgMagenta, color.Bold)
defer color.Unset() // Use it in your function

fmt.Println("All text will now be bold magenta.")

Disable/Enable color

There might be a case where you want to explicitly disable/enable color output. the go-isatty package will automatically disable color output for non-tty output streams (for example if the output were piped directly to less).

The color package also disables color output if the NO_COLOR environment variable is set to a non-empty string.

Color has support to disable/enable colors programmatically both globally and for single color definitions. For example suppose you have a CLI app and a -no-color bool flag. You can easily disable the color output with:

var flagNoColor = flag.Bool("no-color", false, "Disable color output")

if *flagNoColor {
	color.NoColor = true // disables colorized output
}

It also has support for single color definitions (local). You can disable/enable color output on the fly:

c := color.New(color.FgCyan)
c.Println("Prints cyan text")

c.DisableColor()
c.Println("This is printed without any color")

c.EnableColor()
c.Println("This prints again cyan...")

GitHub Actions

To output color in GitHub Actions (or other CI systems that support ANSI colors), make sure to set color.NoColor = false so that it bypasses the check for non-tty output streams.

Todo

  • Save/Return previous values
  • Evaluate fmt.Formatter interface

Credits

License

The MIT License (MIT) - see LICENSE.md for more details