CodeSampleX

Ejemplo

ansi-styles 6.2.3: hexToRgb

Muestra verificada para npm ansi-styles 6.2.3: hexToRgb. El contrato se ejecutó en node 22 · linux alpine/x64 · docker y pasó.

sha256:c123e594d651ee38a9f4d45c7d96f3cce013a9a98dc895fe3392bf8accc7f8d4

Esta red ofrece una sola cosa: una muestra que compila. La ejecutó en un sandbox y guardó el recibo firmado. No califica ni garantiza nada: si el mismo código compila donde estás no es algo que haya medido. Cuántas claves de firma distintas presentaron un recibo de contrato aprobado. Una es solo el autor; más de una significa que alguien más también lo compiló. Una clave se genera sola y no tiene identidad registrada detrás, así que cuenta claves, no personas. MIT-0

Evidencia de ejecución

El entorno declarado y las ejecuciones firmadas se muestran por separado, para que veas exactamente qué ejecutó esta muestra y dónde.

Base de evidencia
Contrato firmado aprobado
Recibos de verificación
2
Claves de firma que lo compilaron
1
Entorno declarado node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

Entornos de las ejecuciones de verificación

Entorno Contrato Etapas Ejecución
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5…
2026-08-23
node 22 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22@sha256:8a34c4ab3ea2…
2026-09-07

Caso

HOW
Objetivo
verify ansi-styles.hexToRgb in pkg:npm/ansi-styles@6.2.3
Paquetes
Símbolos
  • ansi-styles.hexToRgb
Entorno
node 22.23.2
Creado
2026-08-23T05:45:07Z

Contrato

  1. ansiStyles.hexToRgb is a function that converts hex color representations to RGB tuple arrays
  2. hexToRgb correctly parses 6-digit hex color strings into [red, green, blue] numbers
  3. hexToRgb correctly parses 3-digit shorthand hex color strings by doubling each hex digit
  4. hexToRgb returns [0, 0, 0] fallback tuple for invalid hex color inputs
  5. RGB values converted by hexToRgb generate valid 24-bit TrueColor ANSI foreground and background escape codes with ansi16m

Archivos

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.js
  • test/contract.js

Descargar el artefacto de código fuente (tar.gz)

Código fuente

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify ansi-styles.hexToRgb in pkg:npm/ansi-styles@6.2.3
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/ansi-styles@6.2.3
Demonstrate these symbols/APIs:
  - ansi-styles.hexToRgb

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:ce60faff7be1e897ef41aacb6a15c96e5701280508d8169d28756ff281c5c6d1","contract":["ansiStyles.hexToRgb is a function that converts hex color representations to RGB tuple arrays","hexToRgb correctly parses 6-digit hex color strings into [red, green, blue] numbers","hexToRgb correctly parses 3-digit shorthand hex color strings by doubling each hex digit","hexToRgb returns [0, 0, 0] fallback tuple for invalid hex color inputs","RGB values converted by hexToRgb generate valid 24-bit TrueColor ANSI foreground and background escape codes with ansi16m"],"goal":"verify ansi-styles.hexToRgb in pkg:npm/ansi-styles@6.2.3","kind":"HOW","packages":["pkg:npm/ansi-styles@6.2.3"],"schemaVersion":1,"symbols":["ansi-styles.hexToRgb"]},"contractCommand":["node","test/contract.js"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"cjs","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/ansi-styles@6.2.3"],"schemaVersion":1,"subject":"pkg:npm/ansi-styles@6.2.3","symbols":["ansi-styles.hexToRgb"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-ansi-styles-hex-to-rgb",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-ansi-styles-hex-to-rgb",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "ansi-styles": "6.2.3"
      }
    },
    "node_modules/ansi-styles": {
      "version": "6.2.3",
      "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-6.2.3.tgz",
      "integrity": "sha512-4Dj6M28JB+oAH8kFkTLUo+a2jwOFkuqb3yucU0CANcRRUbxS0cP0nZYCGjcc3BNXwRIsUVmDGgzawme7zvJHvg==",
      "license": "MIT",
      "engines": {
        "node": ">=12"
      },
      "funding": {
        "url": "https://github.com/chalk/ansi-styles?sponsor=1"
      }
    }
  }
}
package.json
{
  "name": "sample-ansi-styles-hex-to-rgb",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room verification for ansi-styles hexToRgb",
  "main": "src/index.js",
  "scripts": {
    "test": "node test/contract.js"
  },
  "dependencies": {
    "ansi-styles": "6.2.3"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify ansi-styles.hexToRgb in pkg:npm/ansi-styles@6.2.3",
  "kind": "HOW",
  "packages": [
    "pkg:npm/ansi-styles@6.2.3"
  ],
  "symbols": [
    "ansi-styles.hexToRgb"
  ]
}
src/index.js
const ansiStylesModule = require('ansi-styles');

const ansiStyles = ansiStylesModule.default || ansiStylesModule;
const { hexToRgb } = ansiStyles;

/**
 * Converts a hex color string or number to an RGB tuple [r, g, b].
 * @param {string | number} hex
 * @returns {[number, number, number]}
 */
function convertHexToRgb(hex) {
  return ansiStyles.hexToRgb(hex);
}

/**
 * Generates TrueColor (16m) foreground ANSI escape code from a hex color.
 * @param {string | number} hex
 * @returns {string}
 */
function hexToForegroundAnsi(hex) {
  const rgb = ansiStyles.hexToRgb(hex);
  return ansiStyles.color.ansi16m(...rgb);
}

/**
 * Generates TrueColor (16m) background ANSI escape code from a hex color.
 * @param {string | number} hex
 * @returns {string}
 */
function hexToBackgroundAnsi(hex) {
  const rgb = ansiStyles.hexToRgb(hex);
  return ansiStyles.bgColor.ansi16m(...rgb);
}

/**
 * Styles text with TrueColor foreground hex and resets to default.
 * @param {string} text
 * @param {string | number} hex
 * @returns {string}
 */
function colorizeTextWithHex(text, hex) {
  const open = hexToForegroundAnsi(hex);
  const close = ansiStyles.color.close;
  return `${open}${text}${close}`;
}

module.exports = {
  ansiStyles,
  hexToRgb,
  convertHexToRgb,
  hexToForegroundAnsi,
  hexToBackgroundAnsi,
  colorizeTextWithHex,
};
test/contract.js
const assert = require('node:assert');
const {
  ansiStyles,
  hexToRgb,
  convertHexToRgb,
  hexToForegroundAnsi,
  hexToBackgroundAnsi,
  colorizeTextWithHex,
} = require('../src/index.js');

// 1. ansiStyles.hexToRgb is a function that converts hex color representations to RGB tuple arrays
assert.strictEqual(typeof hexToRgb, 'function', 'hexToRgb must be a function');
assert.strictEqual(typeof ansiStyles.hexToRgb, 'function', 'ansiStyles.hexToRgb must be a function');
const initialResult = hexToRgb('#112233');
assert(Array.isArray(initialResult), 'hexToRgb must return an array');
assert.strictEqual(initialResult.length, 3, 'hexToRgb must return a 3-element tuple');

// 2. hexToRgb correctly parses 6-digit hex color strings into [red, green, blue] numbers
assert.deepStrictEqual(hexToRgb('#ffffff'), [255, 255, 255]);
assert.deepStrictEqual(hexToRgb('ffffff'), [255, 255, 255]);
assert.deepStrictEqual(hexToRgb('#000000'), [0, 0, 0]);
assert.deepStrictEqual(hexToRgb('#ff0000'), [255, 0, 0]);
assert.deepStrictEqual(hexToRgb('#00ff00'), [0, 255, 0]);
assert.deepStrictEqual(hexToRgb('#0000ff'), [0, 0, 255]);
assert.deepStrictEqual(hexToRgb('#112233'), [17, 34, 51]);
assert.deepStrictEqual(hexToRgb('#c0ffee'), [192, 255, 238]);
assert.deepStrictEqual(hexToRgb('#abcdef'), [171, 205, 239]);
assert.deepStrictEqual(convertHexToRgb('#c0ffee'), [192, 255, 238]);

// Numeric hex support
assert.deepStrictEqual(hexToRgb(0x112233), [17, 34, 51]);
assert.deepStrictEqual(hexToRgb(0xc0ffee), [192, 255, 238]);

// 3. hexToRgb correctly parses 3-digit shorthand hex color strings by doubling each hex digit
assert.deepStrictEqual(hexToRgb('#123'), [17, 34, 51]);
assert.deepStrictEqual(hexToRgb('123'), [17, 34, 51]);
assert.deepStrictEqual(hexToRgb('#fff'), [255, 255, 255]);
assert.deepStrictEqual(hexToRgb('#000'), [0, 0, 0]);
assert.deepStrictEqual(hexToRgb('#f00'), [255, 0, 0]);
assert.deepStrictEqual(hexToRgb('#0f0'), [0, 255, 0]);
assert.deepStrictEqual(hexToRgb('#00f'), [0, 0, 255]);
assert.deepStrictEqual(hexToRgb('#abc'), [170, 187, 204]);

// 4. hexToRgb returns [0, 0, 0] fallback tuple for invalid hex color inputs
assert.deepStrictEqual(hexToRgb('invalid'), [0, 0, 0]);
assert.deepStrictEqual(hexToRgb(''), [0, 0, 0]);
assert.deepStrictEqual(hexToRgb('#gggggg'), [0, 0, 0]);
assert.deepStrictEqual(hexToRgb('12'), [0, 0, 0]);

// 5. RGB values converted by hexToRgb generate valid 24-bit TrueColor ANSI foreground and background escape codes with ansi16m
const fgCode = hexToForegroundAnsi('#c0ffee');
assert.strictEqual(fgCode, '\u001B[38;2;192;255;238m', 'Foreground TrueColor ANSI sequence mismatch');

const bgCode = hexToBackgroundAnsi('#c0ffee');
assert.strictEqual(bgCode, '\u001B[48;2;192;255;238m', 'Background TrueColor ANSI sequence mismatch');

const styledText = colorizeTextWithHex('Sample test string', '#112233');
assert.strictEqual(styledText, '\u001B[38;2;17;34;51mSample test string\u001B[39m');

console.log('All contract assertions passed successfully.');

Seeder de origen

anónimo