gu-fan / colorv.vim

edit color easy
http://www.vim.org/scripts/script.php?script_id=3597
135 stars 12 forks source link

Incorrect color preview for #888888 in 256-color xterm #18

Closed rmunn closed 11 years ago

rmunn commented 11 years ago

While editing a CSS file, I discovered that #888888 was showing up as the wrong color when I ran :ColorVPreview. Instead of gray, it's showing up as a yellowish-green. #878787 also shows up as the same shade (almost) of yellowish-green, but #777777 shows up as gray. #898989 also shows up properly as gray. Other combinations show odd behavior as well: #8a8787 is gray, but #878a8a is green. The only pattern I've found so far is that the buggy behavior seems to be limited to color codes starting with 8, but I'm not even sure about that since #8a8787 turned out gray (as it should have).

I can reproduce this behavior on two different computers, one running Vim 7.3.429 on Ubuntu Precise and one running Vim 7.3.762 on Ubuntu Raring.

gu-fan commented 11 years ago

Thanks for your feedback. but my tests here goes right :confused:

have your test the color with dropper of gtk colorpicker? (:ColorVPicker) colorpicker

rmunn commented 11 years ago

Here's what I'm getting: Screenshot from 2013-03-10 07:30:10

The grays are the colors they should be; the greens all seem to be #87AF00: Screenshot from 2013-03-10 07:32:08

rmunn commented 11 years ago

I should mention that I'm running Vim in a console, not Gvim. Under Gvim, the colors produced are correct and #888888 is the gray that it should be. But under console Vim, I get the results in the screenshots above.

According to colortest (http://www.vim.org/scripts/script.php?script_id=1349), this is color number 106 in a 256-color xterm:

rmunn@laptop:~/Downloads$ perl colortest 

**************************
*XTERM 256Color Test Chart
**************************
[snip]
 104 :87/87/d7
 105 :87/87/ff
 106 :87/af/00
 107 :87/af/5f
 108 :87/af/87
[snip]

The gray that should have been produced by #888888 is #8a8a8a, which is color 245 in a 256-color xterm. But color 106 is being chosen instead. All the colors produced by colortest look right to my eyes -- they look like what they should look like given their RGB hex values -- so I doubt there's anything screwy going on with my terminal. I think this is a case of the wrong xterm color code being chosen, for some reason I haven't figured out yet.

rmunn commented 11 years ago

I've updated the title of the issue to clarify that the bug only shows up in the console, not in gvim.

rmunn commented 11 years ago

I believe I've found the bug. It's in the following code in autoload/color.vim (and the corresponding function in autoload/colorv/colorv.py):

function! s:hex2term(hex) "{{{
    let [r,g,b]=colorv#hex2rgb(a:hex)

    " NOTE: the grayscale colors.
    if abs(r-g) <=16 &&  abs(g-b) <=16 && abs(r-b) <=16
        if r<=4
            let t_num = 16
        elseif r>= 92 && r<=96
            let t_num = 59
        elseif r>= 132 && r<=136
            let t_num = 106
        elseif r>= 172 && r<= 176
            let t_num = 145
        elseif r>= 212 && r<=216
            let t_num = 188
        elseif r>= 247
            let t_num = 231
        else
            let div = r%10>=3 ? r/10 : r/10-1
            let t_num = div + 232
        endif
    else
        " NOTE: get the idx num of hex in term table.
        for i in ["r", "g" ,"b"]
            if {i} <= 48
                let {i} = 0
            elseif {i} <= 115
                let {i} = 1
            elseif {i} <= 155
                let {i} = 2
            elseif {i} <= 195
                let {i} = 3
            elseif {i} <= 235
                let {i} = 4
            else
                let {i} = 5
            endif
        endfor
        let t_num = r*36 + g*6 + b + 16
    endif
    return t_num
endfunction "}}}

All the other numbers look right, but terminal color 106 (#87af00) should be color 102 (#878787) instead. Here's a patch to fix the bug:

diff --git a/autoload/colorv.vim b/autoload/colorv.vim
index 67157de..7006d23 100644
--- a/autoload/colorv.vim
+++ b/autoload/colorv.vim
@@ -542,7 +542,7 @@ function! s:hex2term(hex) "{{{
         elseif r>= 92 && r<=96
             let t_num = 59
         elseif r>= 132 && r<=136
-            let t_num = 106
+            let t_num = 102
         elseif r>= 172 && r<= 176
             let t_num = 145
         elseif r>= 212 && r<=216
diff --git a/autoload/colorv/colorv.py b/autoload/colorv/colorv.py
index d0b33eb..4e6993b 100644
--- a/autoload/colorv/colorv.py
+++ b/autoload/colorv/colorv.py
@@ -100,7 +100,7 @@ def hex2term(h):
         elif r>= 92 and r<=96:
             t_num = 59
         elif r>= 132 and r<=136:
-            t_num = 106
+            t_num = 102
         elif r>= 172 and r<= 176:
             t_num = 145
         elif r>= 212 and r<=216:
gu-fan commented 11 years ago

ooops, if you find it 20 min earlier, then I don't need to google and look up the color codes...

Thanks for your work :+1:

I just update the fix at almost the same time you post it. 916271d916ce96915814795f362df23a7d9a2c97

rmunn commented 11 years ago

And now that I've pulled the commit, I see that it works. Great; closing issue.