Skip to content

Commit

Permalink
Released ColorModelConverterInt8StringIntHexExt.
Browse files Browse the repository at this point in the history
  • Loading branch information
signmotion committed Apr 2, 2024
1 parent 5465e36 commit c17bd14
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 48 deletions.
97 changes: 61 additions & 36 deletions lib/src/converters/int_8_string_int_hex.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ part of '../../uni_color_model.dart';

/// Converters for int 8 [String] hex.
extension ColorModelConverterInt8StringIntHexExt on String {
/// ARGB
int get argbInt8 {
/// ARGB -> ARGB
int get argbInt8ToArgbInt8 {
var hexColor = replaceAll('#', '');
if (hexColor.length == 6) {
hexColor = '00$hexColor';
Expand All @@ -15,74 +15,99 @@ extension ColorModelConverterInt8StringIntHexExt on String {
return int.parse('0x$hexColor');
}

ArgbInt8Color get argbInt8ToArgbInt8Color => ArgbInt8Color.argb(
int8Channel0Color,
int8Channel1Color,
int8Channel2Color,
int8Channel3Color,
);

List<int> get argbInt8ToArgbInt8ListIntBits => [
int8Channel0Color,
int8Channel1Color,
int8Channel2Color,
int8Channel3Color,
];

String get argbInt8ToArgbInt8StringIntHex => replaceAll('#', '');

int get argbInt8ToRgbInt8 => rgbInt8;

List<int> get argbInt8ToRgbInt8ListIntBits => [
int8Channel1Color,
int8Channel2Color,
int8Channel3Color,
];
ArgbInt8Color get argbInt8ToArgbInt8Color => length == 6
? ArgbInt8Color.argb(
0x00,
int8Channel0Color,
int8Channel1Color,
int8Channel2Color,
)
: ArgbInt8Color.argb(
int8Channel0Color,
int8Channel1Color,
int8Channel2Color,
int8Channel3Color,
);

List<int> get argbInt8ToArgbInt8ListIntBits => length == 6
? [
0x00,
int8Channel0Color,
int8Channel1Color,
int8Channel2Color,
]
: [
int8Channel0Color,
int8Channel1Color,
int8Channel2Color,
int8Channel3Color,
];

String get argbInt8ToArgbInt8StringIntHex =>
argbInt8ToArgbInt8Color.argbInt8StringIntHex;

/// ARGB -> RGB
int get argbInt8ToRgbInt8 => argbInt8ToArgbInt8 & 0x00ffffff;

List<int> get argbInt8ToRgbInt8ListIntBits => length == 6
? [
int8Channel0Color,
int8Channel1Color,
int8Channel2Color,
]
: [
int8Channel1Color,
int8Channel2Color,
int8Channel3Color,
];

String get argbInt8ToRgbInt8StringIntHex => argbInt8ToRgbInt8.hex(6);

/// RGB
int get rgbInt8 {
/// RGB -> ARGB
/// ! Adds `0xff` to alpha if absent.
int get rgbInt8ToArgbInt8 {
final hexColor = replaceAll('#', '');
if (hexColor.length != 6) {
throw ArgumentError(this);
}

return int.parse('0x$hexColor');
return int.parse('0x$hexColor').rgbInt8ToArgbInt8;
}

/// ! Adds `0xff` to alpha.
ArgbInt8Color get rgbInt8ToArgbInt8Color => ArgbInt8Color.argb(
0xff,
int8Channel0Color,
int8Channel1Color,
int8Channel2Color,
int8Channel3Color,
);

/// ! Adds `0xff` to alpha if absent.
int get rgbInt8ToArgbInt8 => rgbInt8ToArgbInt8Color.argbInt8;

/// ! Adds `0xff` to alpha.
List<int> get rgbInt8ToArgbInt8ListIntBits => [
0xff,
int8Channel0Color,
int8Channel1Color,
int8Channel2Color,
int8Channel3Color,
];

/// ! Adds `0xff` to alpha.
String get rgbInt8ToArgbInt8StringIntHex => rgbInt8ToArgbInt8.hex(8);

/// RGB -> RGB
int get rgbInt8ToRgbInt8 => rgbInt8ToArgbInt8.argbInt8ToRgbInt8;

RgbInt8Color get rgbInt8ToRgbInt8Color => RgbInt8Color.rgb(
int8Channel0Color,
int8Channel1Color,
int8Channel2Color,
int8Channel3Color,
);

List<int> get rgbInt8ToRgbInt8ListIntBits => [
int8Channel0Color,
int8Channel1Color,
int8Channel2Color,
int8Channel3Color,
];

String get rgbInt8ToRgbInt8StringIntHex => replaceAll('#', '');
String get rgbInt8ToRgbInt8StringIntHex =>
rgbInt8ToRgbInt8Color.rgbInt8StringIntHex;
}
22 changes: 11 additions & 11 deletions test/converters/int_8_string_int_hex_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import 'package:test/test.dart';
void main() {
// TODO(sign): With alpha.
const b = '0a1b2c';
const c = '#0a1b2c';
const d = '0a 1b 2c';
const e = ' 0a 1b 2c ';
// TODO(sign): const c = '#0a1b2c';
// TODO(sign): const d = '0a 1b 2c';
// TODO(sign): const e = ' 0a 1b 2c ';

group('ARGB int 8 string int hex -> ARGB int 8', () {
test('argbInt8', () {
expect(b.argbInt8, 0x0a1b2c);
test('argbInt8ToArgbInt8', () {
expect(b.argbInt8ToArgbInt8, 0x0a1b2c);
});

test('argbInt8ToArgbInt8Color', () {
Expand Down Expand Up @@ -57,6 +57,10 @@ void main() {
});

group('RGB int 8 string int hex -> ARGB int 8', () {
test('rgbInt8ToArgbInt8', () {
expect(b.rgbInt8ToArgbInt8, 0xff0a1b2c);
});

test('rgbInt8ToArgbInt8Color', () {
expect(
b.rgbInt8ToArgbInt8Color,
Expand All @@ -68,10 +72,6 @@ void main() {
));
});

test('rgbInt8ToArgbInt8', () {
expect(b.rgbInt8ToArgbInt8, 0xff0a1b2c);
});

test('rgbInt8ToArgbInt8ListIntBits', () {
expect(b.rgbInt8ToArgbInt8ListIntBits, [
0xff,
Expand All @@ -87,8 +87,8 @@ void main() {
});

group('RGB int 8 string int hex -> RGB int 8', () {
test('rgbInt8', () {
expect(b.rgbInt8, 0x0a1b2c);
test('rgbInt8ToRgbInt8', () {
expect(b.rgbInt8ToRgbInt8, 0x0a1b2c);
});

test('rgbInt8ToRgbInt8Color', () {
Expand Down
2 changes: 1 addition & 1 deletion test/extensions/string_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,5 @@ void main() {
expect(''.normalizedNameColor, '');
expect(' '.normalizedNameColor, '');
});
}, tags: ['current']);
});
}

0 comments on commit c17bd14

Please sign in to comment.