Skip to content

Commit 8fcf9ad

Browse files
committed
Fix key press colour change
Old alpha-based implementation is dumb because it depends on the activity background colour. If the keyboard is being used in a dark mode activity where the background is black like the keys, changing the key fill alpha will have no effect. --- Re-implementation lightens dark-coloured keys and darkens light-coloured keys.
1 parent e8f1d5c commit 8fcf9ad

2 files changed

Lines changed: 29 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
## [Unreleased]
55

66
- Fixed NullPointerException for `onSinglePointerTouchEvent` of null key
7+
- Fixed key press colour change dependent on activity background
78
- Reduced space bar height
89
- Reduced digit row key height
910
- Added fillColour attribute for the keyboard as a whole

app/src/main/java/io/github/yawnoc/strokeinput/InputContainer.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ public class InputContainer
4848
private static final int KEY_REPEAT_START_MILLISECONDS = 500;
4949
private static final int KEY_LONG_PRESS_MILLISECONDS = 750;
5050

51-
private static final int DEFAULT_KEY_ALPHA = 0xFF;
52-
private static final int PRESSED_KEY_ALPHA = 0x7F;
51+
private static final float COLOUR_LIGHTNESS_CUTOFF = 0.7f;
5352

5453
// Container meta-properties
5554
private OnInputListener inputListener;
@@ -177,11 +176,11 @@ public void onDraw(Canvas canvas) {
177176

178177
keyRectangle.set(0, 0, key.width, key.height);
179178

180-
int key_fill_colour =
181-
ColorUtils.setAlphaComponent(
182-
key.keyFillColour,
183-
key == currentlyPressedKey ? PRESSED_KEY_ALPHA : DEFAULT_KEY_ALPHA
184-
);
179+
int key_fill_colour = key.keyFillColour;
180+
if (key == currentlyPressedKey) {
181+
key_fill_colour = getContrastingColour(key_fill_colour);
182+
}
183+
185184
keyFillPaint.setColor(key_fill_colour);
186185
keyBorderPaint.setColor(key.keyBorderColour);
187186
keyBorderPaint.setStrokeWidth(key.keyBorderThickness);
@@ -214,6 +213,28 @@ public void onDraw(Canvas canvas) {
214213

215214
}
216215

216+
/*
217+
Lighten a dark colour and darken a light colour.
218+
Used for key press colour changes.
219+
*/
220+
private static int getContrastingColour(int colour) {
221+
222+
float[] colourHSL = new float[3];
223+
ColorUtils.colorToHSL(colour, colourHSL);
224+
225+
float colourLightness = colourHSL[2];
226+
if (colourLightness < COLOUR_LIGHTNESS_CUTOFF) {
227+
colourLightness = (2 * colourLightness + 1) / 3;
228+
}
229+
else {
230+
colourLightness = (2 * colourLightness) / 3;
231+
}
232+
233+
colourHSL[2] = colourLightness;
234+
235+
return ColorUtils.HSLToColor(colourHSL);
236+
}
237+
217238
/*
218239
Handle logic for multiple pointers (e.g. two-thumb typing).
219240
// TODO: fix unexpected behaviour when two pointers merge

0 commit comments

Comments
 (0)