Created
May 19, 2018 16:49
-
-
Save dnevera/6a4fc1d6dcae5e7e11064582f99a5528 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Создаем фильтр обзёрвера цветов текстуры | |
private lazy var patchColors:ColorObserver = { | |
let f = ColorObserver(context: self.context) | |
// | |
// Размер прямоугольной (квадратной) области по которой мы интерполируем | |
// (на самом деле усредняем) цвет текстуры | |
// | |
f.regionSize = 20 | |
// | |
// Добавляем к фильтру обработку событий пересчета целевой тектстуры, | |
// которая на самом деле не пересчитывается и читает в шейдере в буфер её RGB-смеплы | |
// | |
f.addObserver(destinationUpdated: { (destination) in | |
// | |
// Поскольку мы читаем только одну область то берем первый элемент массива | |
// прочитаных семполов цветов | |
// | |
var rgb = f.colors[0] | |
// представление [0-1] в NSColor | |
let color = NSColor(color: float4(rgb.r,rgb.g,rgb.b,1)) | |
// инвертируем цвет | |
let inverted_rgb = float3(1) - rgb | |
let inverted_color = NSColor(color: float4(inverted_rgb.r,inverted_rgb.g,inverted_rgb.b,1)) | |
// для отображения в textfield переведем в 8-битное представление | |
rgb = rgb * float3(255) | |
DispatchQueue.main.async { | |
// просто рисуем | |
self.patch.strokeColor = inverted_color | |
self.colorLabel.backgroundColor = color | |
self.colorLabel.stringValue = String(format: "%3.0f, %3.0f, %3.0f", rgb.r, rgb.g, rgb.b) | |
} | |
}) | |
return f | |
}() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment