-
-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Button: hover and pressed background colors are now derived from actu…
…al button background color (issue #21)
- Loading branch information
Showing
11 changed files
with
700 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
flatlaf-core/src/main/java/com/formdev/flatlaf/util/ColorFunctions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright 2019 FormDev Software GmbH | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.formdev.flatlaf.util; | ||
|
||
import java.awt.Color; | ||
|
||
/** | ||
* Functions that modify colors. | ||
* | ||
* @author Karl Tauber | ||
*/ | ||
public class ColorFunctions | ||
{ | ||
public static Color applyFunctions( Color color, ColorFunction[] functions ) { | ||
float[] hsl = HSLColor.fromRGB( color ); | ||
float alpha = color.getAlpha() / 255f; | ||
|
||
for( ColorFunction function : functions ) | ||
function.apply( hsl ); | ||
|
||
return HSLColor.toRGB( hsl, alpha ); | ||
} | ||
|
||
private static float clamp( float value ) { | ||
return (value < 0) | ||
? 0 | ||
: ((value > 100) | ||
? 100 | ||
: value); | ||
} | ||
|
||
//---- interface ColorFunction -------------------------------------------- | ||
|
||
public interface ColorFunction { | ||
void apply( float[] hsl ); | ||
} | ||
|
||
//---- class Lighten ------------------------------------------------------ | ||
|
||
/** | ||
* Increase the lightness of a color in the HSL color space by an absolute | ||
* or relative amount. | ||
*/ | ||
public static class Lighten | ||
implements ColorFunction | ||
{ | ||
private final float amount; | ||
private final boolean relative; | ||
private final boolean autoInverse; | ||
|
||
public Lighten( float amount, boolean relative, boolean autoInverse ) { | ||
this.amount = amount; | ||
this.relative = relative; | ||
this.autoInverse = autoInverse; | ||
} | ||
|
||
@Override | ||
public void apply( float[] hsl ) { | ||
float amount2 = autoInverse && shouldInverse( hsl ) ? -amount : amount; | ||
hsl[2] = clamp( relative | ||
? (hsl[2] * ((100 + amount2) / 100)) | ||
: (hsl[2] + amount2) ); | ||
} | ||
|
||
protected boolean shouldInverse( float[] hsl ) { | ||
return hsl[2] >= 50; | ||
} | ||
} | ||
|
||
//---- class Darken ------------------------------------------------------- | ||
|
||
/** | ||
* Decrease the lightness of a color in the HSL color space by an absolute | ||
* or relative amount. | ||
*/ | ||
public static class Darken | ||
extends Lighten | ||
{ | ||
public Darken( float amount, boolean relative, boolean autoInverse ) { | ||
super( -amount, relative, autoInverse ); | ||
} | ||
|
||
@Override | ||
protected boolean shouldInverse( float[] hsl ) { | ||
return hsl[2] < 50; | ||
} | ||
} | ||
} |
Oops, something went wrong.