coolor.js
Version:
Add cool colors to your console output.
105 lines (71 loc) • 3.1 kB
Markdown
# Coolor.js
A lightweight and powerful library for adding colors and styles to your console output. Coolor.js provides an intuitive, chainable API for creating beautiful terminal text.
## Features
- **8 Standard Colors**: Black, red, green, yellow, blue, magenta, cyan, and white
- **Text Styles**: Bold, dim, italic, underline, inverse, hidden, and strikethrough
- **Custom RGB Colors**: Support for 16 million custom colors (RGB)
- **Chainable API**: Combine multiple styles with a fluent interface
- **Zero Dependencies**: Lightweight with no external dependencies
- **TypeScript Support**: Full TypeScript definitions included
## Installation
```bash
npm install coolor.js
pnpm add coolor.js
yarn add coolor.js
bun add coolor.js
```
## Usage
Import the default export from the package:
```javascript
import coolor from 'coolor.js'
const coolor = require('coolor.js').default
```
### Basic Usage
```javascript
// Basic colors
console.log(coolor.red('This text is red'))
console.log(coolor.blue('This text is blue'))
// Background colors
console.log(coolor.bgYellow('This has a yellow background'))
// Text styles
console.log(coolor.bold('This text is bold'))
console.log(coolor.underline('This text is underlined'))
```
### Chaining Styles
You can chain multiple styles together:
```javascript
console.log(coolor.red.bold('Bold red text'))
console.log(coolor.bgBlue.white.underline('White underlined text on blue background'))
```
### Custom RGB Colors
Create your own custom colors using RGB values:
```javascript
// Custom text color
const orange = coolor.customColor({ red: 255, green: 165, blue: 0 })
console.log(orange('This text is orange'))
// Custom background color
const lightBlue = coolor.customBgColor({ red: 173, green: 216, blue: 230 })
console.log(lightBlue('This has a light blue background'))
// Combining custom colors with styles
console.log(orange.bold('Bold orange text'))
```
### Creating Reusable Styles
You can create reusable styles for consistent formatting:
```javascript
const errorStyle = coolor.red.bold.underline
const warningStyle = coolor.yellow.bold
const successStyle = coolor.green.bold
console.log(errorStyle('This is an error message'))
console.log(warningStyle('This is a warning message'))
console.log(successStyle('This is a success message'))
```
## API Reference
### `CoolorError`
A custom error class for handling errors while trying to create custom colors.
This class extends the built-in `Error` class and provides a more descriptive error message when an invalid color is provided.
## Browser Compatibility
Coolor.js is designed to work in modern browsers and Node.js environments. It uses ES6 modules and can be bundled with tools like Webpack or Rollup for browser usage.
## Contributing
Contributions are welcome! If you find a bug or have a feature request, please open an issue or submit a pull request.
## License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.