Skip to content

Commit

Permalink
Changed: Use black or white cursor color based on terminal background…
Browse files Browse the repository at this point in the history
… instead of always white if colors.properties didn't have cursor color set

Credit for algorithm link belong to @Jamie-Landeg-Jones

Closes #2653
  • Loading branch information
agnostic-apollo committed Jun 17, 2024
1 parent 903f249 commit 160ab68
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ private void reset() {

public void updateWith(Properties props) {
reset();
boolean cursorPropExists = false;
for (Map.Entry<Object, Object> entries : props.entrySet()) {
String key = (String) entries.getKey();
String value = (String) entries.getValue();
Expand All @@ -82,6 +83,7 @@ public void updateWith(Properties props) {
colorIndex = TextStyle.COLOR_INDEX_BACKGROUND;
} else if (key.equals("cursor")) {
colorIndex = TextStyle.COLOR_INDEX_CURSOR;
cursorPropExists = true;
} else if (key.startsWith("color")) {
try {
colorIndex = Integer.parseInt(key.substring(5));
Expand All @@ -98,6 +100,27 @@ public void updateWith(Properties props) {

mDefaultColors[colorIndex] = colorValue;
}

if (!cursorPropExists)
setCursorColorForBackground();
}

/**
* If the "cursor" color is not set by user, we need to decide on the appropriate color that will
* be visible on the current terminal background. White will not be visible on light backgrounds
* and black won't be visible on dark backgrounds. So we find the perceived brightness of the
* background color and if its below the threshold (too dark), we use white cursor and if its
* above (too bright), we use black cursor.
*/
public void setCursorColorForBackground() {
int backgroundColor = mDefaultColors[TextStyle.COLOR_INDEX_BACKGROUND];
int brightness = TerminalColors.getPerceivedBrightnessOfColor(backgroundColor);
if (brightness > 0) {
if (brightness < 130)
mDefaultColors[TextStyle.COLOR_INDEX_CURSOR] = 0xffffffff;
else
mDefaultColors[TextStyle.COLOR_INDEX_CURSOR] = 0xff000000;
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.termux.terminal;

import android.graphics.Color;

/** Current terminal colors (if different from default). */
public final class TerminalColors {

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

/**
* Get the perceived brightness of the color based on its RGB components.
*
* https://www.nbdtech.com/Blog/archive/2008/04/27/Calculating-the-Perceived-Brightness-of-a-Color.aspx
* http://alienryderflex.com/hsp.html
*
* @param color The color code int.
* @return Returns value between 0-255.
*/
public static int getPerceivedBrightnessOfColor(int color) {
return (int)
Math.floor(Math.sqrt(
Math.pow(Color.red(color), 2) * 0.241 +
Math.pow(Color.green(color), 2) * 0.691 +
Math.pow(Color.blue(color), 2) * 0.068
));
}

}

0 comments on commit 160ab68

Please sign in to comment.