안드로이드 RGB + Alpha

Jang Seok Woo·2021년 7월 16일
0

실무

목록 보기
57/136

안드로이드 위젯에 많은 경우 알파(투명) 값을 줘서 위젯을 투명하게 만들기도 한다. 대부분의 경우 프로젝트에서 사용하는 컬러는 /values/colors.xml 파일에 기술한다. 이 파일에 기술하는 컬러는 #AARRGGBB의 형태를 가지고 있다.

 int color = (A & 0xff) << 24 | (R & 0xff) << 16 | (G & 0xff) << 8 | (B & 0xff);

이 형태는 자신이 가지고 있는 색을 표현하기 위해서 16진수의 2자리로 표현되어 있다. 그래서 각 A, R, G, B의 범위는 0~ 255의 값을 가지고 있다. 안드로이드에서 컬러는 이 클래스(http://developer.android.com/reference/android/graphics/Color.html)에 잘 설명되어 있다.

알파(A)의 값(0~100)에 해당하는 16진수의 헥사 값을 알아보자. 참고로 투명한 정도에 따라서 0~100%의 값으로 확인해보자. 알파값은 투명(0) ~ 불투명(100)의 값을 가지고 있다. 투명도에 따른 16진수의 헥사값은 다음의 공식으로 값을 확인할 수 있다. 아래의 20%의 값은 일반적으로 투명도가 80%라고 표현을 한다.

20% : 255 0.2 = 51 : 16진수의 헥사 값 33 : 투명도가 80%라고 한다.
50% : 255
0.5 = 127.5 : 반올림해서 80

이걸 기준으로 투명도에 따른 16진수 헥사 값을 확인하는 자바 코드는 다음과 같다.

for (double i = 1; i >= 0; i -= 0.01) {
  i = Math.round(i * 100) / 100.0d;
  int rounded = (int) Math.round(i * 255);
  String hex = Integer.toHexString(rounded).toUpperCase();
   
  if (hex.length() == 1)
    hex = "0" + hex;
   
  int percent = (int) (i * 100);
   
  System.out.println(String.format("%d%% — %s", percent, hex));
}

100% — FF
99% — FC
98% — FA
97% — F7
96% — F5
95% — F2
94% — F0
93% — ED
92% — EB
91% — E8
90% — E6
89% — E3
88% — E0
87% — DE
86% — DB
85% — D9
84% — D6
83% — D4
82% — D1
81% — CF
80% — CC
79% — C9
78% — C7
77% — C4
76% — C2
75% — BF
74% — BD
73% — BA
72% — B8
71% — B5
70% — B3
69% — B0
68% — AD
67% — AB
66% — A8
65% — A6
64% — A3
63% — A1
62% — 9E
61% — 9C
60% — 99
59% — 96
57% — 94
56% — 91
56% — 8F
55% — 8C
54% — 8A
53% — 87
52% — 85
51% — 82
50% — 80
49% — 7D
48% — 7A
47% — 78
46% — 75
45% — 73
44% — 70
43% — 6E
42% — 6B
41% — 69
40% — 66
39% — 63
38% — 61
37% — 5E
36% — 5C
35% — 59
34% — 57
33% — 54
32% — 52
31% — 4F
30% — 4D
28% — 4A
28% — 47
27% — 45
26% — 42
25% — 40
24% — 3D
23% — 3B
22% — 38
21% — 36
20% — 33
19% — 30
18% — 2E
17% — 2B
16% — 29
15% — 26
14% — 24
13% — 21
12% — 1F
11% — 1C
10% — 1A
9% — 17
8% — 14
7% — 12
6% — 0F
5% — 0D
4% — 0A
3% — 08
2% — 05
1% — 03
0% — 00

아래의 0%가 투명하고 100%가 불투명하기 때문에, UI 가이드에서 90%의 투명도로 지정한 경우 #1ARRGGBB와 같이 10%의 헥사값을 추가해서 원하는 컬러를 표현할 수 있다.

alpha(), returns the alpha component value
red(), returns the red component value (or first component value in non-RGB models)
green(), returns the green component value (or second component value in non-RGB models)
blue(), returns the blue component value (or third component value in non-RGB models)
getComponent(int), returns a specific component value
getComponents(), returns all component values as an array

To easily encode color ints, it is recommended to use the static methods argb(int, int, int, int) and rgb(int, int, int). The second method omits the alpha component and assumes the color is opaque (alpha is 255). As a convenience this class also offers methods to encode color ints from components defined in the range: argb(float, float, float, float) and rgb(float, float, float).

Color longs (defined below) can be easily converted to color ints by invoking the toArgb(long) method. This method performs a color space conversion if needed.

It is also possible to create a color int by invoking the method toArgb() on a color instance.

It is also possible to create a color int by invoking the method toArgb() on a color instance.

profile
https://github.com/jsw4215

0개의 댓글