# Converting to index color gives incorrect results

**URL:** https://community.aseprite.org/t/converting-to-index-color-gives-incorrect-results/2200
**Category:** Bug Reports
**Created:** [November 4, 2018, 3:39am UTC](https://community.aseprite.org/t/converting-to-index-color-gives-incorrect-results/2200 "2018-11-04T03:39:29Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![DudleyWaffles](https://community.aseprite.org/letter_avatar/dudleywaffles/32/5_5575768a8748004e209b776fc1b2916d.png) [@DudleyWaffles](https://community.aseprite.org/u/DudleyWaffles)
#### Post date: [November 4, 2018, 3:39am UTC](https://community.aseprite.org/t/converting-to-index-color-gives-incorrect-results/2200/1 "2018-11-04T03:39:29Z")

</div>

Hi, I love the program but I’ve got a problem with the 1.2.9 I currently have. When I bring in art from another program, make a palette from the art, and then downsample to index color the colors chosen aren’t always the correct ones even if they exist in the palette. I think this can be reproduced easily.

-Make a new document (rgb).  
-Put two colors down but make them only one or two integers apart in one channel.  
-Make palette from image (should end up having only have three entries with the background color).  
-Convert to Index color.

At this point Aseprite will just make both strokes the same color of one of the palette colors even though the palette contains both colors.

This is kind of a big problem because I have a habit of making images in other programs sometimes and am losing info when I try to bring them over. I’d appreciate any help or advice about a different process or setting I could use.

---

<div class="post-metadata">

### Author: ![DudleyWaffles](https://community.aseprite.org/letter_avatar/dudleywaffles/32/5_5575768a8748004e209b776fc1b2916d.png) [@DudleyWaffles](https://community.aseprite.org/u/DudleyWaffles)
#### Post date: [November 7, 2018, 1:16am UTC](https://community.aseprite.org/t/converting-to-index-color-gives-incorrect-results/2200/2 "2018-11-07T01:16:34Z")

</div>

Well if anyone reads this could you take a sec to check if you can even re-create this behavior? I’d really like to know if it’s the program or if I’ve just been slipped the crazy pills.

---

<div class="post-metadata">

### Author: ![KashouC](https://community.aseprite.org/user_avatar/community.aseprite.org/kashouc/32/1404_2.png) [@KashouC](https://community.aseprite.org/u/KashouC)
#### Post date: [November 7, 2018, 2:15pm UTC](https://community.aseprite.org/t/converting-to-index-color-gives-incorrect-results/2200/3 "2018-11-07T14:15:09Z")

</div>

Yup. If I draw two lines where one is #527b2e (82,107,46) while the other one is #526a2f (82, 106, 47), then make sure both are in the palette and switch to indexed mode they both become #526b2e.

I have no idea why this is the case. All I can think of is that either it’s a bug, or it’s working as intended and the indexed limitation causing this (or warranting this behavior) exists within the file types somehow.

---

<div class="post-metadata">

### Author: ![dacap](https://community.aseprite.org/user_avatar/community.aseprite.org/dacap/32/10_2.png) [@dacap](https://community.aseprite.org/u/dacap)
#### Post date: [November 13, 2018, 1:21pm UTC](https://community.aseprite.org/t/converting-to-index-color-gives-incorrect-results/2200/4 "2018-11-13T13:21:32Z")

</div>

I’ve to check some issues converting RGB -\> Indexed (even more, replacing the whole algorithm to generate palettes from RGB images). So it’s something with high priority.

---

<div class="post-metadata">

### Author: ![dacap](https://community.aseprite.org/user_avatar/community.aseprite.org/dacap/32/10_2.png) [@dacap](https://community.aseprite.org/u/dacap)
#### Post date: [November 13, 2018, 1:40pm UTC](https://community.aseprite.org/t/converting-to-index-color-gives-incorrect-results/2200/5 "2018-11-13T13:40:53Z")

</div>

I’ve found the previous issue reporting this:

> <https://github.com/aseprite/aseprite/issues/1385>
>
> There is a problem with the way ASEprite 1.1.13 determines palette indices for very similar values when converting from RGB to...

---

<div class="post-metadata">

### Author: ![DudleyWaffles](https://community.aseprite.org/letter_avatar/dudleywaffles/32/5_5575768a8748004e209b776fc1b2916d.png) [@DudleyWaffles](https://community.aseprite.org/u/DudleyWaffles)
#### Post date: [November 14, 2018, 8:29pm UTC](https://community.aseprite.org/t/converting-to-index-color-gives-incorrect-results/2200/6 "2018-11-14T20:29:48Z")

</div>

Thanks for posting here.

Can I make a suggestion, if there’s going to be a different system can there be an option for an exact mode? By that I mean the function would just be two loops where for each pixel of the document, it checks every pixel of the palette, and if an exact match is found then it’s accepted and if it isn’t then instead of choosing the next best color it actually stops the process and alerts the user?

Those of us who get really anal about colors would appreciate it.

---

<div class="post-metadata">

### Author: ![DudleyWaffles](https://community.aseprite.org/letter_avatar/dudleywaffles/32/5_5575768a8748004e209b776fc1b2916d.png) [@DudleyWaffles](https://community.aseprite.org/u/DudleyWaffles)
#### Post date: [September 13, 2019, 4:48pm UTC](https://community.aseprite.org/t/converting-to-index-color-gives-incorrect-results/2200/7 "2019-09-13T16:48:53Z")

</div>

So it’s been about a year since I posted this, although it’s been 2 and a half years since someone made this other thread…

> <https://github.com/aseprite/aseprite/issues/1385>
>
> There is a problem with the way ASEprite 1.1.13 determines palette indices for very similar values when converting from RGB to...

I tried the latest patches and I’m glad to see all the other bug fixes and features but I’m still getting the issue here. Is this something that is on the radar at all or should I stop worrying about it?

I feel the need to ask because I still find myself having to double-check doodles that I might start in Photoshop or something when I want to turn them into something more orderly in Aseprite.

---

<div class="post-metadata">

### Author: ![2blackbar](https://community.aseprite.org/user_avatar/community.aseprite.org/2blackbar/32/3715_2.png) [@2blackbar](https://community.aseprite.org/u/2blackbar)
#### Post date: [June 9, 2020, 9:07am UTC](https://community.aseprite.org/t/converting-to-index-color-gives-incorrect-results/2200/8 "2020-06-09T09:07:32Z")

</div>

I agree this is important step when creating sprites, it makes them unusable, this even shouldnt be in aseprite cause results are very random when converting from non indexed pngs, there should be some way to at least feed it some palette, JASC animation shop has this, and works better to convert 24 bit pngs to indexed.There is also program called PALAPPLY that can do this .Maybe it could be merged into aseprite ? Please fix this !

---

<div class="post-metadata">

### Author: ![dacap](https://community.aseprite.org/user_avatar/community.aseprite.org/dacap/32/10_2.png) [@dacap](https://community.aseprite.org/u/dacap)
#### Post date: [June 10, 2020, 12:33pm UTC](https://community.aseprite.org/t/converting-to-index-color-gives-incorrect-results/2200/9 "2020-06-10T12:33:21Z")

</div>

We have a patch ready to be released, but it needs some testing, we’ll try to include it in a beta version in the Steam branch. Today I would like to release a new version without big changes, and then other beta with those extra changes (this bug fix included).

---

<div class="post-metadata">

### Author: ![Dracobot](https://community.aseprite.org/user_avatar/community.aseprite.org/dracobot/32/4266_2.png) [@Dracobot](https://community.aseprite.org/u/Dracobot)
#### Post date: [September 1, 2020, 7:10pm UTC](https://community.aseprite.org/t/converting-to-index-color-gives-incorrect-results/2200/10 "2020-09-01T19:10:32Z")

</div>

is this bug fixed yet?  
i seem to continue having that issue.  
i’m currently on version v1.2.25

---

<div class="post-metadata">

### Author: ![ash3s](https://community.aseprite.org/letter_avatar/ash3s/32/5_5575768a8748004e209b776fc1b2916d.png) [@ash3s](https://community.aseprite.org/u/ash3s)
#### Post date: [April 11, 2022, 6:27pm UTC](https://community.aseprite.org/t/converting-to-index-color-gives-incorrect-results/2200/11 "2022-04-11T18:27:34Z")

</div>

i’m still having this issue, with \<256 colors , even when I create new palette from the sprite project i’m working on, when I convert it to index, several colors change (with no option to remap). is this something to do with opacities?

---

<div class="post-metadata">

### Author: ![behreandtjeremy](https://community.aseprite.org/user_avatar/community.aseprite.org/behreandtjeremy/32/6606_2.png) [@behreandtjeremy](https://community.aseprite.org/u/behreandtjeremy)
#### Post date: [April 11, 2022, 7:56pm UTC](https://community.aseprite.org/t/converting-to-index-color-gives-incorrect-results/2200/12 "2022-04-11T19:56:05Z")

</div>

Hi @ash3s,

If possible, please post a screen capture and say whether you’re using Octree or Table RGB 5 bits to convert.

For some posters on this thread, the issue is complicated by the fact that images created in Photoshop may have a different color profile than standard RGB. Go to `Sprite > Properties` to check. If the profile does not say `sRGB`, test if `Convert`ing to `SRGB` before creating a palette makes a difference.

If you want to try a [script](https://www.aseprite.org/docs/scripting) that creates a palette, see if this gives you different results:

```lua
-- Change these settings to preference.
local removeAlpha = false
local prependMask = true
local clampTo256 = true

local activeSprite = app.activeSprite
if not activeSprite then return end

-- Optional. Remove double hyphen to re-enable.
-- activeSprite:convertColorSpace(ColorSpace { sRGB = true })

local activeSpec = activeSprite.spec
if activeSpec.colorMode ~= ColorMode.RGB then
    return
end

local alphaMask = 0x0
if removeAlpha then
    alphaMask = 0xff000000
end

local dictionary = {}
local idx = 1

for _, activeFrame in ipairs(activeSprite.frames) do
    local flatImage = Image(activeSpec)
    flatImage:drawSprite(
        activeSprite,
        activeFrame,
        Point(0, 0))
    local itr = flatImage:pixels()

    for elm in itr do
        local hex = elm()
        if ((hex >> 0x18) & 0xff) > 0 then
            hex = alphaMask | hex
            if not dictionary[hex] then
                dictionary[hex] = idx
                idx = idx + 1
            end
        end
    end
end

local hexes = {}
for k, v in pairs(dictionary) do
    hexes[v] = k
end

if prependMask then
    local maskIdx = dictionary[0x0]
    if maskIdx then
        if maskIdx > 1 then
            table.remove(hexes, maskIdx)
            table.insert(hexes, 1, 0x0)
        end
    else
        table.insert(hexes, 1, 0x0)
    end
end

local hexesLen = #hexes
if hexesLen > 0 then
    local palLen = hexesLen
    if clampTo256 then
        palLen = math.min(256, hexesLen)
    end
    local palette = Palette(palLen)
    for i = 1, palLen, 1 do
        local hex = hexes[i]
        local aseColor = Color(
                      hex & 0xff,
            (hex >> 0x08) & 0xff,
            (hex >> 0x10) & 0xff,
            (hex >> 0x18) & 0xff)
        palette:setColor(i - 1, aseColor)
    end
    activeSprite:setPalette(palette)
end

app.refresh()

```

I don’t recommend using this script with a sprite that could generate a large palette (i.e., greater than 256). It could be slow and lock up Aseprite until it’s finished. It is not designed to work with any color mode other than RGB.

See also: [Octree Color Indexed Conversion Testing](https://community.aseprite.org/t/octree-color-indexed-conversion-testing/9682) ,  
[New Script for perfect palette generation from RGB sprite](https://community.aseprite.org/t/new-script-for-perfect-palette-generation-from-rgb-sprite/9043) .

Cheers,  
Jeremy

---

<div class="post-metadata">

### Author: ![ash3s](https://community.aseprite.org/letter_avatar/ash3s/32/5_5575768a8748004e209b776fc1b2916d.png) [@ash3s](https://community.aseprite.org/u/ash3s)
#### Post date: [April 14, 2022, 1:59am UTC](https://community.aseprite.org/t/converting-to-index-color-gives-incorrect-results/2200/13 "2022-04-14T01:59:10Z")

</div>

indexed:

 ![indexed](https://community.aseprite.org/uploads/default/original/2X/5/563538252208040922ab8cf7f618394546b3ba74.png)

rgb:

 ![rgb mode](https://community.aseprite.org/uploads/default/original/2X/8/8dac9477bc1cc4028d73c84e0565e077cd81a7c0.png)

Hi thanks for the reply.

This is from a .gif animation with a 256 color table that I exported from PShop. When I open it in aseprite it looks fine. If I convert it to indexed, the colors slightly shift. This was using the default RGB conversion, not Octree.This is a single layer gif so no opacities or layer modes are being used.

I just upgraded to the beta, and tried the same file conversion and everything looks good!-- I dont see any differences, I think Octree has resolved this issue for me.

---

<div class="post-metadata">

### Author: ![ZechariahB](https://community.aseprite.org/letter_avatar/zechariahb/32/5_5575768a8748004e209b776fc1b2916d.png) [@ZechariahB](https://community.aseprite.org/u/ZechariahB)
#### Post date: [April 18, 2023, 9:13pm UTC](https://community.aseprite.org/t/converting-to-index-color-gives-incorrect-results/2200/14 "2023-04-18T21:13:17Z")

</div>

I do not like necro-bumping an old thread, but I believe mentioning this also has a place in this thread because this is very similar. Although conversion of an RGB color image to indexed seems to have been improved via Octree as depicted above, this still produces inaccurate results as of v1.3-rc2 particularly if you instead use a different palette than the colors of an image. No improvement with Table RGB 5 bits + Alpha 3 bits.

Neither can produce visually accurate results when converting an RGB color sprite to a different palette which often requires manual recoloring. A palette with a difference in hue, saturation, lightness than the source image can make it lighter/darker than it should be or ruin conversion and make the image unusable. Unfortunately as well, I found that conversion from a RGB color sprite to indexed with a limited grayscale palette results in an image darker than it otherwise should be including if the colors and the available grey colors have closely matching luminosity. If an image is converted to grayscale, it can still pick the wrong colors.

I used the eyedropper tool with best fit index on a sprite I made which is equivalent to converting an RGB color sprite to indexed with a palette. The sprite is made of 7 colors. The palette used is a generated gradient of 64 grayscale values between black and white. In the center is the sprite with best fitting values when using that grayscale palette according to the human eye. Depicted with red text is the sprites with the wrong colors. Converting an image to indexed should match pixels of an image to their closest perceptually equivalent color in a palette to match more closely to the manually recolored sprite, but that ideal behavior is not quite here yet.

 ![Aseprite_BFI_Bug](https://community.aseprite.org/uploads/default/original/2X/9/9dfb0178692479739c7aa2a42abef3579b20cfd5.png)
