Skip to content

Commit 160ab68

Browse files
Changed: Use black or white cursor color based on terminal background instead of always white if colors.properties didn't have cursor color set
Credit for algorithm link belong to @Jamie-Landeg-Jones Closes #2653
1 parent 903f249 commit 160ab68

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

terminal-emulator/src/main/java/com/termux/terminal/TerminalColorScheme.java

+23
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ private void reset() {
7171

7272
public void updateWith(Properties props) {
7373
reset();
74+
boolean cursorPropExists = false;
7475
for (Map.Entry<Object, Object> entries : props.entrySet()) {
7576
String key = (String) entries.getKey();
7677
String value = (String) entries.getValue();
@@ -82,6 +83,7 @@ public void updateWith(Properties props) {
8283
colorIndex = TextStyle.COLOR_INDEX_BACKGROUND;
8384
} else if (key.equals("cursor")) {
8485
colorIndex = TextStyle.COLOR_INDEX_CURSOR;
86+
cursorPropExists = true;
8587
} else if (key.startsWith("color")) {
8688
try {
8789
colorIndex = Integer.parseInt(key.substring(5));
@@ -98,6 +100,27 @@ public void updateWith(Properties props) {
98100

99101
mDefaultColors[colorIndex] = colorValue;
100102
}
103+
104+
if (!cursorPropExists)
105+
setCursorColorForBackground();
106+
}
107+
108+
/**
109+
* If the "cursor" color is not set by user, we need to decide on the appropriate color that will
110+
* be visible on the current terminal background. White will not be visible on light backgrounds
111+
* and black won't be visible on dark backgrounds. So we find the perceived brightness of the
112+
* background color and if its below the threshold (too dark), we use white cursor and if its
113+
* above (too bright), we use black cursor.
114+
*/
115+
public void setCursorColorForBackground() {
116+
int backgroundColor = mDefaultColors[TextStyle.COLOR_INDEX_BACKGROUND];
117+
int brightness = TerminalColors.getPerceivedBrightnessOfColor(backgroundColor);
118+
if (brightness > 0) {
119+
if (brightness < 130)
120+
mDefaultColors[TextStyle.COLOR_INDEX_CURSOR] = 0xffffffff;
121+
else
122+
mDefaultColors[TextStyle.COLOR_INDEX_CURSOR] = 0xff000000;
123+
}
101124
}
102125

103126
}

terminal-emulator/src/main/java/com/termux/terminal/TerminalColors.java

+20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.termux.terminal;
22

3+
import android.graphics.Color;
4+
35
/** Current terminal colors (if different from default). */
46
public final class TerminalColors {
57

@@ -73,4 +75,22 @@ public void tryParseColor(int intoIndex, String textParameter) {
7375
if (c != 0) mCurrentColors[intoIndex] = c;
7476
}
7577

78+
/**
79+
* Get the perceived brightness of the color based on its RGB components.
80+
*
81+
* https://www.nbdtech.com/Blog/archive/2008/04/27/Calculating-the-Perceived-Brightness-of-a-Color.aspx
82+
* http://alienryderflex.com/hsp.html
83+
*
84+
* @param color The color code int.
85+
* @return Returns value between 0-255.
86+
*/
87+
public static int getPerceivedBrightnessOfColor(int color) {
88+
return (int)
89+
Math.floor(Math.sqrt(
90+
Math.pow(Color.red(color), 2) * 0.241 +
91+
Math.pow(Color.green(color), 2) * 0.691 +
92+
Math.pow(Color.blue(color), 2) * 0.068
93+
));
94+
}
95+
7696
}

0 commit comments

Comments
 (0)