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
b1f10ceb
Unverified
Commit
b1f10ceb
authored
May 19, 2022
by
David Miguel Lozano
Committed by
GitHub
May 19, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Define `ColorSwatch.lerp()` function (#103701)
parent
440e0e2c
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
63 additions
and
0 deletions
+63
-0
colors.dart
packages/flutter/lib/src/painting/colors.dart
+38
-0
colors_test.dart
packages/flutter/test/painting/colors_test.dart
+25
-0
No files found.
packages/flutter/lib/src/painting/colors.dart
View file @
b1f10ceb
...
...
@@ -457,6 +457,44 @@ class ColorSwatch<T> extends Color {
@override
String
toString
()
=>
'
${objectRuntimeType(this, 'ColorSwatch')}
(primary value:
${super.toString()}
)'
;
/// Linearly interpolate between two [ColorSwatch]es.
///
/// It delegates to [Color.lerp] to interpolate the different colors of the
/// swatch.
///
/// If either color is null, this function linearly interpolates from a
/// transparent instance of the other color.
///
/// The `t` argument represents position on the timeline, with 0.0 meaning
/// that the interpolation has not started, returning `a` (or something
/// equivalent to `a`), 1.0 meaning that the interpolation has finished,
/// returning `b` (or something equivalent to `b`), and values in between
/// meaning that the interpolation is at the relevant point on the timeline
/// between `a` and `b`. The interpolation can be extrapolated beyond 0.0 and
/// 1.0, so negative values and values greater than 1.0 are valid (and can
/// easily be generated by curves such as [Curves.elasticInOut]). Each channel
/// will be clamped to the range 0 to 255.
///
/// Values for `t` are usually obtained from an [Animation<double>], such as
/// an [AnimationController].
static
ColorSwatch
<
T
>?
lerp
<
T
>(
ColorSwatch
<
T
>?
a
,
ColorSwatch
<
T
>?
b
,
double
t
)
{
final
Map
<
T
,
Color
>
swatch
;
if
(
b
==
null
)
{
if
(
a
==
null
)
{
return
null
;
}
else
{
swatch
=
a
.
_swatch
.
map
((
T
key
,
Color
color
)
=>
MapEntry
<
T
,
Color
>(
key
,
Color
.
lerp
(
color
,
null
,
t
)!));
}
}
else
{
if
(
a
==
null
)
{
swatch
=
b
.
_swatch
.
map
((
T
key
,
Color
color
)
=>
MapEntry
<
T
,
Color
>(
key
,
Color
.
lerp
(
null
,
color
,
t
)!));
}
else
{
swatch
=
a
.
_swatch
.
map
((
T
key
,
Color
color
)
=>
MapEntry
<
T
,
Color
>(
key
,
Color
.
lerp
(
color
,
b
[
key
],
t
)!));
}
}
return
ColorSwatch
<
T
>(
Color
.
lerp
(
a
,
b
,
t
)!.
value
,
swatch
);
}
}
/// [DiagnosticsProperty] that has an [Color] as value.
...
...
packages/flutter/test/painting/colors_test.dart
View file @
b1f10ceb
...
...
@@ -426,6 +426,31 @@ void main() {
expect
(
greens1
.
value
,
0xFF027223
);
});
test
(
'ColorSwatch.lerp'
,
()
{
const
ColorSwatch
<
int
>
swatchA
=
ColorSwatch
<
int
>(
0x00000000
,
<
int
,
Color
>{
1
:
Color
(
0x00000000
)});
const
ColorSwatch
<
int
>
swatchB
=
ColorSwatch
<
int
>(
0xFFFFFFFF
,
<
int
,
Color
>{
1
:
Color
(
0xFFFFFFFF
)});
expect
(
ColorSwatch
.
lerp
(
swatchA
,
swatchB
,
0.0
),
const
ColorSwatch
<
int
>(
0x00000000
,
<
int
,
Color
>{
1
:
Color
(
0x00000000
)}),
);
expect
(
ColorSwatch
.
lerp
(
swatchA
,
swatchB
,
0.5
),
const
ColorSwatch
<
int
>(
0x7F7F7F7F
,
<
int
,
Color
>{
1
:
Color
(
0x7F7F7F7F
)}),
);
expect
(
ColorSwatch
.
lerp
(
swatchA
,
swatchB
,
1.0
),
const
ColorSwatch
<
int
>(
0xFFFFFFFF
,
<
int
,
Color
>{
1
:
Color
(
0xFFFFFFFF
)}),
);
expect
(
ColorSwatch
.
lerp
(
swatchA
,
swatchB
,
-
0.1
),
const
ColorSwatch
<
int
>(
0x00000000
,
<
int
,
Color
>{
1
:
Color
(
0x00000000
)}),
);
expect
(
ColorSwatch
.
lerp
(
swatchA
,
swatchB
,
1.1
),
const
ColorSwatch
<
int
>(
0xFFFFFFFF
,
<
int
,
Color
>{
1
:
Color
(
0xFFFFFFFF
)}),
);
});
test
(
'ColorDiagnosticsProperty includes valueProperties in JSON'
,
()
{
ColorProperty
property
=
ColorProperty
(
'foo'
,
const
Color
.
fromARGB
(
10
,
20
,
30
,
40
));
final
Map
<
String
,
Object
>
valueProperties
=
property
.
toJsonMap
(
const
DiagnosticsSerializationDelegate
())[
'valueProperties'
]!
as
Map
<
String
,
Object
>;
...
...
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