<br />

<p>
<a href="https://itty.dev/itty-chroma" target="_blank">
  <img src="https://github.com/user-attachments/assets/cfe915d5-63ce-4297-83ef-316426c7af57" alt="itty-chroma" height="120" />
</a>
</p>

[![Version](https://img.shields.io/npm/v/itty-chroma.svg?style=flat-square)](https://npmjs.com/package/itty-chroma)
[![Bundle Size](https://deno.bundlejs.com/?q=itty-chroma&badge&badge-style=flat-square)](https://deno.bundlejs.com/?q=itty-chroma)
[![Coverage Status](https://img.shields.io/coveralls/github/kwhitley/itty-chroma?style=flat-square)](https://coveralls.io/github/kwhitley/itty-chroma)
[![Issues](https://img.shields.io/github/issues/kwhitley/itty-chroma?style=flat-square)](https://coveralls.io/github/kwhitley/itty-chroma)
[![Discord](https://img.shields.io/discord/832353585802903572?label=Discord&logo=Discord&style=flat-square&logoColor=fff)](https://discord.gg/53vyrZAu9u)

###  [v1 Documentation](https://itty.dev/itty-chroma) &nbsp;| &nbsp; [Discord](https://discord.gg/53vyrZAu9u)

---

# Easy, flexible styling in the browser console.
...for the rest of us (and only 500 bytes).

<!--![image](https://github.com/user-attachments/assets/1ac23229-111c-4434-a6ce-379b55d71a71)-->

### Example
```ts
// keep it simple
chroma.red.log('This will be red.')

// or play a little
chroma.log(
  chroma.green,                // set the color to green
  'This is green',
  '{ foo: 'bar' }',
  chroma.blue.underline.bold,  // now switch to blue
  'Now this is blue.',
)
```
![image](https://github.com/user-attachments/assets/7f84014b-97e1-474f-8020-3430efd3e0c6)

<br />

# Quick Start

### Option 1: Import
```ts
import { chroma } from 'itty-chroma'
```

### Option 2: Just copy this snippet:
<!-- BEGIN SNIPPET -->
```ts
let t=(e="",o)=>new Proxy((...t)=>{if(!t.length&&!e)return;let r,i=[e],n="%c",l=e.match(/pad|dec/);for(let e of t)e?.zq&&(e=e()),e?.[0]?.startsWith?.("%c")?(r=e[1].match(/pad|dec/),l&&(n=n.slice(0,-1)),l&&!r&&(n+="%c ",i.push("")),n+=e[0],i.push(...e.slice(1)),l=r):(n+="object"==typeof e?"%o ":"%s ",i.push(e));return o?console[o](n.trim(),...i):[n,...i]},{get(r,i){let n=r=>i=>t(e+(r?r+":"+i:i)+";",o);return"color"==i?n(i):"bold"==i?n("font-weight")(i):"italic"==i?n("font-style")(i):"underline"==i?n("text-decoration")(i):"strike"==i?n("text-decoration")("line-through"):"font"==i?n("font-family"):"size"==i?n("font-size"):"bg"==i?n("background"):"radius"==i?n("border-radius"):"padding"==i||"border"==i?n(i):"style"==i?n(""):"log"==i||"warn"==i||"error"==i?t(e,i):n("color")(i)}}),chroma=t();
```
<!-- END SNIPPET -->
_Note: This will lose TypeScript support, but is great for adding to your browser console (via script extensions, etc)._

<br />

# How it Works

Chroma is just a passthrough for `console`, handling the style assembly, shorthand style props, and chaining.  This means you can pass in any number of args, of any type.  However, only non-objects (strings, numbers, etc) will be colored.

If used with `log/warn/error`, it will assemble and print the statement, just like `console.log/warn/error`.  If you *don't* include `log/warn/error`, it will output a style token instead.

With that in mind:

### 1. Use `chroma` instead of `console` to print something
```ts
chroma.log('text') // console.log('text')
chroma.warn('text') // console.warn('text')
chroma.error('text') // console.error('text')
```
![image](https://github.com/user-attachments/assets/0c82f3e9-0fae-4e4a-a021-6d334874ed00)


### 2. Add styles by chaining style properties
```ts
// call a segment directly, using .log
chroma.bold.red.log('This will be red.')
```
![image](https://github.com/user-attachments/assets/63a78004-87f2-4bf2-ba9e-60407b986419)

### 3. Or compose using chroma segments
```ts
chroma.log(
  chroma.bold.green,
  'This will be green.'
)
```
![image](https://github.com/user-attachments/assets/04a68ebd-3c46-45cc-ad71-9ed8e68b98fc)

These can be saved for re-use:
```ts
const blue = chroma.bold.blue

chroma.log(
  blue,
  'This will be blue.'
)
```
![image](https://github.com/user-attachments/assets/d1083073-f33d-4356-8b21-37ae02fe0d3c)

They can also be saved with the `.log` as a custom logger:
```ts
const ittyLogger = chroma.bold.color("#f0c").log

ittyLogger('This will be itty-colored.')
```
![image](https://github.com/user-attachments/assets/0a2e05aa-923c-4d47-98b8-bf3f583a3cf4)

### 4. Any valid CSS color name works (100% support)
```ts
chroma.salmon.log('This is salmon.')
chroma.cornflowerblue.log('This is cornflowerblue.')
chroma.cornFlowerBlue.log('Case does not matter here...')
```
![image](https://github.com/user-attachments/assets/b363fcec-a289-4f25-af8c-d3d5f31e532f)

### 5. All valid CSS works within properties that expect a value
```ts
chroma
  .color('rgba(255,0,100,0.4)')
  .log('This works just fine')
```
![image](https://github.com/user-attachments/assets/98f978a0-87b6-4488-8f22-696452e927d0)

### 6. ...or use your own custom CSS with `.style(css: string)`
```ts
chroma
  .size('2em')
  .padding('0.5em')
  .style('text-transform:uppercase; text-shadow:0 0 0.5em magenta;')
  .log('So does this')
```
![image](https://github.com/user-attachments/assets/3a6e5bcf-99ab-4616-9794-579c2e0e6cc8)

### 7. A style will continue until replaced, or cleared using **`chroma.clear`**
```ts
chroma.log(
  chroma.red('this will be red'),
  '...but so will this',
  chroma.clear,
  'back to unformatted text'
)
```
![image](https://github.com/user-attachments/assets/d970e8c1-1249-4a39-a183-845ccd5d841f)

### 8. Example: Creating custom log functions
```ts
// we define a curried function to accept some args now, some later
const createLogger = (type = 'log', label, badge = 'grey', text = 'grey') =>
  (...args) =>
    chroma[type](
      chroma.bg(badge).white.bold.padding('2px 5px 1px').radius('0.2rem')(label),
      chroma.color(text).italic,
      ...args,
    )

// our loggers are partial executions
const info = createLogger('log', 'INFO', 'green')
const warning = createLogger('warn', 'WARNING', 'orange', 'brown')

// and we finally call them to log messages
info('This is just a helpful bit of info!')
warning('But this is a more serious warning text...')
```
![image](https://github.com/user-attachments/assets/58cdbcbb-51c3-4b67-8fe8-323bf3a094cd)

<br />

# Supported Properties

| PROPERTY | DESCRIPTION | EXAMPLE(s) |
| --- | --- | --- |
| **.log** | once executed, will output to console.log | `chroma.log('hello')` |
| **.warn** | once executed, will output to console.warn | `chroma.warn('warning text')` |
| **.error** | once executed, will output to console.error | `chroma.error('error text')` |
| **.bold** | bold text | `chroma.bold('this is bold')`, `chroma.bold.red('this is bold and red')` |
| **.italic** | italicized text | `chroma.italic('this is italic')` |
| **.underline** | underlined text | `chroma.underline('text')` |
| **.strike** | text with a line through it | `chroma.strike('this text was removed')` |
| **.{colorName}** | sets text color, supports any valid CSS color name | `chroma.magenta`, `chroma.lightGreen` |
| **.color(value)** | sets font color, supports any valid CSS color | `chroma.color('white')`, `chroma.color('rgba(255,0,0,0.2)')` |
| **.font(value)** | sets font, supports any valid CSS font-family | `chroma.font('Georgia')` |
| **.size(value)** | sets font size | `chroma.size('0.8rem')` |
| **.bg(value)** | sets background, supports any valid CSS background | `chroma.bg('salmon')` |
| **.radius(value)** | sets border-radius (for badges) | `chroma.radius('4px')` |
| **.border(value)** | sets border style | `chroma.border('double 5px red')` |
| **.padding(value)** | sets padding | `chroma.padding('2px 5px')` |
| **.style(value)** | sets custom CSS, allowing any valid sequence | `chroma.style('text-transform:uppercase;text-shadow:0 0 0.5rem rgba(255,0,100,0.5)')` |
| **.none**<sup>1</sup> | clears styling for subsequent arguments | `chroma.red('red text', chroma.clear, 'plain text')` |



