Skip to content

Commit

Permalink
Merge pull request #5 from oversizedev/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
aromanov91 authored Nov 28, 2024
2 parents 04554a7 + e8cd6cc commit 7d54b5e
Show file tree
Hide file tree
Showing 13 changed files with 225 additions and 211 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@ on:
push:
branches:
- '**'
tags:
- "*.*.*"
workflow_dispatch:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:

build-swiftpm:
name: Build SwiftPM
uses: oversizedev/GithubWorkflows/.github/workflows/build-swiftpm.yml@main
if: github.event_name != 'push' || !startsWith(github.ref, 'refs/tags/')
strategy:
matrix:
destination:
Expand All @@ -35,3 +42,9 @@ jobs:
if: github.ref == 'refs/heads/main'
uses: oversizedev/GithubWorkflows/.github/workflows/bump.yml@main
secrets: inherit

release:
name: Create Release
if: github.ref != 'refs/heads/main' && startsWith(github.ref, 'refs/tags/')
uses: oversizedev/GithubWorkflows/.github/workflows/release.yml@main
secrets: inherit
16 changes: 0 additions & 16 deletions .github/workflows/release.yml

This file was deleted.

5 changes: 3 additions & 2 deletions .swiftformat
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
--swiftversion 5.7
--disable preferKeyPath
--swiftversion 6.0
--disable preferKeyPath
--ifdef no-indent
60 changes: 30 additions & 30 deletions Sources/OversizeCore/Extensions/Color/Color+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,41 @@
import SwiftUI

#if os(iOS)
// swiftlint:disable large_tuple
public extension Color {
var components: (red: CGFloat, green: CGFloat, blue: CGFloat, opacity: CGFloat) {
#if canImport(UIKit)
typealias NativeColor = UIColor
#elseif canImport(AppKit)
typealias NativeColor = NSColor
#endif

var r: CGFloat = 0
var g: CGFloat = 0
var b: CGFloat = 0
var o: CGFloat = 0

guard NativeColor(self).getRed(&r, green: &g, blue: &b, alpha: &o) else {
return (0, 0, 0, 0)
}
return (r, g, b, o)
// swiftlint:disable large_tuple
public extension Color {
var components: (red: CGFloat, green: CGFloat, blue: CGFloat, opacity: CGFloat) {
#if canImport(UIKit)
typealias NativeColor = UIColor
#elseif canImport(AppKit)
typealias NativeColor = NSColor
#endif

var r: CGFloat = 0
var g: CGFloat = 0
var b: CGFloat = 0
var o: CGFloat = 0

guard NativeColor(self).getRed(&r, green: &g, blue: &b, alpha: &o) else {
return (0, 0, 0, 0)
}
return (r, g, b, o)
}

func lighter(by percentage: CGFloat = 30.0) -> Color {
adjust(by: abs(percentage))
}
func lighter(by percentage: CGFloat = 30.0) -> Color {
adjust(by: abs(percentage))
}

func darker(by percentage: CGFloat = 30.0) -> Color {
adjust(by: -1 * abs(percentage))
}
func darker(by percentage: CGFloat = 30.0) -> Color {
adjust(by: -1 * abs(percentage))
}

func adjust(by percentage: CGFloat = 30.0) -> Color {
Color(red: min(Double(components.red + percentage / 100), 1.0),
green: min(Double(components.green + percentage / 100), 1.0),
blue: min(Double(components.blue + percentage / 100), 1.0),
opacity: Double(components.opacity))
}
func adjust(by percentage: CGFloat = 30.0) -> Color {
Color(red: min(Double(components.red + percentage / 100), 1.0),
green: min(Double(components.green + percentage / 100), 1.0),
blue: min(Double(components.blue + percentage / 100), 1.0),
opacity: Double(components.opacity))
}
}
#endif

public extension Color {
Expand Down
8 changes: 4 additions & 4 deletions Sources/OversizeCore/Extensions/Color/Color+Hex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

import SwiftUI
#if canImport(UIKit)
import UIKit
import UIKit
#elseif canImport(AppKit)
import AppKit
import AppKit
#endif

public typealias ColorComponentsRGBA = (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)
Expand Down Expand Up @@ -76,9 +76,9 @@ public extension Color {

var rgba: ColorComponentsRGBA {
#if canImport(AppKit)
let color = NSColor(self).usingColorSpace(.displayP3)!
let color = NSColor(self).usingColorSpace(.displayP3)!
#elseif canImport(UIKit)
let color: UIColor = .init(self)
let color: UIColor = .init(self)
#endif

var t = (CGFloat(), CGFloat(), CGFloat(), CGFloat())
Expand Down
26 changes: 13 additions & 13 deletions Sources/OversizeCore/Extensions/Image/Image+Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@
//

#if canImport(UIKit)
import UIKit
import UIKit
#elseif canImport(AppKit)
import AppKit
import AppKit
#endif
import SwiftUI

public extension Image {
init?(data: Data) {
#if canImport(UIKit)
if let uiImage = UIImage(data: data) {
self.init(uiImage: uiImage)
} else {
return nil
}
if let uiImage = UIImage(data: data) {
self.init(uiImage: uiImage)
} else {
return nil
}
#elseif canImport(AppKit)
if let nsImage = NSImage(data: data) {
self.init(nsImage: nsImage)
} else {
return nil
}
#else
if let nsImage = NSImage(data: data) {
self.init(nsImage: nsImage)
} else {
return nil
}
#else
return nil
#endif
}
}
78 changes: 39 additions & 39 deletions Sources/OversizeCore/Extensions/Image/NSImage+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,63 @@
//

#if canImport(AppKit)
import AppKit
import AppKit
#endif

#if canImport(AppKit)
public extension NSImage {
func pngData() -> Data? {
guard let cgImage = cgImage(forProposedRect: nil, context: nil, hints: nil) else {
return nil
}

let bitmapRepresentation = NSBitmapImageRep(cgImage: cgImage)
return bitmapRepresentation.representation(using: .png, properties: [:])
public extension NSImage {
func pngData() -> Data? {
guard let cgImage = cgImage(forProposedRect: nil, context: nil, hints: nil) else {
return nil
}
}

public extension NSImage {
func jpegData(compressionQuality _: CGFloat) -> Data? {
guard let cgImage = cgImage(forProposedRect: nil, context: nil, hints: nil) else {
return nil
}
let bitmapRepresentation = NSBitmapImageRep(cgImage: cgImage)
return bitmapRepresentation.representation(using: .png, properties: [:])
}
}

let bitmapRepresentation = NSBitmapImageRep(cgImage: cgImage)
return bitmapRepresentation.representation(using: .jpeg, properties: [:])
public extension NSImage {
func jpegData(compressionQuality _: CGFloat) -> Data? {
guard let cgImage = cgImage(forProposedRect: nil, context: nil, hints: nil) else {
return nil
}

func jpegData() -> Data? {
guard let cgImage = cgImage(forProposedRect: nil, context: nil, hints: nil) else {
return nil
}
let bitmapRepresentation = NSBitmapImageRep(cgImage: cgImage)
return bitmapRepresentation.representation(using: .jpeg, properties: [:])
}

let bitmapRepresentation = NSBitmapImageRep(cgImage: cgImage)
return bitmapRepresentation.representation(using: .jpeg, properties: [:])
func jpegData() -> Data? {
guard let cgImage = cgImage(forProposedRect: nil, context: nil, hints: nil) else {
return nil
}

let bitmapRepresentation = NSBitmapImageRep(cgImage: cgImage)
return bitmapRepresentation.representation(using: .jpeg, properties: [:])
}
}

public extension NSImage {
var cgImage: CGImage? {
cgImage(forProposedRect: nil, context: nil, hints: nil)
}
public extension NSImage {
var cgImage: CGImage? {
cgImage(forProposedRect: nil, context: nil, hints: nil)
}
}

public extension NSImage {
var averageColor: NSColor? {
guard let cgImage = cgImage(forProposedRect: nil, context: nil, hints: nil) else { return nil }
let inputImage = CIImage(cgImage: cgImage)
public extension NSImage {
var averageColor: NSColor? {
guard let cgImage = cgImage(forProposedRect: nil, context: nil, hints: nil) else { return nil }
let inputImage = CIImage(cgImage: cgImage)

let extentVector = CIVector(x: inputImage.extent.origin.x, y: inputImage.extent.origin.y, z: inputImage.extent.size.width, w: inputImage.extent.size.height)
let extentVector = CIVector(x: inputImage.extent.origin.x, y: inputImage.extent.origin.y, z: inputImage.extent.size.width, w: inputImage.extent.size.height)

guard let filter = CIFilter(name: "CIAreaAverage", parameters: [kCIInputImageKey: inputImage, kCIInputExtentKey: extentVector]) else { return nil }
guard let outputImage = filter.outputImage else { return nil }
guard let filter = CIFilter(name: "CIAreaAverage", parameters: [kCIInputImageKey: inputImage, kCIInputExtentKey: extentVector]) else { return nil }
guard let outputImage = filter.outputImage else { return nil }

var bitmap = [UInt8](repeating: 0, count: 4)
let context = CIContext(options: [.workingColorSpace: kCFNull!])
context.render(outputImage, toBitmap: &bitmap, rowBytes: 4, bounds: CGRect(x: 0, y: 0, width: 1, height: 1), format: .RGBA8, colorSpace: nil)
var bitmap = [UInt8](repeating: 0, count: 4)
let context = CIContext(options: [.workingColorSpace: kCFNull!])
context.render(outputImage, toBitmap: &bitmap, rowBytes: 4, bounds: CGRect(x: 0, y: 0, width: 1, height: 1), format: .RGBA8, colorSpace: nil)

return NSColor(red: CGFloat(bitmap[0]) / 255, green: CGFloat(bitmap[1]) / 255, blue: CGFloat(bitmap[2]) / 255, alpha: CGFloat(bitmap[3]) / 255)
}
return NSColor(red: CGFloat(bitmap[0]) / 255, green: CGFloat(bitmap[1]) / 255, blue: CGFloat(bitmap[2]) / 255, alpha: CGFloat(bitmap[3]) / 255)
}
}

#endif
24 changes: 12 additions & 12 deletions Sources/OversizeCore/Extensions/Image/UIImage+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
//

#if canImport(UIKit)
import UIKit
import UIKit
#endif

#if canImport(UIKit) && !os(watchOS)
public extension UIImage {
var averageColor: UIColor? {
guard let inputImage = CIImage(image: self) else { return nil }
let extentVector = CIVector(x: inputImage.extent.origin.x, y: inputImage.extent.origin.y, z: inputImage.extent.size.width, w: inputImage.extent.size.height)
public extension UIImage {
var averageColor: UIColor? {
guard let inputImage = CIImage(image: self) else { return nil }
let extentVector = CIVector(x: inputImage.extent.origin.x, y: inputImage.extent.origin.y, z: inputImage.extent.size.width, w: inputImage.extent.size.height)

guard let filter = CIFilter(name: "CIAreaAverage", parameters: [kCIInputImageKey: inputImage, kCIInputExtentKey: extentVector]) else { return nil }
guard let outputImage = filter.outputImage else { return nil }
guard let filter = CIFilter(name: "CIAreaAverage", parameters: [kCIInputImageKey: inputImage, kCIInputExtentKey: extentVector]) else { return nil }
guard let outputImage = filter.outputImage else { return nil }

var bitmap = [UInt8](repeating: 0, count: 4)
let context = CIContext(options: [.workingColorSpace: kCFNull!])
context.render(outputImage, toBitmap: &bitmap, rowBytes: 4, bounds: CGRect(x: 0, y: 0, width: 1, height: 1), format: .RGBA8, colorSpace: nil)
var bitmap = [UInt8](repeating: 0, count: 4)
let context = CIContext(options: [.workingColorSpace: kCFNull!])
context.render(outputImage, toBitmap: &bitmap, rowBytes: 4, bounds: CGRect(x: 0, y: 0, width: 1, height: 1), format: .RGBA8, colorSpace: nil)

return UIColor(red: CGFloat(bitmap[0]) / 255, green: CGFloat(bitmap[1]) / 255, blue: CGFloat(bitmap[2]) / 255, alpha: CGFloat(bitmap[3]) / 255)
}
return UIColor(red: CGFloat(bitmap[0]) / 255, green: CGFloat(bitmap[1]) / 255, blue: CGFloat(bitmap[2]) / 255, alpha: CGFloat(bitmap[3]) / 255)
}
}
#endif
16 changes: 16 additions & 0 deletions Sources/OversizeCore/Extensions/Swift/Array+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ extension Array: @retroactive RawRepresentable where Element: Codable {
}
}

public extension Array {
func element(_ index: Int) -> Element? {
if index >= 0, index < count {
self[index]
} else {
nil
}
}
}

public extension BidirectionalCollection where Iterator.Element: Equatable {
func after(_ item: Iterator.Element, loop: Bool = false) -> Element? {
if let itemIndex = firstIndex(of: item) {
Expand Down Expand Up @@ -172,3 +182,9 @@ public extension BidirectionalCollection where Iterator.Element: Equatable {
return nil
}
}

public extension Array {
func compacted<T>() -> [T] where Element == T? {
compactMap { $0 }
}
}
10 changes: 5 additions & 5 deletions Sources/OversizeCore/Extensions/Swift/Date+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,15 @@ public extension Date {
public extension Date {
var displayTodayLabelOrDate: String {
if Calendar.current.isDateInToday(self) {
return "Today"
"Today"
} else if Calendar.current.isDateInTomorrow(self) {
return "Tomorrow"
"Tomorrow"
} else if Calendar.current.isDateInYesterday(self) {
return "Yesterday"
"Yesterday"
} else if Calendar.current.component(.year, from: self) == Calendar.current.component(.year, from: Date()) {
return "\(formatted(.dateTime.day().month(.wide)))"
"\(formatted(.dateTime.day().month(.wide)))"
} else {
return "\(formatted(.dateTime.day().month(.wide).year()))"
"\(formatted(.dateTime.day().month(.wide).year()))"
}
}
}
Expand Down
Loading

0 comments on commit 7d54b5e

Please sign in to comment.