Bun APIs overview and reference table
Bun implements native APIs on the Bun global object and through built-in modules, heavily optimized for server-side tasks. The main API categories include: HTTP Server (Bun.serve), Shell ($), Bundler (Bun.build), File I/O (Bun.file, Bun.write, Bun.stdin, Bun.stdout, Bun.stderr), Child Processes (Bun.spawn, Bun.spawnSync), TCP Sockets (Bun.listen, Bun.connect), UDP Sockets (Bun.udpSocket), WebSockets (WebSocket client, Bun.serve server), Transpiler (Bun.Transpiler), Routing (Bun.FileSystemRouter), Streaming HTML (HTMLRewriter), Headless Browser (Bun.WebView), Hashing (Bun.password, Bun.hash, Bun.CryptoHasher, Bun.sha), CSRF Protection (Bun.CSRF.generate, Bun.CSRF.verify), SQLite (bun:sqlite), SQL Client (Bun.SQL, Bun.sql), Redis/Valkey Client (Bun.RedisClient, Bun.redis), FFI (bun:ffi), DNS (Bun.dns.lookup, Bun.dns.prefetch, Bun.dns.getCacheStats), Testing (bun:test), Workers (Worker), Module Loaders (Bun.plugin), Glob (Bun.Glob), Cookies (Bun.Cookie, Bun.CookieMap), Node-API, import.meta, Utilities (Bun.version, Bun.revision, Bun.env, Bun.main), Sleep & Timing (Bun.sleep, Bun.sleepSync, Bun.nanoseconds), Random & UUID (Bun.randomUUIDv7), System & Environment (Bun.which), Comparison & Inspection (Bun.peek, Bun.deepEquals, Bun.deepMatch, Bun.inspect), String & Text Processing (Bun.escapeHTML, Bun.stringWidth, Bun.indexOfLine), URL & Path Utilities (Bun.fileURLToPath, Bun.pathToFileURL), Compression (Bun.gzipSync, Bun.gunzipSync, Bun.deflateSync, Bun.inflateSync, Bun.zstdCompressSync, Bun.zstdDecompressSync, Bun.zstdCompress, Bun.zstdDecompress), Stream Processing (Bun.readableStreamTo*, Bun.readableStreamToBytes, Bun.readableStreamToBlob, Bun.readableStreamToFormData, Bun.readableStreamToJSON, Bun.readableStreamToArray), Memory & Buffer Management (Bun.ArrayBufferSink, Bun.allocUnsafe, Bun.concatArrayBuffers), Module Resolution (Bun.resolveSync), Parsing & Formatting (Bun.semver, Bun.TOML.parse, Bun.XML, Bun.markdown, Bun.color, Bun.Image), and Low-level/Internals (Bun.mmap, Bun.gc, Bun.generateHeapSnapshot, bun:jsc).
Bun philosophy on API design
Bun strives to implement standard Web APIs wherever possible. Bun introduces new APIs primarily for server-side tasks where no standard exists, such as file I/O and starting an HTTP server. In these cases, Bun's approach still builds atop standard APIs like Blob, URL, and Request.
Bun.color() output formats table
Bun.color() supports these output formats: 'css' (e.g. 'red'), 'ansi' (e.g. '\x1b[38;2;255;0;0m'), 'ansi-16' (e.g. '\x1b[91m'), 'ansi-256' (e.g. '\x1b[38;5;196m'), 'ansi-16m' (e.g. '\x1b[38;2;255;0;0m'), 'number' (e.g. 0x1a2b3c), 'rgb' (e.g. 'rgb(255, 99, 71)'), 'rgba' (e.g. 'rgba(255, 99, 71, 0.5)'), 'hsl' (e.g. 'hsl(120, 50%, 50%)'), 'hex' (e.g. '#1a2b3c'), 'HEX' (e.g. '#1A2B3C'), '{rgb}' (e.g. { r: 255, g: 99, b: 71 }), '{rgba}' (e.g. { r: 255, g: 99, b: 71, a: 1 }), '[rgb]' (e.g. [ 255, 99, 71 ]), '[rgba]' (e.g. [ 255, 99, 71, 255]).
Bun.color() accepted input formats
Bun.color() accepts the following input formats: standard CSS color names like 'red', numbers like 0xff0000, hex strings like '#f00', RGB strings like 'rgb(255, 0, 0)', RGBA strings like 'rgba(255, 0, 0, 1)', HSL strings like 'hsl(0, 100%, 50%)', HSLA strings like 'hsla(0, 100%, 50%, 1)', RGB objects like { r: 255, g: 0, b: 0 }, RGBA objects like { r: 255, g: 0, b: 0, a: 1 }, RGB arrays like [255, 0, 0], RGBA arrays like [255, 0, 0, 255], LAB strings like 'lab(50% 50 50)', and anything else that CSS can parse as a single color value.
Bun.color() 'css' format behavior
The 'css' format outputs valid CSS for use in stylesheets, inline styles, CSS variables, or CSS-in-JS. It returns the most compact string representation of the color. For example, Bun.color('red', 'css') returns 'red', and Bun.color(0xff0000, 'css') also returns 'red'.
Bun.color() 'ansi' format auto-detection
The 'ansi' format outputs ANSI escape codes that color text in terminals. It detects the color depth of stdout from environment variables and picks 'ansi-16m', 'ansi-256', or 'ansi-16' accordingly. If stdout doesn't support any form of ANSI color, it returns an empty string.
Bun.color() 'ansi-16m' format (24-bit)
The 'ansi-16m' format outputs 24-bit ANSI colors, which can display 16 million colors but require a modern terminal that supports them. Bun converts the input color to RGBA, then outputs that as an ANSI color. Example: Bun.color('red', 'ansi-16m') returns '\x1b[38;2;255;0;0m'.
Bun.color() 'ansi-256' format algorithm
The 'ansi-256' format approximates the input color to the nearest of the 256 ANSI colors supported by some terminals. To convert from RGBA to one of the 256 ANSI colors, Bun ported the algorithm that tmux uses.
Bun.color() 'ansi-16' format conversion steps
The 'ansi-16' format approximates the input color to the nearest of the 16 ANSI colors supported by most terminals. Bun converts the input to a 24-bit RGB color space, then to 'ansi-256', then to the nearest of the 16 ANSI colors. Example: Bun.color('red', 'ansi-16') returns '\u001b[91m'.
Bun.color() 'number' format usage
The 'number' format outputs the color as a 24-bit number, a compact representation for databases and configuration. Example: Bun.color('red', 'number') returns 16711680.
Bun.color() '{rgba}' object format
The '{rgba}' format outputs an object with type { r: number (0-255), g: number (0-255), b: number (0-255), a: number (0-1) }. The alpha channel is a decimal number between 0 and 1. Example: Bun.color('red', '{rgba}') returns { r: 255, g: 0, b: 0, a: 1 }.
Bun.color() '{rgb}' object format
The '{rgb}' format is similar to '{rgba}' but does not include the alpha channel. It outputs an object with type { r: number (0-255), g: number (0-255), b: number (0-255) }. Example: Bun.color('red', '{rgb}') returns { r: 255, g: 0, b: 0 }.
Bun.color() '[rgb]' array format
The '[rgb]' format is similar to '[rgba]' but does not include the alpha channel. It outputs an array [number, number, number] with red, green, and blue channels. All values are integers from 0 to 255. Example: Bun.color('red', '[rgb]') returns [255, 0, 0].
Bun.color() 'HEX' format (uppercase)
The 'HEX' format outputs an uppercase hex string. Example: Bun.color('red', 'HEX') returns '#FF0000'.
Bun.color() examples for 'css' format
Examples of Bun.color() with 'css' format: Bun.color('red', 'css') returns 'red', Bun.color(0xff0000, 'css') returns 'red', Bun.color('#f00', 'css') returns 'red', Bun.color('#ff0000', 'css') returns 'red', Bun.color('rgb(255, 0, 0)', 'css') returns 'red', Bun.color('rgba(255, 0, 0, 1)', 'css') returns 'red', Bun.color('hsl(0, 100%, 50%)', 'css') returns 'red', Bun.color('hsla(0, 100%, 50%, 1)', 'css') returns 'red', Bun.color({ r: 255, g: 0, b: 0 }, 'css') returns 'red', Bun.color({ r: 255, g: 0, b: 0, a: 1 }, 'css') returns 'red', Bun.color([255, 0, 0], 'css') returns 'red', Bun.color([255, 0, 0, 255], 'css') returns 'red'.
Bun.color() examples for 'ansi' format
Examples of Bun.color() with 'ansi' format: Bun.color('red', 'ansi') returns '\u001b[38;2;255;0;0m', Bun.color(0xff0000, 'ansi') returns '\u001b[38;2;255;0;0m', Bun.color('#f00', 'ansi') returns '\u001b[38;2;255;0;0m', Bun.color('#ff0000', 'ansi') returns '\u001b[38;2;255;0;0m', Bun.color('rgb(255, 0, 0)', 'ansi') returns '\u001b[38;2;255;0;0m', Bun.color('rgba(255, 0, 0, 1)', 'ansi') returns '\u001b[38;2;255;0;0m', Bun.color('hsl(0, 100%, 50%)', 'ansi') returns '\u001b[38;2;255;0;0m', Bun.color('hsla(0, 100%, 50%, 1)', 'ansi') returns '\u001b[38;2;255;0;0m', Bun.color({ r: 255, g: 0, b: 0 }, 'ansi') returns '\u001b[38;2;255;0;0m', Bun.color({ r: 255, g: 0, b: 0, a: 1 }, 'ansi') returns '\u001b[38;2;255;0;0m', Bun.color([255, 0, 0], 'ansi') returns '\u001b[38;2;255;0;0m', Bun.color([255, 0, 0, 255], 'ansi') returns '\u001b[38;2;255;0;0m'.
Bun.color() examples for 'ansi-256' format
Examples of Bun.color() with 'ansi-256' format: Bun.color('red', 'ansi-256') returns '\u001b[38;5;196m', Bun.color(0xff0000, 'ansi-256') returns '\u001b[38;5;196m', Bun.color('#f00', 'ansi-256') returns '\u001b[38;5;196m', Bun.color('#ff0000', 'ansi-256') returns '\u001b[38;5;196m'.
Bun.color() examples for 'ansi-16' format
Examples of Bun.color() with 'ansi-16' format: Bun.color('red', 'ansi-16') returns '\u001b[91m', Bun.color(0xff0000, 'ansi-16') returns '\u001b[91m', Bun.color('#f00', 'ansi-16') returns '\u001b[91m', Bun.color('#ff0000', 'ansi-16') returns '\u001b[91m'.
Bun.color() examples for 'number' format
Examples of Bun.color() with 'number' format: Bun.color('red', 'number') returns 16711680, Bun.color(0xff0000, 'number') returns 16711680, Bun.color({ r: 255, g: 0, b: 0 }, 'number') returns 16711680, Bun.color([255, 0, 0], 'number') returns 16711680, Bun.color('rgb(255, 0, 0)', 'number') returns 16711680, Bun.color('rgba(255, 0, 0, 1)', 'number') returns 16711680, Bun.color('hsl(0, 100%, 50%)', 'number') returns 16711680, Bun.color('hsla(0, 100%, 50%, 1)', 'number') returns 16711680.
Bun.color() examples for '[rgb]' format
Examples of Bun.color() with '[rgb]' format: Bun.color('hsl(0, 0%, 50%)', '[rgb]') returns [128, 128, 128], Bun.color('red', '[rgb]') returns [255, 0, 0], Bun.color(0xff0000, '[rgb]') returns [255, 0, 0], Bun.color({ r: 255, g: 0, b: 0 }, '[rgb]') returns [255, 0, 0], Bun.color([255, 0, 0], '[rgb]') returns [255, 0, 0].
Bun.color() examples for 'HEX' format
Examples of Bun.color() with 'HEX' format: Bun.color('hsl(0, 0%, 50%)', 'HEX') returns '#808080', Bun.color('red', 'HEX') returns '#FF0000', Bun.color(0xff0000, 'HEX') returns '#FF0000', Bun.color({ r: 255, g: 0, b: 0 }, 'HEX') returns '#FF0000', Bun.color([255, 0, 0], 'HEX') returns '#FF0000'.
Bun.color() use cases
Bun.color can be used to: validate and normalize colors to persist in a database (number format is the most database-friendly), convert colors to different formats, color terminal output beyond the basic 16 colors (use 'ansi' to auto-detect terminal color support, or 'ansi-16', 'ansi-256', or 'ansi-16m' to target a specific color depth), format colors for use in CSS injected into HTML, and get the r, g, b, and a color components as JavaScript objects or numbers from a CSS color string.
Bun.color() return value on parse failure
If the input is unknown or fails to parse, Bun.color returns null.
Bun.color() alternative to npm packages
Bun.color is a built-in alternative to the npm packages 'color' and 'tinycolor2', with full support for parsing CSS color strings and zero dependencies.
Bun.color() signature and purpose
Bun.color(input, outputFormat?) uses Bun's CSS parser to parse, normalize, and convert colors from user input to various output formats. It accepts any CSS-parseable color value as input and returns the color in the specified format, or null if parsing fails.