Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/java.desktop/share/classes/javax/swing/text/DefaultCaret.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -25,6 +25,7 @@

package javax.swing.text;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.HeadlessException;
import java.awt.Point;
Expand Down Expand Up @@ -690,7 +691,18 @@ public void paint(Graphics g) {
// semantics of damage we can't really get around this.
damage(r);
}
g.setColor(component.getCaretColor());
if (component.isEditable()) {
g.setColor(component.getCaretColor());
} else {
Color caretColor = component.getCaretColor();
Color bg = component.getBackground();
int red = (caretColor.getRed() + bg.getRed()) / 2;
int green = (caretColor.getGreen() + bg.getGreen()) / 2;
int blue = (caretColor.getBlue() + bg.getBlue()) / 2;
int alpha = 127;
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.

Would it be enough to change the alpha component of the caret only?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That might be simpler.
It occurred that the code above could cause an IAE since the Color constructor requires values to be in range.
If the caret is white and the bg is say rgb(2, 2,2) then you'll pass 256 and get an exception.
It needs to be clamped.

There are also methods on java.awt.Color
Color.darker()
and
Color.brighter()

which could be used, but you'd first need to examine the caret color and probably the background too, to know which one is appropriate.

ie for a light bg with a dark caret, you'll want to use brighter() to reduce the contrast.
And vice versa.

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.

If the caret is white and the bg is say rgb(2, 2,2) then you'll pass 256

No, i will not. The white is rgb(255, 255, 255) and bg is rgb(2, 2, 2) then i will pass rgb ((2 + 255) / 2, (2 + 255) / 2, (2 + 255) /2) which is rgb(128, 128, 128) which is gray - perfectly fits the purpose of the fix.

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.

Would it be enough to change the alpha component of the caret only?

Not sure it will work good on all color combinations so i did both added alpha value of 50% and brought color half way to the background.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If the caret is white and the bg is say rgb(2, 2,2) then you'll pass 256

No, i will not. The white is rgb(255, 255, 255) and bg is rgb(2, 2, 2) then i will pass rgb ((2 + 255) / 2, (2 + 255) / 2, (2 + 255) /2) which is rgb(128, 128, 128) which is gray - perfectly fits the purpose of the fix.

You're right. I mis-read the parens.

Color newCaretColor = new Color(red, green, blue, alpha);
g.setColor(newCaretColor);
}
int paintWidth = getCaretWidth(r.height);
r.x -= paintWidth >> 1;
g.fillRect(r.x, r.y, paintWidth, r.height);
Expand Down