Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
gitlab-ce
Commits
29a7c83e
Commit
29a7c83e
authored
Jul 26, 2019
by
Ammar Alakkad
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add color utils with relevant tests
parent
59a5a89c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
0 deletions
+60
-0
app/assets/javascripts/lib/utils/color_utils.js
app/assets/javascripts/lib/utils/color_utils.js
+25
-0
spec/frontend/lib/utils/color_utils_spec.js
spec/frontend/lib/utils/color_utils_spec.js
+35
-0
No files found.
app/assets/javascripts/lib/utils/color_utils.js
0 → 100644
View file @
29a7c83e
/**
* Convert hex color to rgb array
*
* @param hex string
* @returns array|null
*/
export
const
hexToRgb
=
hex
=>
{
// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
const
shorthandRegex
=
/^#
?([
a-f
\d])([
a-f
\d])([
a-f
\d])
$/i
;
const
fullHex
=
hex
.
replace
(
shorthandRegex
,
(
_m
,
r
,
g
,
b
)
=>
r
+
r
+
g
+
g
+
b
+
b
);
const
result
=
/^#
?([
a-f
\d]{2})([
a-f
\d]{2})([
a-f
\d]{2})
$/i
.
exec
(
fullHex
);
return
result
?
[
parseInt
(
result
[
1
],
16
),
parseInt
(
result
[
2
],
16
),
parseInt
(
result
[
3
],
16
)]
:
null
;
};
export
const
textColorForBackground
=
backgroundColor
=>
{
const
[
r
,
g
,
b
]
=
hexToRgb
(
backgroundColor
);
if
(
r
+
g
+
b
>
500
)
{
return
'
#333333
'
;
}
return
'
#FFFFFF
'
;
};
spec/frontend/lib/utils/color_utils_spec.js
0 → 100644
View file @
29a7c83e
import
{
textColorForBackground
,
hexToRgb
}
from
'
~/lib/utils/color_utils
'
;
describe
(
'
Color utils
'
,
()
=>
{
describe
(
'
Converting hex code to rgb
'
,
()
=>
{
it
(
'
convert hex code to rgb
'
,
()
=>
{
expect
(
hexToRgb
(
'
#000000
'
)).
toEqual
([
0
,
0
,
0
]);
expect
(
hexToRgb
(
'
#ffffff
'
)).
toEqual
([
255
,
255
,
255
]);
});
it
(
'
convert short hex code to rgb
'
,
()
=>
{
expect
(
hexToRgb
(
'
#000
'
)).
toEqual
([
0
,
0
,
0
]);
expect
(
hexToRgb
(
'
#fff
'
)).
toEqual
([
255
,
255
,
255
]);
});
it
(
'
handle conversion regardless of the characters case
'
,
()
=>
{
expect
(
hexToRgb
(
'
#f0F
'
)).
toEqual
([
255
,
0
,
255
]);
});
});
describe
(
'
Getting text color for given background
'
,
()
=>
{
// following tests are being ported from `text_color_for_bg` section in labels_helper_spec.rb
it
(
'
uses light text on dark backgrounds
'
,
()
=>
{
expect
(
textColorForBackground
(
'
#222E2E
'
)).
toEqual
(
'
#FFFFFF
'
);
});
it
(
'
uses dark text on light backgrounds
'
,
()
=>
{
expect
(
textColorForBackground
(
'
#EEEEEE
'
)).
toEqual
(
'
#333333
'
);
});
it
(
'
supports RGB triplets
'
,
()
=>
{
expect
(
textColorForBackground
(
'
#FFF
'
)).
toEqual
(
'
#333333
'
);
expect
(
textColorForBackground
(
'
#000
'
)).
toEqual
(
'
#FFFFFF
'
);
});
});
});
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