关注这个SO question什么是使用格式化运算符将其应用于numpy数组的最佳方法,其中数组的格式如下所示,对应RGB值
注意RGB值已经缩放为0到1,所以多次乘以255来重新缩放
func longPressDetected(_ sender: Any){
let longPress:UILongPressGestureRecognizer = sender as! UILongPressGestureRecognizer
let state:UIGestureRecognizerState = longPress.state
let location:CGPoint = longPress.location(in: self.tableView) as CGPoint
let indexPath = self.tableView.indexPathForRow(at: location)
var snapshot:UIView!
var sourceIndexPath:NSIndexPath?
if (holderView) != nil{
snapshot = holderView
}
if (beginningIndexPath) != nil{
sourceIndexPath = beginningIndexPath
}
switch(state){
case UIGestureRecognizerState.began:
if let test = indexPath{
sourceIndexPath = indexPath! as NSIndexPath
beginningIndexPath = sourceIndexPath
let cell:UITableViewCell = self.tableView.cellForRow(at: test)!
snapshot = self.customSnapShotFrom(view: cell)
holderView = snapshot
var center:CGPoint = cell.center
snapshot.center = center
snapshot.alpha = 0.0
self.tableView.addSubview(snapshot)
UIView.animate(withDuration: 0.25, animations:{
center.y = location.y
snapshot.center = center
snapshot.transform = CGAffineTransform(scaleX: 1.05, y: 1.05)
snapshot.alpha = 0.98
cell.alpha = 0.0
},completion:{ _ in
cell.isHidden = true})
}
break
case UIGestureRecognizerState.changed:
// print("changed")
var center:CGPoint = snapshot.center
center.y = location.y
snapshot.center = center
if let test = indexPath {
let bool = indexPath!.elementsEqual(beginningIndexPath as IndexPath)
if !bool {
self.updatePriorities(draggedOverIndexPath: test as NSIndexPath)
}
}
default:
// print("finished")
let cell:UITableViewCell = self.tableView.cellForRow(at: sourceIndexPath as! IndexPath)!
cell.isHidden = false
cell.alpha = 0.0
UIView.animate(withDuration: 0.25, animations: {
snapshot.center = cell.center;
snapshot.transform = .identity
snapshot.alpha = 0.0;
cell.alpha = 1.0;
}, completion: { _ in
sourceIndexPath = nil
snapshot.removeFromSuperview()
snapshot = nil
})
}
}
func customSnapShotFrom(view:UIView) -> UIView {
let snapshot:UIView = view.snapshotView(afterScreenUpdates: true)!
snapshot.layer.masksToBounds = false;
snapshot.layer.cornerRadius = 0.0;
snapshot.layer.shadowOffset = CGSize(width: 2.0, height: 2.0);
snapshot.layer.shadowRadius = 4.0;
snapshot.layer.shadowOpacity = 1.0;
return snapshot;
}
func updatePriorities(draggedOverIndexPath:NSIndexPath) {
let firstItem: Person = fetchedResultsController.object(at: beginningIndexPath as IndexPath)
let secondItem: Person = fetchedResultsController.object(at: draggedOverIndexPath as IndexPath)
firstItem.priority = Int32(draggedOverIndexPath.row)
secondItem.priority = Int32(beginningIndexPath.row)
beginningIndexPath = draggedOverIndexPath
coreDataStack.saveContext()
}
并且您希望每行hex triplet值
答案 0 :(得分:6)
您可以使用matplotlib中的rgb2hex
。
from matplotlib.colors import rgb2hex
[ rgb2hex(A[i,:]) for i in range(A.shape[0]) ]
# ['#687847', '#979b8d', '#706d59', '#6a734d', '#8b5e66', '#5e5051']
如果您不想使用matplotlib函数,则需要在使用引用的SO答案之前将数组转换为int
。请注意,输出略有不同,我认为是由于舍入误差造成的。
B = np.array(A*255, dtype=int) # convert to int
# Define a function for the mapping
rgb2hex = lambda r,g,b: '#%02x%02x%02x' %(r,g,b)
[ rgb2hex(*B[i,:]) for i in range(B.shape[0]) ]
# ['#687846', '#979a8d', '#706d59', '#6a734d', '#8a5e66', '#5e5050']