Skip to content

Commit 1da3db3

Browse files
authored
Create Similar RGB Color.java
1 parent 4212d3c commit 1da3db3

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Easy/Similar RGB Color.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public String similarRGB(String color) {
3+
StringBuilder simlarColor = new StringBuilder();
4+
simlarColor.append("#");
5+
for (int i = 1; i < 6; i += 2) {
6+
simlarColor.append(findClosestColor(color.substring(i, i + 2)));
7+
}
8+
return simlarColor.toString();
9+
}
10+
11+
private String findClosestColor(String section) {
12+
int num = Integer.parseInt(section, 16);
13+
int roundedValue = Math.round((float) num / 17);
14+
return Integer.toHexString(roundedValue).repeat(2);
15+
}
16+
}

0 commit comments

Comments
 (0)