Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
ca4f4fc8
Commit
ca4f4fc8
authored
Jun 05, 2017
by
Ian Hickson
Committed by
GitHub
Jun 05, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make the Brightness estimator public (#10508)
parent
2ff2274c
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
47 additions
and
29 deletions
+47
-29
theme_data.dart
packages/flutter/lib/src/material/theme_data.dart
+33
-28
theme_data_test.dart
packages/flutter/test/material/theme_data_test.dart
+14
-1
No files found.
packages/flutter/lib/src/material/theme_data.dart
View file @
ca4f4fc8
...
...
@@ -43,32 +43,6 @@ const Color _kLightThemeSplashColor = const Color(0x66C8C8C8);
const
Color
_kDarkThemeHighlightColor
=
const
Color
(
0x40CCCCCC
);
const
Color
_kDarkThemeSplashColor
=
const
Color
(
0x40CCCCCC
);
// See <https://www.w3.org/TR/WCAG20/#relativeluminancedef>
double
_linearizeColorComponent
(
double
component
)
{
if
(
component
<=
0.03928
)
return
component
/
12.92
;
return
math
.
pow
((
component
+
0.055
)
/
1.055
,
2.4
);
}
Brightness
_estimateBrightnessForColor
(
Color
color
)
{
// See <https://www.w3.org/TR/WCAG20/#relativeluminancedef>
final
double
R
=
_linearizeColorComponent
(
color
.
red
/
0xFF
);
final
double
G
=
_linearizeColorComponent
(
color
.
green
/
0xFF
);
final
double
B
=
_linearizeColorComponent
(
color
.
blue
/
0xFF
);
final
double
L
=
0.2126
*
R
+
0.7152
*
G
+
0.0722
*
B
;
// See <https://www.w3.org/TR/WCAG20/#contrast-ratiodef>
// The spec says to use kThreshold=0.0525, but Material Design appears to bias
// more towards using light text than WCAG20 recommends. Material Design spec
// doesn't say what value to use, but 0.15 seemed close to what the Material
// Design spec shows for its color palette on
// <https://material.io/guidelines/style/color.html#color-color-palette>.
const
double
kThreshold
=
0.15
;
if
((
L
+
0.05
)
*
(
L
+
0.05
)
>
kThreshold
)
return
Brightness
.
light
;
return
Brightness
.
dark
;
}
/// Holds the color and typography values for a material design theme.
///
/// Use this class to configure a [Theme] widget.
...
...
@@ -134,10 +108,10 @@ class ThemeData {
final
bool
isDark
=
brightness
==
Brightness
.
dark
;
primarySwatch
??=
Colors
.
blue
;
primaryColor
??=
isDark
?
Colors
.
grey
[
900
]
:
primarySwatch
[
500
];
primaryColorBrightness
??=
_
estimateBrightnessForColor
(
primaryColor
);
primaryColorBrightness
??=
estimateBrightnessForColor
(
primaryColor
);
final
bool
primaryIsDark
=
primaryColorBrightness
==
Brightness
.
dark
;
accentColor
??=
isDark
?
Colors
.
tealAccent
[
200
]
:
primarySwatch
[
500
];
accentColorBrightness
??=
_
estimateBrightnessForColor
(
accentColor
);
accentColorBrightness
??=
estimateBrightnessForColor
(
accentColor
);
final
bool
accentIsDark
=
accentColorBrightness
==
Brightness
.
dark
;
canvasColor
??=
isDark
?
Colors
.
grey
[
850
]
:
Colors
.
grey
[
50
];
scaffoldBackgroundColor
??=
canvasColor
;
...
...
@@ -467,6 +441,37 @@ class ThemeData {
);
}
// See <https://www.w3.org/TR/WCAG20/#relativeluminancedef>
static
double
_linearizeColorComponent
(
double
component
)
{
if
(
component
<=
0.03928
)
return
component
/
12.92
;
return
math
.
pow
((
component
+
0.055
)
/
1.055
,
2.4
);
}
/// Determines whether the given [Color] is [Brightness.light] or
/// [Brightness.dark].
///
/// This compares the luminosity of the given color to a threshold value that
/// matches the material design specification.
static
Brightness
estimateBrightnessForColor
(
Color
color
)
{
// See <https://www.w3.org/TR/WCAG20/#relativeluminancedef>
final
double
R
=
_linearizeColorComponent
(
color
.
red
/
0xFF
);
final
double
G
=
_linearizeColorComponent
(
color
.
green
/
0xFF
);
final
double
B
=
_linearizeColorComponent
(
color
.
blue
/
0xFF
);
final
double
L
=
0.2126
*
R
+
0.7152
*
G
+
0.0722
*
B
;
// See <https://www.w3.org/TR/WCAG20/#contrast-ratiodef>
// The spec says to use kThreshold=0.0525, but Material Design appears to bias
// more towards using light text than WCAG20 recommends. Material Design spec
// doesn't say what value to use, but 0.15 seemed close to what the Material
// Design spec shows for its color palette on
// <https://material.io/guidelines/style/color.html#color-color-palette>.
const
double
kThreshold
=
0.15
;
if
((
L
+
0.05
)
*
(
L
+
0.05
)
>
kThreshold
)
return
Brightness
.
light
;
return
Brightness
.
dark
;
}
/// Linearly interpolate between two themes.
///
/// The arguments must not be null.
...
...
packages/flutter/test/material/theme_data_test.dart
View file @
ca4f4fc8
...
...
@@ -90,7 +90,20 @@ void main() {
expect
(
themeData
.
accentTextTheme
.
display4
.
fontFamily
,
equals
(
'Ahem'
));
});
test
(
'Can estimate brightness'
,
()
{
test
(
'Can estimate brightness - directly'
,
()
{
expect
(
ThemeData
.
estimateBrightnessForColor
(
Colors
.
white
),
equals
(
Brightness
.
light
));
expect
(
ThemeData
.
estimateBrightnessForColor
(
Colors
.
black
),
equals
(
Brightness
.
dark
));
expect
(
ThemeData
.
estimateBrightnessForColor
(
Colors
.
blue
),
equals
(
Brightness
.
dark
));
expect
(
ThemeData
.
estimateBrightnessForColor
(
Colors
.
yellow
),
equals
(
Brightness
.
light
));
expect
(
ThemeData
.
estimateBrightnessForColor
(
Colors
.
deepOrange
),
equals
(
Brightness
.
dark
));
expect
(
ThemeData
.
estimateBrightnessForColor
(
Colors
.
orange
),
equals
(
Brightness
.
light
));
expect
(
ThemeData
.
estimateBrightnessForColor
(
Colors
.
lime
),
equals
(
Brightness
.
light
));
expect
(
ThemeData
.
estimateBrightnessForColor
(
Colors
.
grey
),
equals
(
Brightness
.
light
));
expect
(
ThemeData
.
estimateBrightnessForColor
(
Colors
.
teal
),
equals
(
Brightness
.
dark
));
expect
(
ThemeData
.
estimateBrightnessForColor
(
Colors
.
indigo
),
equals
(
Brightness
.
dark
));
});
test
(
'Can estimate brightness - indirectly'
,
()
{
expect
(
new
ThemeData
(
primaryColor:
Colors
.
white
).
primaryColorBrightness
,
equals
(
Brightness
.
light
));
expect
(
new
ThemeData
(
primaryColor:
Colors
.
black
).
primaryColorBrightness
,
equals
(
Brightness
.
dark
));
expect
(
new
ThemeData
(
primaryColor:
Colors
.
blue
).
primaryColorBrightness
,
equals
(
Brightness
.
dark
));
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment