Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <section class="container">
	<h1>HTML Colour Parsing Demo</h1>

	<div class="input-group">
		<label for="colorInput">Enter text to parse:</label>
		<input type="text" id="colorInput" placeholder="Enter any text (e.g., chucknorris)" />
	</div>

	<div id="steps"></div>

	<div class="color-preview">
		<div class="color-box" id="colorBox"></div>
		<div>
			<p>Final color: <strong id="colorCode"></strong></p>
		</div>
	</div>
	<small>Note that this won't properly handle hard-coded colour values such as 'red' or 'blue'.</small>
</section>
              
            
!

CSS

              
                body {
	font-family: sans-serif;
	background: #ffffff;
}

.container {
	padding: 2rem;
	max-width: 80ch;
		margin: 2rem auto;
}

input[type="text"] {
	width: 100%;
	padding: 0.5rem;
	font-size: 1rem;
	margin-bottom: 1rem;
}

.step-title {
	font-weight: bold;
	margin-bottom: 0.5rem;
	color: #333;
}

.step-content {
	font-family: monospace;
	background: #d4d4d4;
	padding: 0.5rem;
	border-radius: 4px;
	overflow-wrap: break-word;
	white-space: normal;
}

.color-preview {
	display: flex;
	gap: 1rem;
	align-items: center;
	padding: 1rem;
	background: #d4d4d4;
	margin-top: 1rem;
}

.color-box {
	width: 60px;
	height: 60px;
}
              
            
!

JS

              
                function visualizeIEColor(input) {
  const steps = [];
  let workingValue = input;

  // Step 1: Initial Cleanup
  const originalHasHash = workingValue.startsWith("#");
  if (originalHasHash) {
    steps.push({
      title: "1. Initial Cleanup",
      content: `Remove octothorpe: "${workingValue}" → "${workingValue.slice(1)}"`,
      value: workingValue.slice(1)
    });
    workingValue = workingValue.slice(1);
  } else {
    steps.push({
      title: "1. Initial Cleanup",
      content: "Step skipped: No octothorpe (#) found at start of value",
      value: workingValue
    });
  }

  // Step 2: Replace Invalid Characters
  const nonHexPattern = /[^0-9a-f]/gi;
  const hasNonHex = nonHexPattern.test(workingValue);
  if (hasNonHex) {
    const replacedValue = workingValue.replace(nonHexPattern, "0");
    steps.push({
      title: "2. Replace Invalid Characters",
      content: `Replace non-hex characters: "${workingValue}" → "${replacedValue}"`,
      value: replacedValue
    });
    workingValue = replacedValue;
  } else {
    steps.push({
      title: "2. Replace Invalid Characters",
      content: "Step skipped: All characters are valid hexadecimal (0-9, A-F)",
      value: workingValue
    });
  }

  // Step 3: Standardise Length
  const needsPadding = workingValue.length === 0 || workingValue.length % 3 !== 0;
  if (needsPadding) {
    let standardizedLength = workingValue;
    while (standardizedLength.length === 0 || standardizedLength.length % 3 !== 0) {
      standardizedLength += "0";
    }
    steps.push({
      title: "3. Standardise Length",
      content: `Pad to multiple of 3: "${workingValue}" → "${standardizedLength}"`,
      value: standardizedLength
    });
    workingValue = standardizedLength;
  } else {
    steps.push({
      title: "3. Standardise Length",
      content: `Step skipped: Length (${workingValue.length}) is already a multiple of 3`,
      value: workingValue
    });
  }

  // Step 4: Split into RGB Components
  const partLength = Math.floor(workingValue.length / 3);
  const parts = [
    workingValue.slice(0, partLength),
    workingValue.slice(partLength, partLength * 2),
    workingValue.slice(partLength * 2)
  ];
  steps.push({
    title: "4. Split into RGB Components",
    content: `Split into:\nRed: "${parts[0]}"\nGreen: "${parts[1]}"\nBlue: "${parts[2]}"`,
    value: parts.join(",")
  });

  // Step 5: Handle Length
  let processedParts = [...parts];
  let lengthStepContent = [];
  let lengthProcessingNeeded = false;

  // 5a: Truncate to 8 if longer
  if (partLength > 8) {
    lengthProcessingNeeded = true;
    const beforeParts = [...processedParts];
    processedParts = parts.map(part => part.slice(-8));
    lengthStepContent.push(
      `Truncate to 8 characters:\nRed: "${beforeParts[0]}" → "${processedParts[0]}"\nGreen: "${beforeParts[1]}" → "${processedParts[1]}"\nBlue: "${beforeParts[2]}" → "${processedParts[2]}"`
    );
  }

  // 5b: Remove leading zeros while length > 2 and all start with 0
  let leadingZerosRemoved = false;
  while (
    processedParts[0].length > 2 &&
    processedParts.every(part => part.startsWith("0"))
  ) {
    lengthProcessingNeeded = true;
    leadingZerosRemoved = true;
    const beforeParts = [...processedParts];
    processedParts = processedParts.map(part => part.slice(1));
    lengthStepContent.push(
      `Remove leading zeros:\nRed: "${beforeParts[0]}" → "${processedParts[0]}"\nGreen: "${beforeParts[1]}" → "${processedParts[1]}"\nBlue: "${beforeParts[2]}" → "${processedParts[2]}"`
    );
  }

  // 5c: Truncate to first 2 if still longer
  if (processedParts[0].length > 2) {
    lengthProcessingNeeded = true;
    const beforeParts = [...processedParts];
    processedParts = processedParts.map(part => part.slice(0, 2));
    lengthStepContent.push(
      `Truncate to first 2 characters:\nRed: "${beforeParts[0]}" → "${processedParts[0]}"\nGreen: "${beforeParts[1]}" → "${processedParts[1]}"\nBlue: "${beforeParts[2]}" → "${processedParts[2]}"`
    );
  }

  steps.push({
    title: "5. Handle Length",
    content: lengthProcessingNeeded 
      ? lengthStepContent.join("\n\n")
      : `Step skipped: Components already at correct length (${processedParts[0].length} characters)\nRed: "${processedParts[0]}"\nGreen: "${processedParts[1]}"\nBlue: "${processedParts[2]}"`,
    value: processedParts.join(",")
  });

  // Step 6: Final Assembly
  const finalColor = processedParts.join("");
  steps.push({
    title: "6. Final Assembly",
    content: `Combine RGB components: ${processedParts.join(" + ")} = ${finalColor}`,
    value: finalColor
  });

  return {
    steps,
    finalColor
  };
}

function updateVisualization() {
  const input = colorInput.value;
  const { steps, finalColor } = visualizeIEColor(input);

  const stepsContainer = document.getElementById("steps");
  stepsContainer.innerHTML = steps
    .map(
      (step) => `
        <div class="step">
          <div class="step-title">${step.title}</div>
          <pre class="step-content">${step.content}</pre>
        </div>
      `
    )
    .join("");

  const colorHex = "#" + finalColor;
  colorBox.style.backgroundColor = colorHex;
  colorCode.textContent = colorHex;
}

const colorInput = document.getElementById("colorInput");
const colorBox = document.getElementById("colorBox");
const colorCode = document.getElementById("colorCode");

colorInput.addEventListener("input", updateVisualization);

colorInput.value = "chucknorris";
updateVisualization();
              
            
!
999px

Console