@@ -71,6 +71,7 @@ private void reset() {
71
71
72
72
public void updateWith (Properties props ) {
73
73
reset ();
74
+ boolean cursorPropExists = false ;
74
75
for (Map .Entry <Object , Object > entries : props .entrySet ()) {
75
76
String key = (String ) entries .getKey ();
76
77
String value = (String ) entries .getValue ();
@@ -82,6 +83,7 @@ public void updateWith(Properties props) {
82
83
colorIndex = TextStyle .COLOR_INDEX_BACKGROUND ;
83
84
} else if (key .equals ("cursor" )) {
84
85
colorIndex = TextStyle .COLOR_INDEX_CURSOR ;
86
+ cursorPropExists = true ;
85
87
} else if (key .startsWith ("color" )) {
86
88
try {
87
89
colorIndex = Integer .parseInt (key .substring (5 ));
@@ -98,6 +100,27 @@ public void updateWith(Properties props) {
98
100
99
101
mDefaultColors [colorIndex ] = colorValue ;
100
102
}
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
+ }
101
124
}
102
125
103
126
}
0 commit comments