main_color 0.0.4

  • Readme
  • Changelog
  • Example
  • Installing
  • 62

main_color #

A package to help you get the main color from a pickture

⚙️ Installation #

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  main_color: ^0.0.3

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:main_color/main_color.dart';
  

Showcases #

Default values

Other values

Other values

Other values

Other values

[0.0.1] - 28 april 2020. #

  • only one method: fromImageBytes

[0.0.3] - 28 april 2020. #

  • add example
  • coef were not taken in account

[0.0.4] - 28 april 2020. #

  • example runs faster

example/lib/main.dart

import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:main_color/main_color.dart';

class ImagesAssets {
  static const List<String> Paths = [
    "images/1.jpg",
    "images/2.jpg",
    "images/3.jpg",
    "images/4.jpg",
  ];
}

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Material(
          child: Container(color: Colors.black54, child: ImagesLoader())),
    );
  }
}

class ImagesLoader extends StatefulWidget {
  @override
  _ImagesLoaderState createState() => _ImagesLoaderState();
}

class _ImagesLoaderState extends State<ImagesLoader> {
  Future<List<List<int>>> _imagesBytesFuture;

  @override
  void initState() {
    super.initState();
    _imagesBytesFuture = _loadAssetsBytes();
  }

  Future<List<List<int>>> _loadAssetsBytes() async {
    var bytesList = List<List<int>>();
    for (int i = 0; i < ImagesAssets.Paths.length; i++) {
      bytesList.add(await _loadAssetBytes(ImagesAssets.Paths[i]));
    }
    return bytesList;
  }

  Future<List<int>> _loadAssetBytes(String path) async {
    ByteData data = await rootBundle.load(path);
    List<int> bytes =
        data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
    return bytes;
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: _imagesBytesFuture,
      builder: (context, snapshot) {
        if (!snapshot.hasData) {
          return Center(child: CircularProgressIndicator());
        } else {
          return Example(imagesBytes: snapshot.data);
        }
      },
    );
  }
}

class Example extends StatefulWidget {
  final List<List<int>> imagesBytes;

  const Example({Key key, this.imagesBytes}) : super(key: key);

  @override
  _ExampleState createState() => _ExampleState();
}

class _ExampleState extends State<Example> {
  List<Color> _mainColors;
  double _saturationCoef = MainColor.defaultSaturationCoef;
  double _valueCoef = MainColor.defaultValueCoef;

  @override
  void initState() {
    super.initState();
    _mainColors = _getMainColors();
  }

  List<Color> _getMainColors() {
    var colors = List<Color>();
    for (int i = 0; i < widget.imagesBytes.length; i++) {
      colors.add(MainColor.fromImageBytes(
        widget.imagesBytes[i],
        staturationCoef: _saturationCoef,
        valueCoef: _valueCoef,
      ));
    }
    return colors;
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
      child: Column(
        children: <Widget>[
          _buildSlider("SaturationCoef", _saturationCoef, (double value) {
            setState(() {
              _saturationCoef = value;
              _mainColors = _getMainColors();
            });
          }, (double value) {
            setState(() {
              _saturationCoef = value;
            });
          }),
          _buildSlider("ValueCoef", _valueCoef, (double value) {
            setState(() {
              _valueCoef = value;
              _mainColors = _getMainColors();
            });
          }, (double value) {
            setState(() {
              _valueCoef = value;
            });
          }),
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 8.0),
            child: Row(children: [
              Expanded(
                child: Center(
                  child: Text(
                    "Reference image",
                    style: Theme.of(context)
                        .textTheme
                        .bodyText1
                        .copyWith(fontSize: 18, color: Colors.white),
                  ),
                ),
              ),
              Expanded(
                child: Center(
                  child: Text(
                    "Selected color",
                    style: Theme.of(context)
                        .textTheme
                        .bodyText1
                        .copyWith(fontSize: 18, color: Colors.white),
                  ),
                ),
              ),
            ]),
          ),
          Expanded(
            child: RepaintBoundary(
              child: ListView.builder(
                padding: EdgeInsets.zero,
                itemCount: ImagesAssets.Paths.length,
                itemBuilder: (BuildContext context, int index) =>
                    _buildRow(index, _mainColors),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildRow(int index, List<Color> colors) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 15.0),
      child: IntrinsicHeight(
        child: Row(
          children: [
            Expanded(
              child: SizedBox(child: Image.asset(ImagesAssets.Paths[index])),
            ),
            Expanded(
              child: Container(
                // height: 200,
                color: colors[index],
              ),
            )
          ],
        ),
      ),
    );
  }

  Widget _buildSlider(
    String coefName,
    double value,
    void Function(double) onChangeEnd,
    void Function(double) onChanged,
  ) {
    return Container(
      height: 60,
      child: Row(
        children: <Widget>[
          Expanded(
            child: Padding(
              padding: const EdgeInsets.only(left: 10),
              child: Text(
                "$coefName = ${value.toStringAsFixed(2)}",
                style: Theme.of(context)
                    .textTheme
                    .bodyText1
                    .copyWith(fontSize: 18, color: Colors.white),
              ),
            ),
          ),
          Expanded(
            child: Slider(
              value: value,
              max: 2,
              min: 0,
              onChangeEnd: onChangeEnd,
              onChanged: onChanged,
            ),
          ),
        ],
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  main_color: ^0.0.4

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:main_color/main_color.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
29
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
62
Learn more about scoring.

We analyzed this package on Jul 11, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14
  • Flutter: 1.17.5

Analysis suggestions

Package not compatible with SDK dart

Because:

  • main_color that is a package requiring null.

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
flutter 0.0.0
image ^2.1.12 2.1.14
Transitive dependencies
archive 2.0.13
args 1.6.0
charcode 1.1.3
collection 1.14.12 1.14.13
convert 2.1.1
crypto 2.1.5
meta 1.1.8 1.2.2
path 1.7.0
petitparser 3.0.4
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
xml 4.2.0
Dev dependencies
flutter_test