I've just started with SwiftUI coming from a webdev background. I'm trying to apply a leading border to a HStack by using a Rectangle as the first element. The contents of the HStack are variable height due to text-wrapping (lineLimit = 2) and I can't seem to get the border to play nice and adopt the height of its neighbouring VStack. Here's an image (code below)
HStack(alignment: .top, spacing: 0) {
Rectangle().fill(accentColor).frame(width: 2.0)
.fixedSize(horizontal: false, vertical: false)
VStack(alignment: .leading) {
Text(communityName.uppercased())
.font(.custom("Raleway-Regular", size: 10))
.foregroundColor(Color(.sRGB, red: 0.4, green: 0.4, blue: 0.4))
.kerning(1.2)
.padding(.top, 20)
Text(name)
.font(.custom("DMSerifDisplay-Regular", size: 20))
.foregroundColor(Color(.sRGB, red: 0.2, green: 0.2, blue: 0.2))
.lineLimit(2)
.fixedSize(horizontal: false, vertical: true)
}
.padding(.leading, 10.0)
Spacer()
}
.padding(7.0)
.background(Color(.sRGB, red: 0.969, green: 0.969, blue: 0.969))
And the consumer view code:
ForumResultRow(name: "Bathrooms & Laundry", communityName: "Homeone", accentColor: Color.orange)
ForumResultRow(name: "Investor Stories & Showcase + Property Market Economics", communityName: "PropertyChat", accentColor: Color.blue)
ForumResultRow(name: "Bathrooms & Laundry", communityName: "Homeone", accentColor: Color.orange)
It seems that when rendering multiple of the same view, the border will use the same height for all that is equal to the height in the shortest instance. Any tips on how to get this to behave properly?
Here is a solution. Tested with Xcode 11.4 / iOS 13.4
var body: some View {
HStack(alignment: .top, spacing: 0) {
VStack(alignment: .leading) {
Text(communityName.uppercased())
.font(.custom("Raleway-Regular", size: 10))
.foregroundColor(Color(.sRGB, red: 0.4, green: 0.4, blue: 0.4))
.kerning(1.2)
.padding(.top, 20)
Text(name)
.font(.custom("DMSerifDisplay-Regular", size: 20))
.foregroundColor(Color(.sRGB, red: 0.2, green: 0.2, blue: 0.2))
.lineLimit(2)
.fixedSize(horizontal: false, vertical: true)
}
.padding(.leading, 10.0)
Spacer()
}
.overlay(
Rectangle().fill(accentColor).frame(width: 2.0), // << here !!
alignment: .leading)
.padding(7.0)
.background(Color(.sRGB, red: 0.969, green: 0.969, blue: 0.969))
}
Related
I'm having code as follows:
let arr = new Float32Array([-0.4, 0.3, -0.4, 0, 0.3, -0.2,
-0.5, -0.25,
0.7, 0.25,
0.9, -0.7
]);
let position_buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, position_buffer);
gl.bufferData(gl.ARRAY_BUFFER, arr, gl.STATIC_DRAW)
gl.vertexAttribPointer(position_location, 2, gl.FLOAT, false, 0 , 0);
gl.enableVertexAttribArray(position_location);
gl.drawArrays(gl.TRIANGLES, 0, 6)
If i want to change the second triangle (initial vertex -0.5, -0.25 in the arr), is there a way, to reach a gpu buffer, and tell him to redraw the vertices with an offset 3, and length of 3 ? So my first triangle wouldn't be redrawn?
It's called instanced drawing in webgl. Maybe for somebody would be helpful.
Swift 5.5, iOS 15
I have box I build using SCNBox like this using SceneKit.
let colors = [UIColor(red: 1, green: 0, blue: 0, alpha: 0.4), // front
UIColor(red: 0, green: 1, blue: 0, alpha: 0.4), // right
UIColor(red: 0, green: 0, blue: 1, alpha: 0.4), // back
UIColor(red: 1, green: 1, blue: 0, alpha: 0.4), // left
UIColor(red: 1, green: 140/255, blue: 0, alpha: 0.4), // top
UIColor(red: 1, green: 1, blue: 1, alpha: 0.4)] // bottom
let sideMaterials = colors.map { color -> SCNMaterial in
let material = SCNMaterial()
material.diffuse.contents = color
material.locksAmbientWithDiffuse = true
return material
}
cubeGeometry = SCNBox(width: 1.0, height: 1.0, length: 1.0, chamferRadius: 0.2)
cubeGeometry.materials = sideMaterials
And I am trying to figure out a way to see which face I hit when I click on it, so I using a hitTest function.
That produces an array of hits that I can look at. I tried looking at hit.geometryIndex, but that doesn't give me what I want? I want to be able to identify which of the six sides I tapped on.
I tried this answer that looked very promising, but it sadly it isn't reliable.
Identify face of a cube hit on touches began in Swift - SceneKit
hit.faceindex looks more promising to me, but how can I find out what the index values have been set too? The box is made up of triangles? Can I assume I got 12 face indexes given I got two per side?
How can I list the face index numbers?
I am trying to draw a shadow for a ModalView using the Canvas Line vertex instructions. I.e. the ModalView bottom and left sides should have a slight shadowy overlay when open. I have tried calling the ModalView property overlay_color with no effect and Canvas Line vertex instructions do not create the right effect. But I cannot seem to only draw a bottom and left border that gives the shadowy effect.
<PopScrollModal>
on_open: app.root._is_modal_open = True
on_dismiss: app.root._is_modal_open = False
id: popscroll
auto_dismiss: True
orientation: 'vertical'
size_hint: (0.94, 0.41)
border: [50, 50, 16, 16]
overlay_color: [0.1, 0.1, 0.1, 0.4]
pos_hint: {'top': 0.72}
background_normal: ''
background_color: (1, 1, 1, 0)
background: 'white.png'
canvas:
Color:
rgba: app.theme_cls.bg_dark
RoundedRectangle:
size: self.size
pos: self.pos
radius: [7,]
canvas.after:
Color:
rgba: (0.2, 0.2, 0.2, 0.4)
Line:
width: 1.
rounded_rectangle: (self.x, self.y, self.width, self.height, 7)
RecycleView:
id: view_popscroll
viewclass: 'PopScrollBut'
pos_hint: {'top': 1}
size_hint: [1, 0.99]
do_scroll_y: True
RecycleGridLayout:
cols: 1
spacing: 1
default_size: None, 70
default_size_hint: 1, None
size_hint: 1, None
size: self.minimum_size
This line instruction draws on the bottom but does not adhere to the radius of the canvas:
canvas.after:
Color:
rgba: (0.2, 0.2, 0.2, 0.4)
Line:
width: 1.
close: False
points: self.pos[0], self.pos[1], self.pos[0] + self.size[0], self.pos[1]]
The Line instruction only draws a line around the ModalView.
Can somebody help to understand how to set the Points so they only appear left and bottom or set the overlay_color in the same way?
You can do that using BorderImage. It is not well documented and difficult to understand how it is actually intended to work. But here is an example that mostly does what you want:
class MyPop(Popup):
pass
kv = '''
<MyPop>:
canvas.before:
BorderImage:
source: 'shadow32.png'
border: 30, 0, 0, 30
pos: self.x - 30, self.y - 30
size: self.width + 60, self.height + 60
'''
And here is the shadow32.png that is used above:
I am trying to extract the number of each RGB value from the sample photo like below using python pillow. But, the image is only processing the background color Green.
I have tried this script
i = Image.open(img_path, 'r')
r, g, b = i.getpixel((0, 0))
print("Red: {}, Green: {}, Blue: {}".format(r, g, b))
But the output is showing like this.
Red: 0, Green: 255, Blue: 0
the image size is (2048, 1536) and its PNG.
How do I get value of red and blue pixels? I am new to this any kind of help is appreciate.
`
You can get your unique RGB values by:
np.unique(np.array(i).reshape(-1,3),axis=0)
However, your image has more than simple green/blue/red points:
Unique sets of RGB in your image:
[[ 0 42 255]
[ 0 44 255]
[ 0 46 193]
...
[255 64 44]
[255 64 63]
[255 65 44]]
or:
print(["Red: {}, Green: {}, Blue: {}".format(r, g, b) for (r,g,b) in colors])
['Red: 0, Green: 42, Blue: 255',
'Red: 0, Green: 44, Blue: 255',
'Red: 0, Green: 46, Blue: 193',
'Red: 0, Green: 46, Blue: 207',
'Red: 0, Green: 47, Blue: 215',
'Red: 0, Green: 50, Blue: 255',
'Red: 0, Green: 53, Blue: 255',
'Red: 0, Green: 83, Blue: 90',
'Red: 0, Green: 94, Blue: 100',
'Red: 1, Green: 45, Blue: 238',
...
]
I have two squares: red with color (255, 0, 0) 50% opacity, blue with color (0, 0, 255) 50% opacity
and black opaque background.
At the intersection of these colors, Photoshop shows the color (128, 0, 64) (
photoshp screenshot
).
And I agree whith that. Blue color first blends with black background:
(0, 0, 255) * 0.5 + (0, 0, 0) * ( 1 - 0.5) = (0, 0, 127.5)
alpha = 0.5 + 1 * (1 - 0.5) = 1
Then the result is mixed with red:
(255, 0, 0) * 0.5 + (0, 0, 127.5) * (1 - 0.5) = (127.5, 0, 63.75)
alpha = 0.5 + 1 * (1 - 0.5) = 1
But if background is transparent photoshop gives color (170, 0, 85) with 75% opacity (
photoshp screenshot
).
How does it get that color? I expected (127.5, 0, 127.5) with 75% opacity, because there is nothing in background to blend with.
Following the math described in this article, alpha blending blue square with 50% opacity onto the black background with 0% opacity results in this:
alpha_bg = 0
alpha_fg = 0.5
alpha_blended = alpha_fg + alpha_bg * (1 - alpha_fg) = 0.5
color_blended = ({0, 0, 255} * alpha_fg + {0, 0, 0} * (alpha_bg * (1 - alpha_fg))) / alpha_blended =
({0, 0, 255} * 0.5 + {0, 0, 0} * 0) / 0.5 = {0, 0, 255}
Then, repeating these calculations for blending the red square with 50% opacity on top of the color we calculated above:
alpha_bg = 0.5
alpha_fg = 0.5
alpha_blended = alpha_fg + alpha_bg * (1 - alpha_fg) = 0.5 + 0.5 * (1 - 0.5) = 0.75
color_blended = ({255, 0, 0} * alpha_fg + {0, 0, 255} * (alpha_bg * (1 - alpha_fg))) / alpha_blended =
({255, 0, 0} * 0.5 + {0, 0, 255} * (0.5 * (1 - 0.5))) / 0.75 = {170, 0, 85}