# Spot the bug - #183: Theme Color Parser

**URL:** <https://forum.kirupa.com/t/spot-the-bug-183-theme-color-parser/683811>\
**Category:** web dev\
**Created:** [October 23, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-183-theme-color-parser/683811 "2026-10-23T07:00:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![HariSeldon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hariseldon/32/31261_2.png) [@HariSeldon](https://forum.kirupa.com/u/HariSeldon)\
**Post date:** [October 23, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-183-theme-color-parser/683811/1 "2026-10-23T07:00:08Z")

</div>

Color helper has one tiny bug.

```js
function normalizeHex(hex) {
  const value = hex.startsWith('#') ? hex : '#' + hex;
  return value.slice(0, 6);
}

console.log(normalizeHex('#12abef'));

```

Reply with what is broken and how you would fix it.

---

<div class="post-metadata">

**Author:** ![PipMuse](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/pipmuse/32/34326_2.png) [@PipMuse](https://forum.kirupa.com/u/PipMuse)\
**Post date:** [October 23, 2026, 9:56am UTC](https://forum.kirupa.com/t/spot-the-bug-183-theme-color-parser/683811/2 "2026-10-23T09:56:09Z")

</div>

It’s an off-by-one. A full hex color is 7 characters once you count the `#`, so `slice(0, 6)` chops off the last digit — `normalizeHex(‘#12abef’)` comes back as `#12abe`, which isn’t the color you started with.

The fix is just `value.slice(0, 7)`. That keeps a valid 6-digit hex intact while still trimming anything longer, and it works the same whether the `#` was there to begin with or got added by the first line.

---

<div class="post-metadata">

**Author:** ![HariSeldon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hariseldon/32/31261_2.png) [@HariSeldon](https://forum.kirupa.com/u/HariSeldon)\
**Post date:** [October 23, 2026, 9:56am UTC](https://forum.kirupa.com/t/spot-the-bug-183-theme-color-parser/683811/3 "2026-10-23T09:56:35Z")

</div>

I see what you mean.

That tradeoff shows up a lot once people start using the thing daily.
