Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

💄 Add iterm themes #14

Merged
merged 2 commits into from
Jul 15, 2021
Merged

Conversation

metalelf0
Copy link
Contributor

This adds three iterm themes, generated by converting the existing kitty ones manually to xresources format and converting them to iterm2 via terminal.sexy. The same thing can be easily done for other terminals, if needed.

This adds three iterm themes, generated by converting the existing
kitty ones manually to xresources format and converting them to iterm2
via terminal.sexy. The same thing can be easily done for other
terminals, if needed.
Copy link
Member

@ful1e5 ful1e5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do with Lua templating. That reduces pain afterward when we change colors inside colors.lua.

Reference extra lua module

@metalelf0
Copy link
Contributor Author

I considered that, but I'm not that proficient with lua and preferred to provide a quick solution for people wanting iterm themes.

@metalelf0
Copy link
Contributor Author

Ok, I've taken a dive into your code. The main problem is that while kitty and alacritty write colors as hex codes (#aabbcc) iterm2 wants them divided in three components (red, green and blue) where each component is represented as a float from 0 to 1. This is then written in an xml file, made of parts like this:

	<key>Ansi 10 Color</key>
	<dict>
		<key>Color Space</key>
		<string>sRGB</string>
		<key>Blue Component</key>
		<real>0.0784313725490196</real>
		<key>Green Component</key>
		<real>0.807843137254902</real>
		<key>Red Component</key>
		<real>0.0784313725490196</real>
	</dict>

Your code would need to be changed a lot to accomodate this: your template helper currently only does simple string interpolation. This means that we'd either need to prepare colors already divided in three components inside colors.lua, or change the template helper to accept functions (e.g. <real>${iterm2_blue_component(black)}</real>. The second solution is IMHO the best one, but I've tried and my lua isn't strong enough to get it working.

Do you have any hint about how to do this? Or any better solution?

Thanks and keep up the great work!

@ful1e5
Copy link
Member

ful1e5 commented Jul 12, 2021

Try this util function for converting hex to RGB. You can also adjust the floating-point with the "%0.16f" part.

local hex2rgb = function(hex)
  local _, redColor, greenColor, blueColor = hex:match("(.)(..)(..)(..)")
  redColor, greenColor, blueColor =
      string.format("%0.16f", (tonumber(redColor, 16) / 255)),
      string.format("%0.16f", (tonumber(greenColor, 16) / 255)),
      string.format("%0.16f", (tonumber(blueColor, 16) / 255))
  return redColor, greenColor, blueColor
end

References

@metalelf0
Copy link
Contributor Author

Hey @ful1e5! I already managed to write a utility function to convert the individual colors. I still have to figure out a solution to properly pass them to the template, as adapting your template helper looks really difficult (instead of a single color, it would need three variables for each color). Maybe there's a simple way of doing that in lua I'm not aware of.

@ful1e5
Copy link
Member

ful1e5 commented Jul 14, 2021

@metalelf0 Can you able share variable names according to colors.lua, Which you placed inside the key section (Ansi 0, Ansi 1, ..).

@ful1e5
Copy link
Member

ful1e5 commented Jul 14, 2021

I'm able to generate an itermcolors scheme with this bit of code. The only problem it has is colors.

lua/extra/init.lua (Modified)

package.path = "./lua/?/init.lua;./lua/?.lua"

local configModule = require("github-theme.config")

local function write(str, fileName)
  print("[write] extra/" .. fileName)
  local file = io.open("extras/" .. fileName, "w")
  file:write(str)
  file:close()
end

local extras = {kitty = "conf", alacritty = "yml", iterm = "itermcolors"}
for _, style in ipairs({"dark", "dimmed", "light"}) do
  configModule.themeStyle = style
  for extra, ext in pairs(extras) do
    local plugin = require("github-theme.extra." .. extra)
    write(plugin[extra](configModule), extra .. "_github_" .. style .. "." .. ext)
  end
end

lua/extra/iterm.lua (New)

local util = require("github-theme.util")
local configModule = require("github-theme.config")

local M = {}

local rgb = function(hex)
  local _, redColor, greenColor, blueColor = hex:match("(.)(..)(..)(..)")
  redColor, greenColor, blueColor =
      string.format("%0.16f", (tonumber(redColor, 16) / 255)),
      string.format("%0.16f", (tonumber(greenColor, 16) / 255)),
      string.format("%0.16f", (tonumber(blueColor, 16) / 255))
  return {r = redColor, g = greenColor, b = blueColor}
end

local keyToXML = function(key, color)
  local xml = util.template([[
	<key>${k} Text Color</key>
]], {k = key:gsub("__", " ")})

  xml = xml .. util.template([[
	<dict>
		<key>Color Space</key>
		<string>sRGB</string>
		<key>Blue Component</key>
		<real>${b}</real>
		<key>Green Component</key>
		<real>${g}</real>
		<key>Red Component</key>
		<real>${r}</real>
	</dict>
]], color)

  return xml
end

function M.iterm(config)
  config = config or configModule.config
  config.transform_colors = true
  local colors = require("github-theme.colors").setup(config)

  local itermColor = {
    Ansi__0 = rgb(colors.black),
    Ansi__1 = rgb(colors.red),
    Ansi__10 = rgb(colors.brightGreen),
    Ansi__11 = rgb(colors.brightYellow),
    Ansi__12 = rgb(colors.brightBlue),
    Ansi__13 = rgb(colors.brightMagenta),
    Ansi__14 = rgb(colors.brightCyan),
    Ansi__15 = rgb(colors.term_fg),
    Ansi__2 = rgb(colors.green),
    Ansi__3 = rgb(colors.yellow),
    Ansi__4 = rgb(colors.blue),
    Ansi__5 = rgb(colors.magenta),
    Ansi__6 = rgb(colors.cyan),
    Ansi__7 = rgb(colors.fg_dark),
    Ansi__8 = rgb(colors.fg_dark),
    Ansi__9 = rgb(colors.brightRed),
    Background = rgb(colors.bg),
    Bold = rgb(colors.fg),
    Cursor = rgb(colors.cursor),
    Cursor__Text = rgb(colors.bg),
    Forground = rgb(colors.fg),
    Selected__Text = rgb(colors.fg),
    Selection = rgb(colors.bg_visual_selection)
  }

  local iterm = [[
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
]]

  for k, c in pairs(itermColor) do iterm = iterm .. keyToXML(k, c) end

  iterm = iterm .. [[
</dict>
</plist>
]]

  return iterm
end

return M

@ful1e5 ful1e5 added this to In progress in Workboard via automation Jul 14, 2021
@ful1e5 ful1e5 added assigned Under progress. enhancement New feature or request Planned Part of a project labels Jul 14, 2021
@metalelf0
Copy link
Contributor Author

Mmm, with this code I get this error when running :luafile lua/github-theme/extra/init.lua: E5113: Error while calling lua chunk: lua/github-theme/extra/init.lua:17: attempt to call a nil value.

@ful1e5
Copy link
Member

ful1e5 commented Jul 15, 2021

try with new window of nvim with nvim --cmd "set rtp+=$(pwd)" . command.

@metalelf0
Copy link
Contributor Author

Ok, running it with make extra works. I've fixed a couple typos and now it should be ready. I tested the color themes in iterm2 and they look ok to me.

Copy link
Member

@ful1e5 ful1e5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing

Workboard automation moved this from In progress to Reviewer approved Jul 15, 2021
@ful1e5 ful1e5 merged commit 706679d into projekt0n:main Jul 15, 2021
Workboard automation moved this from Reviewer approved to Done Jul 15, 2021
@ful1e5 ful1e5 linked an issue Jul 15, 2021 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
assigned Under progress. enhancement New feature or request Planned Part of a project
Projects
Development

Successfully merging this pull request may close these issues.

Support iTerm
2 participants