reobin / vcspg

The vim color scheme preview generator
GNU Affero General Public License v3.0
2 stars 0 forks source link

I've had a look =) #1

Closed maurges closed 4 years ago

maurges commented 4 years ago

To address some of your comments in the other thread:

  1. Cycling through words with w is not the greatest idea indeed. Let me try to explain my idea more clearly: you don't actually need the color of each word, you need the color of each syntax group in the file. You can get the color with synIDattr(hlID("SomeSynGroup"), "fg"), and I didn't know these functions yesterday. =) So I just thought that you can position a cursor on a word that has the desired syntax group, and call a function that gets you the color of the word under cursor (this function I remembered from one of my plugins).

  2. If the word has an empty color, then the word is not a member of any syntax group. So you can obtain its color from the group Normal

  3. So the better version of the script would not need an example .js file. Just need a list of syntax groups and to query their colors with the loaded colorscheme. Then you create a .css from those groups and colors.

maurges commented 4 years ago

Also I made some fixes to your code and was too lazy to fork and pull-request, so here's a patch if you want:

From c8453735b0c40bf133fac892b45e92faf2c332ac Mon Sep 17 00:00:00 2001
From: d86leader <d86leader@gmail.com>
Date: Fri, 14 Aug 2020 15:18:53 +0700
Subject: [PATCH] Slight rewrite of getting colors; fixed many problems

---
 src/generate_color_scheme_data.sh |  4 +-
 src/get_color_values.vim          | 67 ++++++++++++++++++++++++++-----
 2 files changed, 59 insertions(+), 12 deletions(-)

diff --git a/src/generate_color_scheme_data.sh b/src/generate_color_scheme_data.sh
index 6b99922..0a96a00 100644
--- a/src/generate_color_scheme_data.sh
+++ b/src/generate_color_scheme_data.sh
@@ -10,8 +10,8 @@ git clone --depth 1 \

 cat set_termguicolors.vim > ~/.vimrc

-echo "colorscheme $name\n" >> ~/.vimrc
+echo "colorscheme $name" >> ~/.vimrc

 cat get_color_values.vim >> ~/.vimrc

-vim -c ':call GetColorValues()' code_sample.js -c ':q'
+vim -c ':call WriteColorValues()' code_sample.js -c ':q'
diff --git a/src/get_color_values.vim b/src/get_color_values.vim
index e3941c7..8997a95 100644
--- a/src/get_color_values.vim
+++ b/src/get_color_values.vim
@@ -1,19 +1,66 @@
-function GetColorValue()
+" TODO: convert color names to color codes via $VIMRUNTIME/rgb.txt
+
+function! GetColorValue() abort
   let l:synID = synID(line("."), col("."), 1)
-  let l:synIDtrans = synIDtrans(l:synID)
-  return "{\"group\":\"" . synIDattr(l:synID, "name") . "\",\"color\":\"" . synIDattr(l:synIDtrans, "fg") . "\"}"
+  let l:name = synIDattr(l:synID, "name")
+  let l:color = synIDattr(l:synID, "fg")
+  if l:name == ""
+    let l:name = "Normal"
+  endif
+  if l:color == ""
+    let l:color = synIDattr(hlID("Normal"), "fg")
+  endif
+  return {l:name: l:color}
+endfunction
+
+function! GetCursorColors() abort
+  " there are also lCursor and CursorIM, but i don't know when they are used
+  return [ {"Cursor": synIDattr(hlID("Cursor"), "bg")}
+       \ , {"CursorLine": synIDattr(hlID("CursorLine"), "bg")}
+       \ , {"CursorLineNr": synIDattr(hlID("CursorLineNr"), "fg")}
+       \ , {"CursorColumn": synIDattr(hlID("CursorColumn"), "bg")}
+       \ , {"MatchParen": synIDattr(hlID("MatchParen"), "bg")}
+       \ ]
+endfunction
+
+function! GetSpecialColors() abort
+  return [ {"LineNr": synIDattr(hlID("LineNr"), "fg")}
+       \ , {"VertSplitFg": synIDattr(hlID("VertSplit"), "fg")}
+       \ , {"VertSplitBg": synIDattr(hlID("VertSplit"), "bg")}
+       \ , {"FoldedFg": synIDattr(hlID("Folded"), "fg")}
+       \ , {"FoldedBg": synIDattr(hlID("Folded"), "bg")}
+       \ ]
+  " Maybe also Directory, DiffStuff, IncSearch, Search, Pmenu, PmenuSel
+  " A lot of interesting things in *highlight-default*
 endfunction

-" get color values of file
-function GetColorValues()
+" get color values of all words in the file + some more
+function! GetColorValues() abort
   call cursor(1, 1)
   let l:index = 0
-  let l:values = "["
-  while l:index < 26
-    let l:values .= GetColorValue() . ","
+  let l:values = []
+  while l:index < 26 " FIXME
+    let l:values += [GetColorValue()]
     normal! w
     let l:index += 1
   endwhile
-  let l:values .= "]"
-  call writefile([l:values], "data.json", "b")
+  " add cursor colors as well
+  let l:values += GetCursorColors()
+  " add background color
+  let l:values += [{"Background": synIDattr(hlID("Normal"), "bg")}]
+  " add other important colors
+  let l:values += GetSpecialColors()
+
+  " Note: most colorschemes have a problem that when you move a cursor on a
+  " brace, the highlight of it and the matching brace are just awful, to the
+  " point where I can't find where the cursor is at all. The colors are
+  " captured as MatchParen, but i don't know how to show them
+
+  call sort(l:values)
+  call uniq(l:values)
+  return l:values
+endfunction
+
+function WriteColorValues() abort
+  call writefile([json_encode(GetColorValues())], "data.json")
 endfunction

base-commit: 130ed470739744ff536b5bb6240f0c39b2f43ae3
-- 
2.26.2

Also if you have some problems, we can talk via email which should be visible in the patch. Thanks and have a good time!

reobin commented 4 years ago

@d86leader Thank you so much! Going away for the weekend I'll have a look on Monday.

Have a good one!

reobin commented 4 years ago

@d86leader This works pretty well! Thank you, man!

About what you said about not needing a code sample file: I actually like being able to feed a file for which I want the color values. This gives me the possibility of very easily change the sample code that will be shown with every color scheme on the website.

I might change it later, but for now, I'll keep it and build a CSS file from that.

Again, thank you for this contribution to vimcolorschemes. I'll probably have more questions later, so I'll keep your contact close.