Git Product home page Git Product logo

hue's Introduction

Hue

Hue is the all-in-one coloring utility that you'll ever need.

Version Carthage Compatible License Platform Swift

Usage

Hex

Hue IconYou can easily use hex colors with the init(hex:) convenience initializer on UIColor. It supports the following hex formats #ffffff, ffffff, #fff, fff

let white = UIColor(hex: "#ffffff")
let black = UIColor(hex: "#000000")
let red = UIColor(hex: "#ff0000")
let blue = UIColor(hex: "#0000ff")
let green = UIColor(hex: "#00ff00")
let yellow = UIColor(hex: "#ffff00")

Computed color properties

let white = UIColor(hex: "#ffffff")
let black = UIColor(hex: "#000000")

if white.isDarkColor {} // return false
if white.isBlackOrWhite {} // return true

Alpha

.alpha is a sugar for colorWithAlphaComponent, internally it does the exact same thing, think of it as a lipstick for your implementation.

let colorWithAlpha = myColor.alpha(0.75)

Gradients

You can easily create gradient layers using the gradient() method on arrays with UIColor. As an extra bonus, you can also add a transform closure if you want to modify the CAGradientLayer.

let gradient = [UIColor.blackColor(), UIColor.orangeColor()].gradient()

let secondGradient = [UIColor.blackColor(), UIColor.orangeColor()].gradient { gradient in
  gradient.locations = [0.25, 1.0]
  return gradient
}

Image colors

let image = UIImage(named: "My Image")
let (background, primary, secondary, detail) = image.colors()

Components

You can get red, green, blue, and alpha components from any UIColor by using the (red|green|blue|alpha)Component property.

let myColor = UIColor(hex: "#ffafc2")
let myColorBlueComponent = myColor.blueComponent
let myColorGreenComponent = myColor.greenComponent
let myColorRedComponent = myColor.redComponent
let myColorAlphaComponent = myColor.alphaComponent

Blending

let red = UIColor.redColor()
let green = UIColor.greenColor()
let yellow = red.addRGB(green)

let desaturatedBlue = UIColor(hex: "#aaaacc")
let saturatedBlue = desaturatedBlue.addHue(0.0, saturation: 1.0, brightness: 0.0, alpha: 0.0)

Supporting the project

If you want to support the development of this framework, you can do so by becoming a sponsor. ❤️

Examples

Hex Example screenshot

Hex

This super simple example that displays a bunch of color schemes in a Carousel view.

It uses hex to set the color for the schemes. It leverages from .isDarkColor to make the text color readable in all scenarios.

The demo also features Spots for rendering the Carousel view.

Example code:

let color = UIColor(hex: "#3b5998")
backgroundColor = color
label.textColor = color.isDark
  ? UIColor.whiteColor()
  : UIColor.darkGrayColor()

Gradients

Gradients Example screenshot

This examples shows how much fun you can have with combining CAGradientLayer with CABasicAnimation.

It uses .hex for getting the colors and .gradient() for transforming a collection of UIColor's into a CAGradientLayer.

The demo features Spots for rendering the list view and Fakery for generating random content strings.

Extract from the demo:

lazy var gradient: CAGradientLayer = [
  UIColor(hex: "#FD4340"),
  UIColor(hex: "#CE2BAE")
  ].gradient { gradient in
    gradient.speed = 0
    gradient.timeOffset = 0

    return gradient
  }

Installation

Hue is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Hue'

Hue is also available through Carthage. To install just write into your Cartfile:

github "hyperoslo/Hue"

To install Hue using Swift Package Manager with Xcode 11, just follow the instructions at https://developer.apple.com/documentation/swift_packages and import the platform specific library to the project:

import Hue

Author

Hyper made this with ❤️

Contribute

We would love you to contribute to Hue, check the CONTRIBUTING file for more info.

Credits

Credit goes out to Panic Inc who created ColorArt and @jathu for his work on UIImageColors which deeply inspired the functionality behind the image color analysis.

License

Hue is available under the MIT license. See the LICENSE file for more info.

hue's People

Contributors

aashishdhawan avatar aiden-leong avatar alisher12398 avatar bcylin avatar boherna avatar delba avatar ffittschen avatar frederik-jacques avatar gmlwhdtjd avatar guidev avatar injazz avatar iyuna avatar jwells89 avatar ko0f avatar kumapo avatar lukepistrol avatar mohpor avatar onmyway133 avatar pushchris avatar rahimkhalid avatar ramongilabert avatar rikchilvers avatar sunnyyoung avatar vadymmarkov avatar xaanimus avatar zenangst avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hue's Issues

Carthage not working properly

I am getting the following output:

% carthage update
*** Fetching Hue
No tagged versions found for github "hyperoslo/Hue"

TableView's gradients cut off in landscape

Hey I have a simple question. Have a split view controller that I am testing with Hue. Using your demo example, how do I modify it so that in landscape view the whole tableview has the gradients instead of partially cut off?

UIImage extensions not exposed in ObjC

I've installed Hue via CocoaPods and I'm trying to use the UIImage color extension within an Objective-C ViewController. But looking at the header file exposed it doesn't seem to expose any of the UIImage extensions. At first I thought this may be related to the UIImage extension not being public compared to the UIColor extension. So I forked that and changed the extension to be public but that didn't seem to help.

I'm not that familiar with using Swift within an Objective-C project, but according to a little research and the Apple docs this should be possible.

Thoughts?

Hue-Swift.h: The @interface UIImage is near the bottom.

// Generated by Apple Swift version 2.1.1 (swiftlang-700.1.101.15 clang-700.1.81)
#pragma clang diagnostic push

#if defined(__has_include) && __has_include(<swift/objc-prologue.h>)
# include <swift/objc-prologue.h>
#endif

#pragma clang diagnostic ignored "-Wauto-import"
#include <objc/NSObject.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>

#if defined(__has_include) && __has_include(<uchar.h>)
# include <uchar.h>
#elif !defined(__cplusplus) || __cplusplus < 201103L
typedef uint_least16_t char16_t;
typedef uint_least32_t char32_t;
#endif

typedef struct _NSZone NSZone;

#if !defined(SWIFT_PASTE)
# define SWIFT_PASTE_HELPER(x, y) x##y
# define SWIFT_PASTE(x, y) SWIFT_PASTE_HELPER(x, y)
#endif
#if !defined(SWIFT_METATYPE)
# define SWIFT_METATYPE(X) Class
#endif

#if defined(__has_attribute) && __has_attribute(objc_runtime_name)
# define SWIFT_RUNTIME_NAME(X) __attribute__((objc_runtime_name(X)))
#else
# define SWIFT_RUNTIME_NAME(X)
#endif
#if defined(__has_attribute) && __has_attribute(swift_name)
# define SWIFT_COMPILE_NAME(X) __attribute__((swift_name(X)))
#else
# define SWIFT_COMPILE_NAME(X)
#endif
#if !defined(SWIFT_CLASS_EXTRA)
# define SWIFT_CLASS_EXTRA
#endif
#if !defined(SWIFT_PROTOCOL_EXTRA)
# define SWIFT_PROTOCOL_EXTRA
#endif
#if !defined(SWIFT_ENUM_EXTRA)
# define SWIFT_ENUM_EXTRA
#endif
#if !defined(SWIFT_CLASS)
# if defined(__has_attribute) && __has_attribute(objc_subclassing_restricted) 
#  define SWIFT_CLASS(SWIFT_NAME) SWIFT_RUNTIME_NAME(SWIFT_NAME) __attribute__((objc_subclassing_restricted)) SWIFT_CLASS_EXTRA
#  define SWIFT_CLASS_NAMED(SWIFT_NAME) __attribute__((objc_subclassing_restricted)) SWIFT_COMPILE_NAME(SWIFT_NAME) SWIFT_CLASS_EXTRA
# else
#  define SWIFT_CLASS(SWIFT_NAME) SWIFT_RUNTIME_NAME(SWIFT_NAME) SWIFT_CLASS_EXTRA
#  define SWIFT_CLASS_NAMED(SWIFT_NAME) SWIFT_COMPILE_NAME(SWIFT_NAME) SWIFT_CLASS_EXTRA
# endif
#endif

#if !defined(SWIFT_PROTOCOL)
# define SWIFT_PROTOCOL(SWIFT_NAME) SWIFT_RUNTIME_NAME(SWIFT_NAME) SWIFT_PROTOCOL_EXTRA
# define SWIFT_PROTOCOL_NAMED(SWIFT_NAME) SWIFT_COMPILE_NAME(SWIFT_NAME) SWIFT_PROTOCOL_EXTRA
#endif

#if !defined(SWIFT_EXTENSION)
# define SWIFT_EXTENSION(M) SWIFT_PASTE(M##_Swift_, __LINE__)
#endif

#if !defined(OBJC_DESIGNATED_INITIALIZER)
# if defined(__has_attribute) && __has_attribute(objc_designated_initializer)
#  define OBJC_DESIGNATED_INITIALIZER __attribute__((objc_designated_initializer))
# else
#  define OBJC_DESIGNATED_INITIALIZER
# endif
#endif
#if !defined(SWIFT_ENUM)
# define SWIFT_ENUM(_type, _name) enum _name : _type _name; enum SWIFT_ENUM_EXTRA _name : _type
#endif
typedef float swift_float2  __attribute__((__ext_vector_type__(2)));
typedef float swift_float3  __attribute__((__ext_vector_type__(3)));
typedef float swift_float4  __attribute__((__ext_vector_type__(4)));
typedef double swift_double2  __attribute__((__ext_vector_type__(2)));
typedef double swift_double3  __attribute__((__ext_vector_type__(3)));
typedef double swift_double4  __attribute__((__ext_vector_type__(4)));
typedef int swift_int2  __attribute__((__ext_vector_type__(2)));
typedef int swift_int3  __attribute__((__ext_vector_type__(3)));
typedef int swift_int4  __attribute__((__ext_vector_type__(4)));
#if defined(__has_feature) && __has_feature(modules)
@import UIKit;
@import CoreGraphics;
#endif

#pragma clang diagnostic ignored "-Wproperty-attribute-mismatch"
#pragma clang diagnostic ignored "-Wduplicate-method-arg"

@interface UIColor (SWIFT_EXTENSION(Hue))
+ (UIColor * __nonnull)hex:(NSString * __nonnull)string;
- (UIColor * __nonnull)colorWithMinimumSaturation:(CGFloat)minSaturation;
- (UIColor * __nonnull)alpha:(CGFloat)value;
@end


@interface UIColor (SWIFT_EXTENSION(Hue))
- (NSString * __nonnull)hexWithPrefix:(BOOL)withPrefix;
@property (nonatomic, readonly) BOOL isDark;
@property (nonatomic, readonly) BOOL isBlackOrWhite;
- (BOOL)isDistinctFrom:(UIColor * __nonnull)color;
- (BOOL)isContrastingWith:(UIColor * __nonnull)color;
@end


@interface UIImage (SWIFT_EXTENSION(Hue))
@end

#pragma clang diagnostic pop

[Privacy Manifest] Implementation

Hello,

As we approach the upcoming spring and the anticipated implementation of the Privacy Manifest by Apple, it's crucial to ensure that your SDK fully complies with Apple's requirements.

Does your SDK need to include it?
If so, do you have a release date planned?

Thank you in advance for your attention to this matter.

Best regards

isDark computed property seems to be broken

Hi, the isDark computed property seems to be broken with the current version (2.0.1).

Steps to reproduce

  1. import Hue
  2. create any UIColor (I tested it from fff through all gray scales to 000)
  3. call color.isDark

Example

import Hue

let white = UIColor(hex: "fff")
let isDark = white.isDark

Expected outcome

let isDark = white.isDark // false

Actual outcome

let isDark = white.isDark // true

Edit: The "safety check" seems to be the issue, since the UIColor(hex:_) creates colors with alpha component, i.e. with 4 components

Edit2: I created a PR (#40) that fixes this issue

syntax error in hue test iOS UIColorTest

you code:
let testWhite = black.addRGB(color: white)
XCTAssertEqual(testWhite.redComponent, white.redComponent)
XCTAssertEqual(testWhite.greenComponent, white.greenComponent)
XCTAssertEqual(testWhite.blueComponent, white.blueComponent)

let testYellow = green.addRGB(color: red)
XCTAssertEqual(testYellow.redComponent, yellow.redComponent)
XCTAssertEqual(testYellow.greenComponent, yellow.greenComponent)
XCTAssertEqual(testYellow.blueComponent, yellow.blueComponent)

let testBlue = deSaturatedBlue.addHue(0.0, saturation: 1.0, brightness: 0.0, alpha: 0.0);
XCTAssertEqual(testBlue.redComponent, blue.redComponent)
XCTAssertEqual(testBlue.greenComponent, blue.greenComponent)
XCTAssertEqual(testBlue.blueComponent, blue.blueComponent)

need:
let testWhite = black.add(rgb color: white)
XCTAssertEqual(testWhite.redComponent, white.redComponent)
XCTAssertEqual(testWhite.greenComponent, white.greenComponent)
XCTAssertEqual(testWhite.blueComponent, white.blueComponent)

let testYellow = green.add(rgb color: red)
XCTAssertEqual(testYellow.redComponent, yellow.redComponent)
XCTAssertEqual(testYellow.greenComponent, yellow.greenComponent)
XCTAssertEqual(testYellow.blueComponent, yellow.blueComponent)

let testBlue = deSaturatedBlue.add(hue: 0.0, saturation: 1.0, brightness: 0.0, alpha: 0.0);
XCTAssertEqual(testBlue.redComponent, blue.redComponent)
XCTAssertEqual(testBlue.greenComponent, blue.greenComponent)
XCTAssertEqual(testBlue.blueComponent, blue.blueComponent)

fabs deprecation UIColor+Hue.swift

Good day! minor issue:

It seems under Xcode version 10.0 (10A255), fabs has been deprecated.

/Users/demonfuse/iOS/hurricane/hurricane/Pods/Hue/Source/iOS/UIColor+Hue.swift:98:8: 'fabs' is deprecated: renamed to 'abs'
/Users/demonfuse/iOS/hurricane/hurricane/Pods/Hue/Source/iOS/UIColor+Hue.swift:98:8: Use 'abs' instead
/Users/demonfuse/iOS/hurricane/hurricane/Pods/Hue/Source/iOS/UIColor+Hue.swift:98:78: 'fabs' is deprecated: renamed to 'abs'
/Users/demonfuse/iOS/hurricane/hurricane/Pods/Hue/Source/iOS/UIColor+Hue.swift:98:78: Use 'abs' instead
/Users/demonfuse/iOS/hurricane/hurricane/Pods/Hue/Source/iOS/UIColor+Hue.swift:99:40: 'fabs' is deprecated: renamed to 'abs'
/Users/demonfuse/iOS/hurricane/hurricane/Pods/Hue/Source/iOS/UIColor+Hue.swift:99:10: Use 'abs' instead
/Users/demonfuse/iOS/hurricane/hurricane/Pods/Hue/Source/iOS/UIColor+Hue.swift:99:40: 'fabs' is deprecated: renamed to 'abs'
/Users/demonfuse/iOS/hurricane/hurricane/Pods/Hue/Source/iOS/UIColor+Hue.swift:99:40: Use 'abs' instead
/Users/demonfuse/iOS/hurricane/hurricane/Pods/Hue/Source/iOS/UIColor+Hue.swift:100:12: 'fabs' is deprecated: renamed to 'abs'
/Users/demonfuse/iOS/hurricane/hurricane/Pods/Hue/Source/iOS/UIColor+Hue.swift:100:12: Use 'abs' instead
/Users/demonfuse/iOS/hurricane/hurricane/Pods/Hue/Source/iOS/UIColor+Hue.swift:100:42: 'fabs' is deprecated: renamed to 'abs'
/Users/demonfuse/iOS/hurricane/hurricane/Pods/Hue/Source/iOS/UIColor+Hue.swift:100:42: Use 'abs' instead

This was pulled using pods today.

Thanks and have a great day!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.