Skip to content

Highlights: invert dialog colors in night mode#14923

Merged
hius07 merged 1 commit into
koreader:masterfrom
hius07:invert-hl-colors
Feb 7, 2026
Merged

Highlights: invert dialog colors in night mode#14923
hius07 merged 1 commit into
koreader:masterfrom
hius07:invert-hl-colors

Conversation

@hius07

@hius07 hius07 commented Feb 4, 2026

Copy link
Copy Markdown
Member

@Frenzie Frenzie added this to the 2026.02 milestone Feb 4, 2026
if Screen.night_mode then
local r, g, b = Blitbuffer.HIGHLIGHT_COLORS[color_name]:match("#(..)(..)(..)")
return Blitbuffer.colorFromString(string.format("#%02x%02x%02x",
255 - tonumber(r, 16), 255 - tonumber(g, 16), 255 - tonumber(b, 16)))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think bit.bxor(tonumber(r, 16), 0xFF) should do the same thing fwiw. (It doesn't matter here with just a few colors, but presumably it would when processing a picture pixel by pixel.)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

As far as AI advises, bit.bxor(x, 0xFF) is not faster than 255 - x.

@Frenzie Frenzie Feb 5, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Better to use AI to write a test in less than a minute than to ask it things it can't know. ;-)

local bit = require("bit")
local bxor = bit.bxor  -- local cache

local function test(n)
  local clock = os.clock
  
  -- prebuild test data as hex strings, numbers
  local hex = {}
  local nums = {}
  for x = 0, 255 do
    hex[x]  = ("%02X"):format(x)
    nums[x] = x
  end

  -- warm-up
  for i = 1, 10000 do
    local x = nums[i % 256]
    local _ = bxor(x, 0xFF)
    local _ = 255 - x
  end

  print("=== Original: bxor(tonumber(x, 16), 0xFF) ===")
  local start = clock()
  for i = 1, n do
    local h = hex[i % 256]
    local x = tonumber(h, 16)
    local y = bxor(x, 0xFF)
  end
  print(("time: %.6f s"):format(clock() - start))

  print("=== Cached bxor(x, 0xFF) ===")
  start = clock()
  for i = 1, n do
    local x = nums[i % 256]
    local y = bxor(x, 0xFF)
  end
  print(("time: %.6f s"):format(clock() - start))

  print("=== 255 - x (arithmetic) ===")
  start = clock()
  for i = 1, n do
    local x = nums[i % 256]
    local y = 255 - x
  end
  print(("time: %.6f s"):format(clock() - start))
  
  print()
end

-- run with e.g. luajit test.lua 100000000
local n = tonumber(arg[1]) or 1e8
test(n)
=== Original: bxor(tonumber(x, 16), 0xFF) ===
time: 2.501012 s
=== Cached bxor(x, 0xFF) ===
time: 0.042331 s
=== 255 - x (arithmetic) ===
time: 0.044898 s
=== Original: bxor(tonumber(x, 16), 0xFF) ===
time: 2.486529 s
=== Cached bxor(x, 0xFF) ===
time: 0.042634 s
=== 255 - x (arithmetic) ===
time: 0.040219 s

tl;dr There's no difference, at least on this AMD Ryzen 7 7800X3D. So I'm still wrong. :-) (I'll have to double check if it's the same on some actual ereaders though.)

@hius07 hius07 merged commit 0115692 into koreader:master Feb 7, 2026
4 checks passed
@hius07 hius07 deleted the invert-hl-colors branch February 7, 2026 16:15
0xstillb pushed a commit to 0xstillb/koreader-thai that referenced this pull request May 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Preview of highlight colour displayed inverted

2 participants